每次运行此代码时,我都会获得print语句的不同值。我很困惑为什么要这样做,因为我特意为train / test split包含了random_state参数。 (另一方面,我希望我应该对数据进行编码;它正在给出" ValueError:无法将字符串转换为float"否则)。
df = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data',
names=['buying', 'maint', 'doors', 'persons',
'lug_boot', 'safety', 'acceptability'])
# turns variables into numbers (algorithms won't let you do it otherwise)
df = df.apply(LabelEncoder().fit_transform)
print(df)
X = df.reindex(columns=['buying', 'maint', 'doors', 'persons',
'lug_boot', 'safety'])
y = df['acceptability']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
print(X_train)
# decision trees classification
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(X_train, y_train)
y_true = y_test
y_pred = clf.predict(X_test)
print(math.sqrt(mean_squared_error(y_true, y_pred)))
答案 0 :(得分:1)
DecisionTreeClassifier
也需要random_state
个参数:http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html
您所做的只是确保列车/测试分割是可重复的,但分类器还需要确保每次运行时它自己的种子是相同的
<强>更新强>
感谢@Chester VonWinchester指出:https://github.com/scikit-learn/scikit-learn/issues/8443由于sklearn的实现选择,max_features= None
可能是非确定性的,即使它应该意味着考虑所有功能。
上面的链接中有进一步的信息和讨论。