Note
Go to the end to download the full example code. or to run this example in your browser via Binder
数字分类练习#
关于在数字数据集上使用分类技术的教程练习。
本练习用于 clf_tut 部分的 supervised_learning_tut 章节的 stat_learn_tut_index 。
KNN score: 0.961111
LogisticRegression score: 0.933333
from sklearn import datasets, linear_model, neighbors
X_digits, y_digits = datasets.load_digits(return_X_y=True)
X_digits = X_digits / X_digits.max()
n_samples = len(X_digits)
X_train = X_digits[: int(0.9 * n_samples)]
y_train = y_digits[: int(0.9 * n_samples)]
X_test = X_digits[int(0.9 * n_samples) :]
y_test = y_digits[int(0.9 * n_samples) :]
knn = neighbors.KNeighborsClassifier()
logistic = linear_model.LogisticRegression(max_iter=1000)
print("KNN score: %f" % knn.fit(X_train, y_train).score(X_test, y_test))
print(
"LogisticRegression score: %f"
% logistic.fit(X_train, y_train).score(X_test, y_test)
)
Total running time of the script: (0 minutes 0.052 seconds)
Related examples
流水线:将PCA和逻辑回归连接起来
比较有无邻域成分分析的最近邻分类
用于数字分类的受限玻尔兹曼机特征
对比MLPClassifier的随机学习策略