0
$\begingroup$
    classes = {0: 'Other', 1: 'Thunder'}

    x_new = p.read_csv('LeeAnnDataSet.csv', sep=',').values

    y_predict = train_test_split(x_new)

    print(classes[y_predict[0]])

    print(classes[y_predict[1]])

**Every time I try using this command, I receive the error message below. I don't understand what I may be doing wrong. **

    TypeError                                 Traceback (most recent call last)
    <ipython-input-66-5abff76d706f> in <module>
 
    4 y_predict = train_test_split(x_new)
  
    5 

    ----> 6 print(classes[y_predict[0]])
  
    7 print(classes[y_predict[1]])

    TypeError: unhashable type: 'numpy.ndarray'
$\endgroup$
1
$\begingroup$

The error is caused by passing a numpy array into a function that expects an integer value. read_csv() will read a file, and create a numpy array from the data inside. You can slice off the column of the numpy array that you want to use, convert it to a list and then pass this one by one into classes[]

# this should work, but change the value of 12 to the column you want to convert to a class label
import pandas as p
import numpy as np
from sklearn.model_selection import train_test_split

classes = {0: 'Other', 1: 'Thunder'}

x_new = p.read_csv('train.csv', sep=',').values # x_new: <class 'numpy.ndarray'>
y_predict = train_test_split(x_new) # y_predict: <class 'list'>

z = np.asarray(y_predict)[0].T  # transpose your y_predict dataset
for i in range(0,len(z)):
    print(z[12][i])
    print(classes[z[12][i]])

Issues with the datatypes in your original code:

import pandas as p
import numpy as np
from sklearn.model_selection import train_test_split
​
classes = {0: 'Other', 1: 'Thunder'}
​
x_new = p.read_csv('train.csv', sep=',').values
print('x_new:',type(x_new))
y_predict = train_test_split(x_new)
print('y_predict:',type(y_predict))
print('y_predict[0]',type(y_predict[0]),y_predict[0])
print(classes[y_predict[0]])
print(classes[y_predict[1]])
​
​
x_new: <class 'numpy.ndarray'>
y_predict: <class 'list'>
y_predict[0] <class 'numpy.ndarray'> 

[[2078 90.59 'y' ... 0 0 0]

[7384 106.47 'x' ... 0 0 0]

[2860 115.75 'ak' ... 0 0 0]

...

[7897 87.76 'f' ... 0 0 0]

[5029 87.16 'f' ... 0 0 0]

[2414 99.16 'aj' ... 0 0 0]]


    TypeError                                 Traceback (most recent call last)
    <ipython-input-57-ea39b293dd61> in <module>
         10 print('y_predict:',type(y_predict))
         11 print('y_predict[0]',type(y_predict[0]),y_predict[0])
    ---> 12 print(classes[y_predict[0]])
         13 print(classes[y_predict[1]])
    
    TypeError: unhashable type: 'numpy.ndarray'

To give you an idea of what is expected, here is an example that works:

import pandas as p
import numpy as np
from sklearn.model_selection import train_test_split

classes = {0: 'Other', 1: 'Thunder'}

y_predict = [1,1,0,0]
print(type(y_predict))
print(type(y_predict[0]))
print(classes[1])
print(classes[y_predict[0]])

<class 'list'>

<class 'int'>

Thunder

Thunder

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.