2017-03-04 62 views
0

當使用train_test_split for iris時,我收到輸入形狀錯誤。我不明白爲什麼。我測試了其他數據集。 train_test_split應該處理這個形狀。有什麼建議麼?謝謝ValueError:錯誤的輸入形狀(60,4)虹膜數據集train_test_split

# Decision Tree Classifier 
from sklearn import datasets 
from sklearn import metrics 
from sklearn.tree import DecisionTreeClassifier 
from sklearn.linear_model import LogisticRegression 
from sklearn.model_selection import train_test_split 
# load the iris datasets 
iris = datasets.load_iris() 
#print(iris.data) 
#print(dataset) 
# fit a CART model to the data 
model = LogisticRegression() 
from sklearn.utils import shuffle 
import numpy as np 
#print(type(dataset.data)) 

#Xtrain = dataset.data[:int(0.8*len(dataset.data))] 
#Ytrain = dataset.target[:int(0.8*len(dataset.data))] 
#Xtest = dataset.data[int(0.8*len(dataset.data)):] 
#Ytest = dataset.target[int(0.8*len(dataset.data)):] 
Xtrain, Ytrain, Xtest, Ytest = train_test_split(iris.data.astype(np.float64), iris.target.astype(np.float64), test_size=0.4, train_size=0.6) 
model.fit(Xtrain,Ytrain) 
#print(model) 
# make predictions 
expected = Ytest 
predicted = model.predict(Xtest) 
# summarize the fit of the model 
print(metrics.classification_report(expected, predicted)) 
print(metrics.confusion_matrix(expected, predicted)) 

回答

0

在代碼中更改以下行,它應該工作!

Xtrain, Ytrain, Xtest, Ytest = train_test_split(iris.data.astype(np.float64), iris.target.astype(np.float64), test_size=0.4, train_size=0.6) 

到:

Xtrain, Xtest, Ytrain, Ytest = train_test_split(iris.data.astype(np.float64), iris.target.astype(np.float64), test_size=0.4, train_size=0.6) 

您接受這些參數的順序是不正確的。請參閱:http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html

+0

非常感謝TheChetan,經過幾個小時的編碼,我錯過了樹林。 –

相關問題