This commit is contained in:
13653820592 2026-01-05 15:09:26 +08:00
parent e15effc5b5
commit 2cc906fe3f
1 changed files with 212 additions and 0 deletions

View File

@ -0,0 +1,212 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5893b1c0-fa37-40b0-bf86-5e542987634e",
"metadata": {},
"source": [
"# 1 获取数据"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e5e89d3b-2384-4bbf-9197-2dbbca120838",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, ..., 8, 9, 8])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.datasets import load_digits\n",
"digits = load_digits() # 加载数据\n",
"data = digits[\"data\"] # 属性列\n",
"target = digits[\"target\"] # 标签列\n",
"target"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "950a6379-974e-4e74-82b2-f58a8606682a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "8fac6553-56ad-4096-834a-55bd9f6cdab5",
"metadata": {},
"source": [
"# 2 划分数据集"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "86506fed-8cb6-4e54-88f4-a70150297947",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"x_train,x_test,y_train,y_test=train_test_split(data, target, test_size=0.2, random_state=123)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3dd4bc37-cf06-41f2-a16b-b4851e497c8d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "e021dded-b3cb-4c04-bab2-e18f5374c037",
"metadata": {},
"source": [
"# 3 模型构建"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "525e6d7e-0140-4443-818f-71ce37baee79",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.neighbors import KNeighborsClassifier\n",
"model= KNeighborsClassifier(n_neighbors=5).fit(x_train,y_train) # 模型构建\n",
"y_pred=model.predict(x_test) #预测\n"
]
},
{
"cell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, 4, 6, 6, 4, 1, 5, 3, 0, 1, 6, 0, 2, 5, 5, 8, 8,\n",
" 2, 6, 0, 1, 1, 5, 1, 6, 9, 2, 8, 2, 4, 9, 8, 3, 3, 3, 2, 4, 0, 9,\n",
" 4, 3, 6, 0, 2, 1, 3, 3, 2, 5, 9, 0, 7, 5, 9, 7, 6, 4, 7, 9, 6, 7,\n",
" 4, 1, 5, 9, 6, 5, 5, 0])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_pred"
]
},
{
"cell_type": "markdown",
"id": "6469074a-0502-43ae-a076-16d3f744ce22",
"metadata": {},
"source": [
"# 4 模型评价"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "efd03e71-1992-4a5a-871f-2d544230a17d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Confusion Matrix:\n",
"[[39 0 0 0 0 0 0 0 0 0]\n",
" [ 0 34 0 0 0 0 0 0 0 0]\n",
" [ 0 0 36 0 0 0 0 0 0 0]\n",
" [ 0 0 0 33 0 0 0 0 0 0]\n",
" [ 0 0 0 0 41 0 0 1 0 0]\n",
" [ 0 0 0 0 0 36 0 0 0 1]\n",
" [ 0 0 0 0 0 0 43 0 0 0]\n",
" [ 0 0 0 0 0 0 0 31 0 0]\n",
" [ 0 3 1 0 0 0 0 0 33 0]\n",
" [ 0 0 0 1 0 0 0 0 0 27]]\n",
"k邻近模型预测的正确率为 0.9805555555555555\n"
]
}
],
"source": [
"from sklearn.metrics import confusion_matrix, accuracy_score \n",
" \n",
"\n",
"# 计算混淆矩阵 \n",
"cm = confusion_matrix(y_test, y_pred)\n",
"print(\"Confusion Matrix:\")\n",
"print(cm)\n",
"\n",
"\n",
"print(\"k邻近模型预测的正确率为\",accuracy_score(y_test,y_pred))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37b7d2de-80da-4c41-99af-0aae3cc5d4ce",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3609a180-6bc6-4b76-8269-7d620d45c2ce",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}