Borys
Borys

Reputation: 1423

Error in saving a text file in NumPy

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

Upvotes: 0

Views: 539

Answers (1)

plonser
plonser

Reputation: 3363

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.

Upvotes: 2

Related Questions