{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 一.导入数据"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom time import time\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import MinMaxScaler\n\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 查看数据集信息\n* 查看测试集，缺少Label一列\n* 查看训练集是否有缺失值,结果是不存在缺失值\n* 查看测试集是否有缺失值,结果是不存在缺失值\n* 查看训练集和测试集的行列数\n* 将训练集中的特征和标签列分开"},{"metadata":{"trusted":true},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = train.iloc[:,1:]\ny = train.iloc[:,0]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 查看训练集的数字（绘图）\n1. 将长度为784的向量数据转化为28*28的矩阵\n2. 显示图片，颜色为黑白"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (10,5))\n\nfor num in range(0,10):\n    plt.subplot(2,5,num+1)\n    grid_data = X.iloc[num].values.reshape(28,28)\n    plt.imshow(grid_data, interpolation = \"none\", cmap = \"Greys\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 二.特征预处理\n* 特征预处理是数据挖掘的必要步骤, 如果不进行特征处理, 会导致模型的拟合程度下降, 分类的精确度下降。\n* 本文采用标准预处理, 使用MinMaxScaler将每个特征的值域规范化到0至1之间。\n* Sklearn模块有标准预处理的函数,直接调用即可。"},{"metadata":{},"cell_type":"markdown","source":"特征预处理,将特征的值域规范化"},{"metadata":{"trusted":true},"cell_type":"code","source":"X = MinMaxScaler().fit_transform(X)\ntest = MinMaxScaler().fit_transform(test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"分开训练集和测试集"},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 14)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 三.主成分分析\n* 此次训练集的特征较多,有784个, 我们需要进行主成分分析, 将特征降维至几十个, 这样做的好处是, 提升程序的运行速度, 提高算法的预测精准度。\n* 我们需要找出使模型准确率最高的主成分个数, 再进行建模。"},{"metadata":{},"cell_type":"markdown","source":"1. 生成主成分个数的取值列表，并对每一个取值计算支持向量机分类器预测的准确率\n2. 画出主成分和准确度的关系图，主成分n_components的临界值为0.80时，精确度最高。分别对应两个临界值提交后发现0.86的准确度更高\n3. 找出识别有误的数据并查看"},{"metadata":{"trusted":true},"cell_type":"code","source":"all_scores = []\nn_components = np.linspace(0.7,0.9,num=50, endpoint=False)\n\n#当主成分为n时,计算模型预测的准确率\ndef get_accuracy_score(n, X_train, X_test, y_train, y_test):\n    t0 = time()\n    pca = PCA(n_components = n)\n    pca.fit(X_train)\n    X_train_pca = pca.transform(X_train)\n    X_test_pca = pca.transform(X_test)\n    \n    # 使用支持向量机分类器\n    clf = svm.SVC()\n    clf.fit(X_train_pca, y_train)\n    \n    # 计算准确度\n    accuracy = clf.score(X_test_pca, y_test)\n    t1 = time()\n    print('n_components:{:.2f} , accuracy:{:.4f} , time elaps:{:.2f}s'.format(n, accuracy, t1-t0))\n    return accuracy \n\nfor n in n_components:\n    score = get_accuracy_score(n,X_train, X_test, y_train, y_test)\n    all_scores.append(score)  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 画出主成分和准确度的关系图，主成分n_components的临界值为0.80时，精确度最高\nplt.plot(n_components, all_scores, '-o')\nplt.xlabel('n_components')\nplt.ylabel('accuracy')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 找出识别有误的数据并打印\npca = PCA(n_components = 0.80)\npca.fit(X_train)\nX_train_pca = pca.transform(X_train)\nX_test_pca = pca.transform(X_test)\n\nclf = svm.SVC()\nclf.fit(X_train_pca, y_train)\ny_pred = clf.predict(X_test_pca)\n\nerrors = (y_pred != y_test)\ny_pred_errors = y_pred[errors]\ny_test_errors = y_test[errors].values\nX_test_errors = X_test[errors]\n\nprint(y_pred_errors[:5])\nprint(y_test_errors[:5])\nprint(X_test_errors[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 数据可视化,查看预测有误的数字\nn = 0\nnrows = 2\nncols = 5\n\nfig, ax = plt.subplots(nrows,ncols,figsize=(10,6))\n\nfor row in range(nrows):\n    for col in range(ncols):\n        ax[row,col].imshow((X_test_errors[n]).reshape((28,28)), cmap = \"Greys\")\n        ax[row,col].set_title(\"Predict:{}\\nTrue: {}\".format(y_pred_errors[n],y_test_errors[n]))\n        n += 1","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 四.建模\n* 调参也是关键步骤, 我们使用网格搜索, 自动选出使模型准确度最高的参数, 再进行训练和预测。\n* 即使用支持向量机预测,使用网格搜索进行调参。"},{"metadata":{},"cell_type":"markdown","source":"* n_components为0.78时, 模型的准确率最高\n* 对训练集和测试集进行PCA降低维度处理, 主成分个数为39"},{"metadata":{"trusted":true},"cell_type":"code","source":"pca = PCA(n_components=1)\npca.fit(X)\n\n# 打印主成分个数\nprint(pca.n_components_)\n\n# 对训练集和测试集进行主成分转换\nX = pca.transform(X)\ntest = pca.transform(test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"1. 使用网格搜索进行调参\n2. 使用支持向量机预测\n3. 预测结果并保存为cvs文件"},{"metadata":{"trusted":true},"cell_type":"code","source":"# 使用支持向量机预测,使用网格搜索进行调参\nclf_svc = GridSearchCV(estimator=svm.SVC(C=100.0, kernel='rbf', gamma=0.03), param_grid={ 'C': [1, 2, 4, 5], 'kernel': [ 'linear', 'rbf', 'sigmoid' ] }, cv=5, verbose=2 ) \n\n# 训练算法\nclf_svc.fit(X, y)\n\n# 显示使模型准确率最高的参数\nprint(clf_svc.best_params_)\n\npreds = clf_svc.predict(test)\nimage_id = pd.Series(range(1,len(preds)+1))\nresult_2 = pd.DataFrame({'ImageID': image_id,'Label':preds})\n\nresult_2.to_csv('result_svc.csv',index = False)\nprint('Done')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}