-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
上传了人工智能导论三项作业决策树,svm;knn,以及pytorch图像分类
- Loading branch information
1 parent
83081a2
commit da1b273
Showing
50 changed files
with
3,988 additions
and
5 deletions.
There are no files selected for viewing
Binary file not shown.
195 changes: 195 additions & 0 deletions
195
学习/人工智能导论/作业1-KNN/.ipynb_checkpoints/Untitled-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import random\n", | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"from past.builtins import xrange\n", | ||
"%matplotlib inline\n", | ||
"plt.rcParams['figure.figsize'] = (15., 12.) # 设置默认大小\n", | ||
"plt.rcParams['image.interpolation'] = 'nearest'\n", | ||
"plt.rcParams['image.cmap'] = 'gray'\n", | ||
"\n", | ||
"%load_ext autoreload\n", | ||
"%autoreload 2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(5000, 32, 32, 3)\n", | ||
"(500, 32, 32, 3)\n", | ||
"(500, 32, 32, 3)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 读取提供的cifar10-mini数据集,\n", | ||
"data = np.load('cifar10-mini.npz')\n", | ||
"\n", | ||
"X_train= data['X_train']\n", | ||
"X_val= data['X_val']\n", | ||
"X_test= data['X_test']\n", | ||
"y_train= data['y_train']\n", | ||
"y_val= data['y_val']\n", | ||
"y_test= data['y_test']\n", | ||
"\n", | ||
"# 打印数据shape\n", | ||
"print(X_train.shape)\n", | ||
"print(X_val.shape)\n", | ||
"print(X_test.shape)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from features import *\n", | ||
"\n", | ||
"################################################################################\n", | ||
"# TODO: #\n", | ||
"# 你需要使用 hog_feature, color_histogram_hsv 两个函数完成特征的提取 #\n", | ||
"# 你可以在 features.py 中查看这两个函数的代码 #\n", | ||
"################################################################################\n", | ||
"\"\"\"你的代码\"\"\"\n", | ||
"X_train_feats = extract_features(X_train,[hog_feature,color_histogram_hsv])\n", | ||
"X_val_feats = extract_features(X_val,[hog_feature,color_histogram_hsv])\n", | ||
"X_test_feats = extract_features(X_test,[hog_feature,color_histogram_hsv])\n", | ||
"\n", | ||
"################################################################################\n", | ||
"# END OF YOUR CODE #\n", | ||
"################################################################################\n", | ||
"\n", | ||
" \n", | ||
"# 预处理: 减去均值\n", | ||
"mean_feat = np.mean(X_train_feats, axis=0, keepdims=True)\n", | ||
"X_train_feats -= mean_feat\n", | ||
"\n", | ||
"mean_feat = np.mean(X_val_feats, axis=0, keepdims=True)\n", | ||
"X_val_feats -= mean_feat\n", | ||
"\n", | ||
"mean_feat = np.mean(X_test_feats, axis=0, keepdims=True)\n", | ||
"X_test_feats -= mean_feat\n", | ||
"\n", | ||
"# 预处理: 除以标准差,这能保证所有的值在 0~1 之间\n", | ||
"std_feat = np.std(X_train_feats, axis=0, keepdims=True)\n", | ||
"X_train_feats /= std_feat\n", | ||
"\n", | ||
"std_feat = np.std(X_val_feats, axis=0, keepdims=True)\n", | ||
"X_val_feats /= std_feat\n", | ||
"\n", | ||
"std_feat = np.std(X_test_feats, axis=0, keepdims=True)\n", | ||
"X_test_feats /= std_feat\n", | ||
"\n", | ||
"# 预处理: 增加一个偏置值,在 K-NN 中,该步操作并无必要,但增加偏置值对其他分类器如 SVM 等有帮助。\n", | ||
"X_train_feats = np.hstack([X_train_feats, np.ones((X_train_feats.shape[0], 1))])\n", | ||
"X_val_feats = np.hstack([X_val_feats, np.ones((X_val_feats.shape[0], 1))])\n", | ||
"X_test_feats = np.hstack([X_test_feats, np.ones((X_test_feats.shape[0], 1))])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"((500, 155), (500, 155), (500, 155))" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"X_test_feats.shape , X_val_feats.shape , X_test_feats.shape" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sklearn.neighbors import KNeighborsClassifier\n", | ||
"from sklearn.metrics import accuracy_score" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"KNN准确率: 0.3180\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"knn = KNeighborsClassifier() \n", | ||
"knn.fit(X_train_feats , y_train ) \n", | ||
"predict_y = knn.predict(X_test_feats) \n", | ||
"print(\"KNN准确率: %.4lf\" % accuracy_score(y_test, predict_y))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.3" | ||
}, | ||
"toc": { | ||
"base_numbering": 1, | ||
"nav_menu": {}, | ||
"number_sections": true, | ||
"sideBar": true, | ||
"skip_h1_title": false, | ||
"title_cell": "Table of Contents", | ||
"title_sidebar": "Contents", | ||
"toc_cell": false, | ||
"toc_position": {}, | ||
"toc_section_display": true, | ||
"toc_window_display": false | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.