{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## 1. 数据读取","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler , MinMaxScaler\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.naive_bayes import  GaussianNB \nfrom sklearn.svm import SVC\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score\nfrom sklearn.neighbors import KNeighborsClassifier\n\n\ndf = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv' , index_col=0)\n\nclasses_ditc = {}\nclasses = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')['label'].unique().tolist()\nfor value , key in enumerate(classes):\n    classes_ditc[key] = value\n\nclasses_ditc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-02T10:10:23.707619Z","iopub.execute_input":"2023-11-02T10:10:23.7084Z","iopub.status.idle":"2023-11-02T10:10:26.178782Z","shell.execute_reply.started":"2023-11-02T10:10:23.708353Z","shell.execute_reply":"2023-11-02T10:10:26.1776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install py7zr\n# import py7zr\n\n# def extract_7z(archive_path, extract_path):\n#     with py7zr.SevenZipFile(archive_path, mode='r') as z:\n#         z.extractall(path=extract_path)\n#     print(\"解压完成！\")\n\n# archive_path = '/kaggle/input/cifar-10/test.7z'  # 替换为您的7z文件路径\n# extract_path = './test/'  # 替换为您希望解压到的文件夹路径\n\n# extract_7z(archive_path, extract_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:10:26.180859Z","iopub.execute_input":"2023-11-02T10:10:26.181815Z","iopub.status.idle":"2023-11-02T10:10:26.186966Z","shell.execute_reply.started":"2023-11-02T10:10:26.181773Z","shell.execute_reply":"2023-11-02T10:10:26.185698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.reset_index(drop=True , inplace=True)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:10:26.188129Z","iopub.execute_input":"2023-11-02T10:10:26.188448Z","iopub.status.idle":"2023-11-02T10:10:26.213485Z","shell.execute_reply.started":"2023-11-02T10:10:26.188421Z","shell.execute_reply":"2023-11-02T10:10:26.212524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:10:26.216068Z","iopub.execute_input":"2023-11-02T10:10:26.216432Z","iopub.status.idle":"2023-11-02T10:10:26.233247Z","shell.execute_reply.started":"2023-11-02T10:10:26.216402Z","shell.execute_reply":"2023-11-02T10:10:26.231534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_path(data_path):\n    result = []\n    for id , p in enumerate(tqdm(os.listdir(data_path))): # 用作测试用\n        path = data_path + p\n        ary = transfor2array(path , False)\n        result.append(ary)\n    return np.array(result)\n\ndef transfor2array(path , flag = False):\n    # 读取图像文件\n    image = Image.open(path)\n\n    # 将图像转换为numpy数组\n    image_array = np.array(image).tolist()\n    \n    # 显示图像\n    if flag:\n        plt.imshow(image_array)\n        plt.axis('off')  # 关闭坐标轴\n        plt.show()\n    return image_array\n\n\ntrain_path = 'train/train/'\ntest_path = 'test/test/'\ntrain_ds = read_path(train_path) , \n# test_ds = read_path(test_path)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:10:26.234428Z","iopub.execute_input":"2023-11-02T10:10:26.234811Z","iopub.status.idle":"2023-11-02T10:12:06.054502Z","shell.execute_reply.started":"2023-11-02T10:10:26.234782Z","shell.execute_reply":"2023-11-02T10:12:06.052124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show\n_ = transfor2array('train/train/26248.png' , True)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:06.058519Z","iopub.execute_input":"2023-11-02T10:12:06.058988Z","iopub.status.idle":"2023-11-02T10:12:06.172224Z","shell.execute_reply.started":"2023-11-02T10:12:06.058952Z","shell.execute_reply":"2023-11-02T10:12:06.170403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2.特征工程","metadata":{}},{"cell_type":"code","source":"# train_ds = train_ds.reshape(len(df) , -1) # reshape\ntrain_ds = np.array(train_ds).reshape(len(df),-1)\ntrain_df = pd.concat([df['label'].map(lambda x : classes_ditc[x]) , pd.DataFrame(train_ds)] , axis = 1 )\n\n#利用统计值\ntrain_df['mean'] = train_df.iloc[:, 1:].mean(axis = 1)\ntrain_df['std'] = train_df.iloc[:, 1:].std(axis = 1)\ntrain_df['var'] = train_df.iloc[:, 1:].var(axis = 1)\ntrain_df['median'] = train_df.iloc[:, 1:].median(axis = 1)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:06.174899Z","iopub.execute_input":"2023-11-02T10:12:06.176516Z","iopub.status.idle":"2023-11-02T10:12:21.251645Z","shell.execute_reply.started":"2023-11-02T10:12:06.176455Z","shell.execute_reply":"2023-11-02T10:12:21.25051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_hist(data):\n    # 绘制直方图\n    plt.hist(data, bins=10, edgecolor='black')  # 设置分组数为5，设置边界颜色为黑色\n\n    # 设置图形属性\n    plt.title('Histogram')  # 设置标题\n    plt.xlabel('Value')  # 设置x轴标签\n    plt.ylabel('Frequency')  # 设置y轴标签\n\n    # 显示图形\n    plt.show()\n\n\n# 观察不同类型的分布\nfor i in range(10):\n    class_df = train_df[train_df['label'] == i]\n    data = class_df.iloc[:,1:-4].to_numpy().reshape((-1))\n    plot_hist(data)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:21.253039Z","iopub.execute_input":"2023-11-02T10:12:21.253463Z","iopub.status.idle":"2023-11-02T10:12:28.974114Z","shell.execute_reply.started":"2023-11-02T10:12:21.253431Z","shell.execute_reply":"2023-11-02T10:12:28.972614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:28.975607Z","iopub.execute_input":"2023-11-02T10:12:28.975929Z","iopub.status.idle":"2023-11-02T10:12:29.025275Z","shell.execute_reply.started":"2023-11-02T10:12:28.975901Z","shell.execute_reply":"2023-11-02T10:12:29.024066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n\n# 准备数据\nX = train_df.iloc[: , 1:-4]  \n\n# 创建PCA对象，指定降维后的维度\npca = PCA(n_components= 100)  \n\n# 拟合数据并进行降维\nX_reduced = pca.fit_transform(X)\n\n# 与之前的统计值进行合并\nX = np.concatenate((X_reduced, train_df.iloc[: , -4:].to_numpy()), axis=1)\nprint(X.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:29.029038Z","iopub.execute_input":"2023-11-02T10:12:29.029411Z","iopub.status.idle":"2023-11-02T10:12:47.789972Z","shell.execute_reply.started":"2023-11-02T10:12:29.029381Z","shell.execute_reply":"2023-11-02T10:12:47.788053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 特征构建","metadata":{}},{"cell_type":"code","source":"random_state = 42\nx_train, x_val, y_train, y_val = train_test_split(X, train_df['label'], test_size=0.2, random_state= random_state)\n\n# sclar\nscaler = StandardScaler()\nx_train_scaled = scaler.fit_transform(x_train)\nx_val_scaled = scaler.transform(x_val)\nprint(x_train.shape ,x_val.shape  ,y_train.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:47.79262Z","iopub.execute_input":"2023-11-02T10:12:47.793557Z","iopub.status.idle":"2023-11-02T10:12:47.896563Z","shell.execute_reply.started":"2023-11-02T10:12:47.793514Z","shell.execute_reply":"2023-11-02T10:12:47.895388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 决策树的需要离散化\n## DT的数据需要离散化,将连续的数据离散成10份\ndef qcut_data(data):\n    data_copy = data.copy() ##同样需要copy下\n    for i in range(data.shape[1]):\n        if len(np.unique(data_copy[:,i])) <=5: ##overall 一列不需要离散化\n            pass\n        else:\n            data_copy[:,i] = pd.qcut(data_copy[:,i], 10, precision=0, labels=False, duplicates='drop')\n    return data_copy\n\nx_train_DT = qcut_data(x_train_scaled)\nx_val_DT = qcut_data(x_val_scaled)  ","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:47.898513Z","iopub.execute_input":"2023-11-02T10:12:47.899423Z","iopub.status.idle":"2023-11-02T10:12:48.934061Z","shell.execute_reply.started":"2023-11-02T10:12:47.899381Z","shell.execute_reply":"2023-11-02T10:12:48.932814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 模型和参数选择","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import AdaBoostClassifier,BaggingClassifier , AdaBoostRegressor , BaggingRegressor , RandomForestClassifier ,RandomForestRegressor, GradientBoostingClassifier\nfrom sklearn.tree import DecisionTreeClassifier , DecisionTreeRegressor\nfrom sklearn.naive_bayes import  GaussianNB \nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.svm import LinearSVC\nfrom sklearn.linear_model import LinearRegression \nfrom sklearn.metrics import roc_auc_score \nfrom sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:13:38.538171Z","iopub.execute_input":"2023-11-02T10:13:38.538635Z","iopub.status.idle":"2023-11-02T10:13:38.547159Z","shell.execute_reply.started":"2023-11-02T10:13:38.5386Z","shell.execute_reply":"2023-11-02T10:13:38.545591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 特征工程和建模\nmodels = {\n    'Decision Tree': DecisionTreeClassifier(),\n    'Random Forest': RandomForestClassifier(random_state = random_state),\n    'SVM': LinearSVC(),\n    'Logistic Regression': LogisticRegression( random_state = random_state ),\n    'knn' : KNeighborsClassifier()\n}\n\nresults = {}\n\nfor model_name, model in models.items():\n    if model_name == 'Decision Tree' or model_name == 'Random Forest':\n        model.fit(x_train_DT, y_train)\n        y_pred = model.predict(x_val_DT)\n    else:\n        model.fit(x_train_scaled, y_train)\n        y_pred = model.predict(x_val_scaled)\n    accuracy = accuracy_score(y_val, y_pred)\n    precision = precision_score(y_val, y_pred, average='weighted')\n    recall = recall_score(y_val, y_pred, average='weighted')\n    results[model_name] = {'Accuracy': accuracy, 'Precision': precision, 'Recall': recall}\n    \n    print(f'{model_name}:')\n    print(f'Accuracy: {accuracy:.4f}')\n    print(f'Precision: {precision:.4f}')\n    print(f'Recall: {recall:.4f}')\n    print('-------------------------')    \n ","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:14:01.322494Z","iopub.execute_input":"2023-11-02T10:14:01.322952Z","iopub.status.idle":"2023-11-02T10:21:20.444434Z","shell.execute_reply.started":"2023-11-02T10:14:01.322917Z","shell.execute_reply":"2023-11-02T10:21:20.442947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 利用GridSearchCV寻找超参数","metadata":{}},{"cell_type":"code","source":"def gridsearch(classifier , param_grid , flag = True):\n\n    grid_search = GridSearchCV(classifier, param_grid, cv=5)\n    \n    if flag:\n        grid_search.fit(x_train_DT, y_train)\n        y_pred = grid_search.best_estimator_.predict(x_val_DT)\n    else:\n        grid_search.fit(x_train_scaled, y_train)\n        y_pred = grid_search.best_estimator_.predict(x_val_scaled)\n\n    # 打印最佳参数和最佳得分\n    print(\"Best Parameters: \", grid_search.best_params_)\n    print(\"Best Score: \", grid_search.best_score_)\n\n    \n    accuracy = accuracy_score(y_val, y_pred)\n    precision = precision_score(y_val, y_pred, average='weighted')\n    recall = recall_score(y_val, y_pred, average='weighted')\n    print(f'Accuracy: {accuracy:.4f}')\n    print(f'Precision: {precision:.4f}')\n    print(f'Recall: {recall:.4f}')\n\n\nclassifier = DecisionTreeClassifier()\n\nparam_grid = {\n    'max_depth': [3, 5, 7, 9 ],\n    'min_samples_split': [2, 5, 10],\n    'min_samples_leaf': [1, 2, 3]\n                }\ngridsearch(classifier , param_grid)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:49.467486Z","iopub.status.idle":"2023-11-02T10:12:49.468129Z","shell.execute_reply.started":"2023-11-02T10:12:49.46791Z","shell.execute_reply":"2023-11-02T10:12:49.467932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gridsearch(classifier , param_grid , flag = True):\n\n    grid_search = GridSearchCV(classifier, param_grid, cv=5)\n    \n    if flag:\n        grid_search.fit(x_train_DT, y_train)\n        y_pred = grid_search.best_estimator_.predict(x_val_DT)\n    else:\n        grid_search.fit(x_train_scaled, y_train)\n        y_pred = grid_search.best_estimator_.predict(x_val_scaled)\n\n    # 打印最佳参数和最佳得分\n    print(\"Best Parameters: \", grid_search.best_params_)\n    print(\"Best Score: \", grid_search.best_score_)\n\n    \n    accuracy = accuracy_score(y_val, y_pred)\n    precision = precision_score(y_val, y_pred, average='weighted')\n    recall = recall_score(y_val, y_pred, average='weighted')\n    print(f'Accuracy: {accuracy:.4f}')\n    print(f'Precision: {precision:.4f}')\n    print(f'Recall: {recall:.4f}')\n    \nclassifier = RandomForestClassifier(random_state = random_state)\n\nparam_grid = {\n    'n_estimators': [500, 1000, 2000],\n    'criterion': ['gini', 'entropy'],\n    'max_depth': [3, 5, 7, 9 ,11]\n                }\ngridsearch(classifier , param_grid)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:12:49.469315Z","iopub.status.idle":"2023-11-02T10:12:49.469922Z","shell.execute_reply.started":"2023-11-02T10:12:49.46972Z","shell.execute_reply":"2023-11-02T10:12:49.469741Z"},"trusted":true},"execution_count":null,"outputs":[]}]}