简体   繁体   English

sklearn:使用 array.reshape(-1, 1) 如果您的数据具有单个特征或使用 array.reshape(1, -1) 如果它包含单个样本来重塑您的数据

[英]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 sample

Hey there I'm using Label Encoder and Onehotencoder in my machine learning project sample but an error appeared while executing the code at the part where Onehotencoder executed and the error was 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.嘿,我在我的机器学习项目示例中使用了Label EncoderOnehotencoder ,但是在Onehotencoder执行的部分执行代码时出现错误,错误是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. 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. and my feature column has only two attributes Negative or Positive .我的特征列只有两个属性NegativePositive

What does this error message mean and how do I fix it此错误消息是什么意思,我该如何解决

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

I suspect the issue is that you have 2 possible labels and are treating them as separate values.我怀疑问题在于您有 2 个可能的标签,并将它们视为单独的值。 The output of an SVM is usually a single value, so your labels need to be a single value for each sample. SVM 的输出通常是单个值,因此每个样本的标签必须是单个值。 Instead of mapping the labels to one hot vectors, instead just use a single value of 1 when the label is positive and a value of 0 when the label is negative.不是将标签映射到一个热向量,而是在标签为正时使用单个值1当标签为负时使用值0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 array.reshape(1, -1) - 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 如果数据具有单个功能,则可以使用array.reshape(-1,1)重塑数据 - Reshape your data either using array.reshape(-1, 1) if your data has a single feature 使用 array.reshape(-1, 1) python 重塑数据 - Reshape your data either using array.reshape(-1, 1) python 预期 2D 数组,得到 1D 数组:array=[5.6 7. ]。 使用 array.reshape(-1, 1) 重塑数据 - Expected 2D array, got 1D array instead: array=[5.6 7. ]. Reshape your data either using array.reshape(-1, 1) ValueError:预期的 2D 数组,得到 1D 数组:array=[19. 27.896 0. 1. 0. ]。 使用 array.reshape(-1, 1) 重塑数据 - ValueError: Expected 2D array, got 1D array instead: array=[19. 27.896 0. 1. 0. ]. Reshape your data either using array.reshape(-1, 1) 这是什么原因:'ValueError: Expected 2D array, got 1D array instead: & reshape your data using array.reshape(-1, 1)'? - what's the reasons about this:'ValueError: Expected 2D array, got 1D array instead: & reshape your data using array.reshape(-1, 1)'? 使用 array.reshape(-1, 1) 重塑数组 - Reshaping array using array.reshape(-1, 1) 出现错误:使用数组重塑数据 - Getting an error: Reshape your data either using array sci-kit learn:使用 X.reshape(-1, 1) 重塑数据 - sci-kit learn: Reshape your data either using X.reshape(-1, 1) 如何创建二维数组作为 array.reshape() 方法的重写代码 - How to create a 2D array as rewrite code for the array.reshape() method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM