Search code examples
python-3.xerror-handlingnaivebayes

Copy paste a Naive Bayes example code on vscode but got errors


I copied the code from datacamp to try the Naive Bayes classification on my own on python 3.8 . but when run the code the compiler gives this error

Traceback (most recent call last):
  File "c:\Users\USER\Desktop\DATA MINING\NaiveTest.py", line 34, in <module>
    model.fit(features,label)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\naive_bayes.py", line 207, in fit
    X, y = self._validate_data(X, y)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\base.py", line 433, in _validate_data
    X, y = check_X_y(X, y, **check_params)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 814, in check_X_y
    X = check_array(X, accept_sparse=accept_sparse,
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
    return f(*args, **kwargs)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 630, in check_array
    raise ValueError(
ValueError: Expected 2D array, got scalar array instead:
array=<zip object at 0x0F2C4C28>.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

I am posting the whole code cause I'm not sure which part that causes this so I'm requesting help to solve this.

# Assigning features and label variables
weather=['Sunny','Sunny','Overcast','Rainy','Rainy','Rainy','Overcast','Sunny','Sunny','Rainy','Sunny','Overcast','Overcast','Rainy']
temp=['Hot','Hot','Hot','Mild','Cool','Cool','Cool','Mild','Cool','Mild','Mild','Mild','Hot','Mild']
play=['No','No','Yes','Yes','Yes','No','Yes','No','Yes','Yes','Yes','Yes','Yes','No']

# Import LabelEncoder
from sklearn import preprocessing
#creating labelEncoder
le = preprocessing.LabelEncoder()

# Converting string labels into numbers.
weather_encoded=le.fit_transform(weather)
print (weather_encoded)
temp_encoded=le.fit_transform(temp)
label=le.fit_transform(play)
print ("Temp:",temp_encoded)
print ("Play:",label)


#Combinig weather and temp into single listof tuples
features=zip(weather_encoded,temp_encoded)
print(list(zip(weather_encoded,temp_encoded)))
print([i for i in zip(weather_encoded,temp_encoded)])


from sklearn.naive_bayes import GaussianNB
#Create a Gaussian Classifier
model = GaussianNB()
# Train the model using the training sets
model.fit(features,label)
#Predict Output
predicted= model.predict([[0,2]]) # 0:Overcast, 2:Mild
print ("Predicted Value:", predicted)

supposedly the result something like this Predicted Value: [1] but it gave this error instead


Solution

  • What happens is that features should be a list to be passed to model.fit, currently they are type zip

    #Combinig weather and temp into single listof tuples
    features=zip(weather_encoded,temp_encoded)
    

    you may need to convert features to list, e.g.

    #Combinig weather and temp into single listof tuples
    features=list(zip(weather_encoded,temp_encoded))