[Kaggle] Digit Recognizer 手写数字识别

2020-07-13 15:15:31 浏览数 (2)

1. Baseline

  • 读取数据
代码语言:javascript复制
import pandas as pd
train = pd.read_csv('train.csv')
X_test = pd.read_csv('test.csv')
  • 特征、标签分离
代码语言:javascript复制
train.head()
y_train = train['label']
X_train = train.drop(['label'], axis=1)
X_train
  • 网格搜索 KNN 模型最佳参数
代码语言:javascript复制
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
# help(KNeighborsClassifier)
para_dict = [
    {'weights':["uniform", "distance"], 'n_neighbors':[3,4,5], 'leaf_size':[10,20]}
]
knn_clf = KNeighborsClassifier()
grid_search = GridSearchCV(knn_clf, para_dict, cv=3,scoring='accuracy',n_jobs=-1)
grid_search.fit(X_train, y_train)
代码语言:javascript复制
输出
GridSearchCV(cv=3, estimator=KNeighborsClassifier(), n_jobs=-1,
             param_grid=[{'leaf_size': [10, 20], 'n_neighbors': [3, 4, 5],
                          'weights': ['uniform', 'distance']}],
             scoring='accuracy')
  • 最佳参数
代码语言:javascript复制
grid_search.best_params_
# {'leaf_size': 10, 'n_neighbors': 4, 'weights': 'distance'}
  • 最好得分
代码语言:javascript复制
grid_search.best_score_
# 0.9677619047619048
  • 生成 test 集预测结果
代码语言:javascript复制
y_pred = grid_search.predict(X_test)
  • 写入结果文件
代码语言:javascript复制
image_id = pd.Series(range(1,len(y_pred) 1))
output = pd.DataFrame({'ImageId':image_id, 'Label':y_pred})
output.to_csv("submission.csv",  index=False) # 不要index列
  • 预测结果

排行榜

以上 KNN 模型得分 0.97067,目前排名2467

0 人点赞