Files
Your Name 8ac470b565 notebooks
2021-02-05 20:44:51 +00:00

185 lines
3.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "8AgQ_HS9i40B"
},
"outputs": [],
"source": [
"from sklearn import neighbors, datasets, model_selection"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "TluW5qEojQHo"
},
"outputs": [],
"source": [
"# dataset\n",
"cancer = datasets.load_breast_cancer()\n",
"\n",
"# target\n",
"y = cancer.target\n",
"\n",
"# features\n",
"X = cancer.data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "NBmvMbQ1j6zQ"
},
"outputs": [],
"source": [
"# no arguments specified\n",
"knn = neighbors.KNeighborsClassifier()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
},
"colab_type": "code",
"id": "tWS3Dqfgj30k",
"outputId": "fcf0e80d-25d0-490a-ead1-2264528a3bc9"
},
"outputs": [],
"source": [
"# 10 folds, scored on precision\n",
"cv = model_selection.cross_val_score(knn, X, y, cv=10, scoring='precision')\n",
"\n",
"# precision scores\n",
"print(cv)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "w_F9k2hdj_8V",
"outputId": "d51240c4-c8a2-498c-86d5-3681adbc81a1"
},
"outputs": [],
"source": [
"# average over all folds\n",
"print(round(cv.mean(), 2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "mM_XnnNQkbrx",
"outputId": "5dfaee9d-958c-4854-c270-47841c9853a6"
},
"outputs": [],
"source": [
"# k = 15\n",
"knn = neighbors.KNeighborsClassifier(n_neighbors=15)\n",
"\n",
"cv = model_selection.cross_val_score(knn, X, y, cv=10, scoring='precision')\n",
"\n",
"print(round(cv.mean(), 2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 70
},
"colab_type": "code",
"id": "VSpsCUkYog-B",
"outputId": "ae3ac9bf-83e9-4719-c76a-606f868abf5b"
},
"outputs": [],
"source": [
"def evaluate_knn(k):\n",
" knn = neighbors.KNeighborsClassifier(n_neighbors=k)\n",
" cv = model_selection.cross_val_score(knn, X, y, cv=10, scoring='precision')\n",
" print(round(cv.mean(), 2))\n",
"\n",
"evaluate_knn(k=7)\n",
"evaluate_knn(k=3)\n",
"evaluate_knn(k=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"colab_type": "code",
"id": "oTu-nTHPsRfH",
"outputId": "9f72c262-5ad1-497c-ab56-2db620f12669"
},
"outputs": [],
"source": [
"# k =5, weights evaluated using distance\n",
"knn = neighbors.KNeighborsClassifier(n_neighbors=5, weights='distance')\n",
"\n",
"cv = model_selection.cross_val_score(knn, X, y, cv=10, scoring='precision')\n",
"\n",
"print(round(cv.mean(), 2))"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Exercise_8_1.ipynb",
"provenance": []
},
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}