这是一个包含10个特性和一个类的癌症数据集。
X=df.iloc[:,1:10].values
y=df.iloc[:,[-1]].values
from sklearn.preprocessing import Imputer
imputer=Imputer(missing_values='NaN',strategy='mean',axis=1)
imputer=imputer.fit(X)
X=imputer.transform(X)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
from sklearn.svm import SVC
classifier=SVC (kernel='rbf',random_state=0)
classifier.fit(X_train,y_train)
y_pred=classifier.predict(y_test)当我执行这个任务时
ValueError: X.shape[1] = 1 should be equal to 9, the number of features at training time发布于 2018-03-25 21:26:33
您的错误是由下面的行引起的,您在该行中传递的是y_test而不是X_test
classifier.predict(y_test)完整代码:
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import Imputer
from sklearn.svm import SVC
data = load_breast_cancer()
df = pd.DataFrame(data.data, columns=data.feature_names)
X=df.iloc[:,1:10]
y = data.target
imputer=Imputer(strategy='mean',axis=1)
X = imputer.fit_transform(X)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
clf = SVC(kernel='rbf').fit(X_train, y_train)
y_pred=clf.predict(X_test)
print(clf.score(X_test, y_test))产量:
0.6842105263157895https://stackoverflow.com/questions/49480960
复制相似问题