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']]
Use X = np.array(X, dtype="O") instead. Every item is stored as Python object then.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With