SVC 中的 ValueError
ValueError while in SVC
这是一个具有 10 个特征和 class 的癌症数据集。
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
您的错误是由以下行引起的,您在其中传递了 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.6842105263157895
这是一个具有 10 个特征和 class 的癌症数据集。
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
您的错误是由以下行引起的,您在其中传递了 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.6842105263157895