{"cells":[{"metadata":{},"cell_type":"markdown","source":"## AutoML 完成手写字符分类参考答案"},{"metadata":{},"cell_type":"markdown","source":"👉 报名 [楼+ 机器学习实战](https://www.shiyanlou.com/louplus/ml) 课程。"},{"metadata":{},"cell_type":"markdown","source":"### 安装 auto-sklearn"},{"metadata":{"trusted":true},"cell_type":"code","source":"!apt-get install build-essential swig","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install auto-sklearn==0.6.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 测试安装\nimport autosklearn","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 读取数据"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv(\"../input/train.csv\")\ndf.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = df.iloc[:, 1:]  # 特征\ny = df['label']  # 目标值\n\nX.shape, y.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 使用 autosklearn 搜索模型"},{"metadata":{"trusted":true},"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')  # 忽略代码警告","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from autosklearn.classification import AutoSklearnClassifier\n\n# 限制算法搜索最大时间，更快得到结果\nauto_model = AutoSklearnClassifier() # 默认最长搜索时间为 60 分钟\nauto_model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"auto_model.fit(X, y)  # 训练","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 测试数据推理"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test = pd.read_csv(\"../input/test.csv\")\ndf_pred = pd.read_csv(\"../input/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = auto_model.predict(df_test)\ndf_pred['Label'] = preds\ndf_pred.to_csv(\"preds.csv\", index=None) # 保存推理文件","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}