How to convert 3 lists to 1 3D Numpy array
I have three lists that I want to convert to one list. When I try the following get this error
A = numpy.array(X,Y,Z,dtype=float)
ValueError: only 2 non-keyword arguments accepted
I haven't seen anything here that says you can only give two arguments
http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
Here is the code
import numpy
from numpy import *
X = []
Y = []
Z = []
f = open(r'C:\My.txt')
f.readline()
for line in f:
if line != '':
line = line.strip()
columns = line.split()
x = columns[2]
y = columns[3]
z = columns[4]
X.append(x)
Y.append(y) #appends data in list
Z.append(z)
A = numpy.array(X,Y,Z,dtype=float)
A.shape(3,3)
print(A)
thanks in advance
source to share
Try passing three lists as a tuple:
A = numpy.array((X, Y, Z), dtype=float)
In the documentation, the numpy.array
signature for numpy.array
is
numpy.array (object, dtype = None, copy = True, order = None, subok = False, ndmin = 0, maskna = None, ownmaskna = False)
i.e. the only argument object
is what turns into an ndarray, every other argument must be a keyword argument (hence the error message you were getting) that can be used to customize the creation of the array.
Edit In response to Surfcast23's comment in the IDE, I've tried the following:
>>> import numpy
>>> x = [0, 0, 0, 0]
>>> y = [3, 4, 4, 3]
>>> z = [3, 4, 3, 4]
>>> A = numpy.array((x, y, z), dtype=float)
>>> A
array([[ 0., 0., 0., 0.],
[ 3., 4., 4., 3.],
[ 3., 4., 3., 4.]])
>>> A.shape
(3L, 4L)
source to share
I looked at your code and found out that X [] is missing for X, Y, Z. An array cannot take two D arrays as one. Try putting [X, Y, Z] for an array and you will get the correct answer.
import numpy
from numpy import *
X = []
Y = []
Z = []
f = open(r'C:\My.txt')
f.readline()
for line in f:
if line != '':
line = line.strip()
columns = line.split()
x = columns[2]
y = columns[3]
z = columns[4]
X.append(x)
Y.append(y) #appends data in list
Z.append(z)
A = numpy.array([X,Y,Z],dtype = float)
A.shape(3,3)
print(A)
source to share