Python:y应该是一个1d数组,而得到了一个形状为{}的数组.格式(形状)


from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score,confusion_matrix
import pandas as pd
df=pd.read_csv('weather.csv',delimiter=',')
print(df)
x=df.values[:,0:df.shape[1]-1]
y=df.values[:,df.shape[1]-1]
x_train,y_train,x_test,y_test = train_test_split(x,y,test_size=0.5,random_state=0)
gnb=GaussianNB()
y_pred=gnb.fit(x_train,y_train).predict(x_test)
print(y_test,y_pred)
print("Number of misplaced points out of a total %d points : %d" % (x_test.shape[0],y_test!=y.pred).sum())
print(accuracy_score(y_test,y_pred))
print(confusion_matrix(y_test,y_pred)

以上是我在Google Colab中尝试的代码。但这里显示了一个错误:

"y should be a 1d array, got an array of shape {} instead.".format(shape)"

这是行中显示的错误

y_pred=gnb.fit(x_train,y_train).predict(x_test)

请帮我解决这个错误。我是一个初学者,所以用阐述来回答这个问题

您的问题是train_test_split的输出顺序与您想象的不同。

train_test_split首先返回第一个参数的拆分,然后返回第二个参数的剥离。所以你应该像一样使用它

x_train, x_test, y_test, y_test = train_test_split(x,y,test_size=0.5,random_state=0)

您可以在文档中找到更多信息和一些示例。

您可以通过检查变量值的形状来解决类似的问题。要么使用调试器,要么打印它们的形状:

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
data = np.random.rand(100, 5)  # some test data
df = pd.DataFrame(data)
x = df.values[:, :-1]  # you probably don't want to include the last column here?
y = dfvalues[:, -1]  # does the same as df.shape[1]-1
print(f"x shape: {x.shape}")  # (100, 4)
print(f"y shape: {y.shape}")  # (100,)  ==> 1d, fine
x_train, y_train, x_test, y_test = train_test_split(x,y,test_size=0.5,random_state=0)

print(f"x_train shape: {x_train.shape}")  # (50, 4)
print(f"y_train shape: {y_train.shape}")  # (50, 4)  ==> 2d, so something is wrong
print(f"x_test shape: {x_test.shape}")  # (50,) => also bad
print(f"x_test shape: {y_test.shape}")  # (50,) => also bad
gnb=GaussianNB()
y_pred=gnb.fit(x_train,y_train).predict(x_test)  # error y should be 1d ...

现在你可以看到为什么会出现错误,也可以看到哪里出了问题。然后,您可以查找产生意外输出的最后一个命令的文档。

最新更新