0

This is to save the arrays in the disk.

 import numpy as np, itertools

x1 = np.linspace(0.1, 3.5, 3)
x2 = np.arange(5, 24, 3)
x3 = np.arange(50.9, 91.5, 3)

def calculate(x1,x2,x3):
    res = x1**5+x2*x1+x3
    return res

products = np.array(list(itertools.product(x1,x2,x3)))

results = np.array([calculate(a,b,c) for a,b,c in products])
print results

np.savetxt('test.out', (products,results)) 

The error is :

ValueError: could not broadcast input array from shape (294,3) into shape (294)

How to solve it? The outfile will look like as follows:

0.1   5.   50.9  51.40001
0.1   5.   53.9  54.40001
2
  • How do you want the output file to look like? Commented Apr 30, 2015 at 16:34
  • @plonser I have shown it in my question now. Commented Apr 30, 2015 at 16:43

1 Answer 1

2

You have to glue the two arrays together in a compatible way. The easiest way is probably

arr_combined = np.column_stack((products,results))

np.savetxt('test.out',arr_combined)

np.column_stack adds one dimensional arrays as column vectors to a 2d array.

Sign up to request clarification or add additional context in comments.

1 Comment

add format argument to get the output same as in output example, otherwise you will get someting like this: 1.000000000000000056e-01 5.000000000000000000e+00 5.089999999999999858e+01 5.140001000000000175e+01 np.savetxt('test.out',arr_combined,fmt='%1.f %1.1f %1.1f %1.5f')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.