{ "metadata": { "kernelspec": { "name": "python", "display_name": "Python (Pyodide)", "language": "python" }, "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8" } }, "nbformat_minor": 5, "nbformat": 4, "cells": [ { "id": "3034a8f4-bf94-4105-9e51-70be64145d33", "cell_type": "code", "source": "import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score", "metadata": { "trusted": true }, "outputs": [], "execution_count": 1 }, { "id": "f3643f05-2a1f-4083-bb1e-156a9fc4a116", "cell_type": "code", "source": "data = pd.read_csv(\"diabetes.csv\")\nprint(data.head())", "metadata": { "trusted": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": " Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \\\n0 6 148 72 35 0 33.6 \n1 1 85 66 29 0 26.6 \n2 8 183 64 0 0 23.3 \n3 1 89 66 23 94 28.1 \n4 0 137 40 35 168 43.1 \n\n Pedigree Age Outcome \n0 0.627 50 1 \n1 0.351 31 0 \n2 0.672 32 1 \n3 0.167 21 0 \n4 2.288 33 1 \n" } ], "execution_count": 3 }, { "id": "d4982e5c-0006-41ea-bc8d-b652368156cf", "cell_type": "code", "source": "X = data.drop(columns=['Outcome'])\ny = data['Outcome']", "metadata": { "trusted": true }, "outputs": [], "execution_count": 4 }, { "id": "f0e7b2f7-f0bb-4d67-b98e-3695617e1d65", "cell_type": "code", "source": "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)", "metadata": { "trusted": true }, "outputs": [], "execution_count": 5 }, { "id": "1ea952ce-b927-4b39-ba82-ec2a95adfc1d", "cell_type": "code", "source": "scaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)", "metadata": { "trusted": true }, "outputs": [], "execution_count": 6 }, { "id": "f321cb2f-6633-45c1-a170-bd31e792d354", "cell_type": "code", "source": "knn = KNeighborsClassifier(n_neighbors=5) # K=5\nknn.fit(X_train, y_train)", "metadata": { "trusted": true }, "outputs": [ { "execution_count": 7, "output_type": "execute_result", "data": { "text/plain": "KNeighborsClassifier()", "text/html": "
KNeighborsClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" }, "metadata": {} } ], "execution_count": 7 }, { "id": "d689a957-2a8f-4925-8466-1b10b197048c", "cell_type": "code", "source": "", "metadata": { "trusted": true }, "outputs": [], "execution_count": null } ] }