sklearn:如果数据具有单个特征,则使用 array.reshape(-1, 1) 或 array.reshape(



嘿,我在机器学习项目示例中使用了Label EncoderOnehotencoder,但是在执行Onehotencoder的部分执行代码时出现错误,并且错误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.并且我的功能列只有两个属性NegativePositive

此错误消息是什么意思以及如何解决它

#read data set from excel 
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('diab.csv')
feature=dataset.iloc[:,:-1].values
lablel=dataset.iloc[:,-1].values
#convert string data to binary 
#transform sting data in lablel column to decimal/binary 0 /1
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
lab=LabelEncoder()
lablel=lab.fit_transform(lablel)
onehotencoder=OneHotEncoder()
lablel=onehotencoder.fit_transform(lablel).toarray()

#create trainning model and test it 
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(feature,lablel,test_size=0.30)

#fitting SVM to trainnong set 
from sklearn.svm import SVC
classifier=SVC(kernel='linear',random_state=0)
classifier.fit(x_train,y_train)
y_pred=classifier.predict(x_test)

#making the confusion matrix 
from sklearn.metrics import confusion_matrix
cm=confusion_matrix(y_test, y_pred)
from sklearn.neighbors import KNeighborsClassifier
my_classifier=KNeighborsClassifier()
my_classifier.fit(x_train,y_train)
prediction=my_classifier.predict(x_test)
print(prediction)

from sklearn.metrics import accuracy_score
print (accuracy_score(y_test,prediction))
plot=plt.plot((prediction), 'b', label='GreenDots')
plt.show()

我怀疑问题是您有 2 个可能的标签并将它们视为单独的值。 SVM 的输出通常是单个值,因此每个样本的标签必须是单个值。 当标签为正时,只需使用单个值1,当标签为0负时,只需使用单个值 ,而不是将标签映射到一个热向量。

最新更新