在 vscode 上复制粘贴朴素贝叶斯示例代码但出现错误

Posted

技术标签:

【中文标题】在 vscode 上复制粘贴朴素贝叶斯示例代码但出现错误【英文标题】:Copy paste a Naive Bayes example code on vscode but got errors 【发布时间】:2021-07-09 19:05:06 【问题描述】:

我从 datacamp 复制了代码,以在 python 3.8 上自己尝试朴素贝叶斯分类。但是当运行代码时,编译器会给出这个错误

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.

我发布了整个代码,因为我不确定是哪个部分导致了这个问题,所以我请求帮助来解决这个问题。

# 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)

据说结果类似于Predicted Value: [1] 但它却给出了这个错误

【问题讨论】:

【参考方案1】:

发生的情况是特征应该是要传递给model.fit的列表,目前它们是zip类型

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

您可能需要将特征转换为列表,例如

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

【讨论】:

以上是关于在 vscode 上复制粘贴朴素贝叶斯示例代码但出现错误的主要内容,如果未能解决你的问题,请参考以下文章

NLTK朴素贝叶斯,文本分类代码示例 采样 SMSSpamCollection数据集下载

基于C++的朴素贝叶斯分类器

基于C++的朴素贝叶斯分类器

基于C++的朴素贝叶斯分类器

朴素贝叶斯(naive bayes)原理小结

干货 | 朴素贝叶斯python代码实现