Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep original data type when convert list of list into numpy array

Tags:

python

list

numpy

How do I keep the original data type when convert list of list into numpy array?

I used np.array, np.matrix to convert list of list into numpy array. But it turns out that all of int become string. Python version is 3.7.x.

X = [[3, 'aa', 10],                 
     [1, 'bb', 22],                      
     [2, 'cc', 28],                      
     [5, 'bb', 32],                      
     [4, 'cc', 32]]
# X is a list of list
X = np.array(X)
return X

# X becomes
[['3' 'aa' '10']
 ['1' 'bb' '22']
 ['2' 'cc' '28']
 ['5' 'bb' '32']
 ['4' 'cc' '32']]
like image 777
nosense Avatar asked Nov 30 '25 04:11

nosense


1 Answers

Use X = np.array(X, dtype="O") instead. Every item is stored as Python object then.

like image 57
Michael Butscher Avatar answered Dec 02 '25 17:12

Michael Butscher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!