如何使用Python中的train_test_split修复错误?

时间:2019-06-04 09:07:22

标签: python

from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x=np.array([0.1,0.2,0.7,8.0,45.0,56.0,66.0,0.7,0.6,64.0])
y=np.array([0,0,0,1,1,1,1,0,0,1])
x = np.array(x).reshape((1, -1))
y = np.array(y).reshape((1, -1))
x_train, x_test, y_train, y_test = train_test_split(x, y,test_size=0.4,         train_size=0.5, random_state=7, stratify=y)
knn = KNeighborsClassifier()
knn.fit(y_train, x_train)
y_train_predict = knn.predict(x_train)
y_test_predict = knn.predict(x_test)
print(y_train_predict)
print(y_test_predict)

错误:

  

在n_samples = 1,test_size = 0.4和train_size = 0.5的情况下,生成的火车集将为空。调整任何上述参数。

1 个答案:

答案 0 :(得分:3)

尝试:

 x = np.array(x).reshape(-1, 1)
 y = np.array(y).reshape(-1, 1)