【问题标题】:sklearn: 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 samplesklearn:如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 array.reshape(1, -1)
【发布时间】:2020-01-06 00:19:48
【问题描述】:

嘿,我在机器学习项目示例中使用了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()

【问题讨论】:

  • 请提供错误消息,包括行号和堆栈跟踪。
  • "onehotencoder=OneHotEncoder() labell=onehotencoder.fit_transform(labell).toarray() " 在这些代码行中
  • 您能在将label 的形状传递给OneHotEncoder 之前打印它吗?
  • 形状是一个 d 数组

标签: python machine-learning scikit-learn artificial-intelligence


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2020-07-06
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    相关资源
    最近更新 更多