-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
417 additions
and
0 deletions.
There are no files selected for viewing
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,238 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import pandas as pd\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import seaborn as sns\n", | ||
"%matplotlib inline\n", | ||
"\n", | ||
"from sklearn.preprocessing import StandardScaler\n", | ||
"from sklearn.model_selection import train_test_split\n", | ||
"from sklearn.model_selection import cross_val_score\n", | ||
"\n", | ||
"# KNN classifier\n", | ||
"from sklearn.neighbors import KNeighborsClassifier\n", | ||
"\n", | ||
"# Evaluation Metrics\n", | ||
"from sklearn.metrics import accuracy_score\n", | ||
"from sklearn.metrics import confusion_matrix" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#Import scikit-learn dataset library\n", | ||
"from sklearn import datasets\n", | ||
"\n", | ||
"#Load dataset\n", | ||
"wine = datasets.load_wine()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"['alcohol', 'malic_acid', 'ash', 'alcalinity_of_ash', 'magnesium', 'total_phenols', 'flavanoids', 'nonflavanoid_phenols', 'proanthocyanins', 'color_intensity', 'hue', 'od280/od315_of_diluted_wines', 'proline']\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(wine.feature_names)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"['class_0' 'class_1' 'class_2']\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(wine.target_names)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(178, 13)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(wine.data.shape)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Spliting of the dataset into training set and test set\n", | ||
"X_train, X_test, y_train, y_test = train_test_split(wine.data, wine.target, test_size=0.3) # 70% training and 30% test" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#Create KNN Classifier\n", | ||
"knn = KNeighborsClassifier(n_neighbors=5)\n", | ||
"\n", | ||
"#Train the model using the training sets\n", | ||
"knn.fit(X_train, y_train)\n", | ||
"\n", | ||
"#Predict the response for test dataset\n", | ||
"y_pred = knn.predict(X_test)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Accuracy: 0.5925925925925926\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"#Import scikit-learn metrics module for accuracy calculation\n", | ||
"from sklearn import metrics\n", | ||
"# Model Accuracy, how often is the classifier correct?\n", | ||
"print(\"Accuracy:\",metrics.accuracy_score(y_test, y_pred))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#Create KNN Classifier\n", | ||
"knn = KNeighborsClassifier(n_neighbors=7)\n", | ||
"\n", | ||
"#Train the model using the training sets\n", | ||
"knn.fit(X_train, y_train)\n", | ||
"\n", | ||
"#Predict the response for test dataset\n", | ||
"y_pred = knn.predict(X_test)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Accuracy: 0.6296296296296297\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"#Import scikit-learn metrics module for accuracy calculation\n", | ||
"from sklearn import metrics\n", | ||
"# Model Accuracy, how often is the classifier correct?\n", | ||
"print(\"Accuracy:\",metrics.accuracy_score(y_test, y_pred))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#Create KNN Classifier\n", | ||
"knn = KNeighborsClassifier(n_neighbors=8)\n", | ||
"\n", | ||
"#Train the model using the training sets\n", | ||
"knn.fit(X_train, y_train)\n", | ||
"\n", | ||
"#Predict the response for test dataset\n", | ||
"y_pred = knn.predict(X_test)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Accuracy: 0.6111111111111112\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"#scikit-learn metrics module for accuracy calculation\n", | ||
"from sklearn import metrics\n", | ||
"# Model Accuracy, how often is the classifier correct?\n", | ||
"print(\"Accuracy:\",metrics.accuracy_score(y_test, y_pred))" | ||
] | ||
}, | ||
{ | ||
"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.6.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.