拆分数据

让我们看看如何对数据进行拆分, 以便训练和测试.

怎么做...?

  • 在上一节中的python文件中添加如下代码:
from sklearn.model_selection import train_test_split, cross_val_score
# cross_val_score模块在sklearn 18.1以后放在了model_selection中


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=5)
# 这里使用75%数据作为训练数据, 25%为测试训练
classifier_gaussiannb_new = GaussianNB()
classifier_gaussiannb_new.fit(X_train, y_train)
  • 用测试数据评估分类器
y_test_pred = classifier_gaussiannb_new.predict(X_test)
  • 计算精确率
accuracy = 100.0 * (y_test == y_test_pred).sum() / X_test.shape[0]
print ("Accuracy of the classifier =", round(accuracy, 2), "%")
  • 绘制图形
plot_classifier(classifier_gaussiannb_new, X_test, y_test)

results matching ""

    No results matching ""