Skip to content

Instantly share code, notes, and snippets.

@QuantumDamage
Created April 7, 2019 18:22
Show Gist options
  • Save QuantumDamage/05314db6697444b02c7a9659f7733844 to your computer and use it in GitHub Desktop.
Save QuantumDamage/05314db6697444b02c7a9659f7733844 to your computer and use it in GitHub Desktop.
K najbliższych sąsiadów – kto z kim przestaje, takim się staje
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Kod 1\n \nimport pandas as pd\nfrom sklearn.datasets import load_breast_cancer\n \nfrom sklearn.neighbors import KNeighborsClassifier\n \nfrom sklearn.model_selection import train_test_split\n \nbreast_cancer = load_breast_cancer()\nX = pd.DataFrame(breast_cancer[\"data\"], \n columns = breast_cancer[\"feature_names\"])\ny = pd.Series(breast_cancer[\"target\"])\n \nX_train, X_test, y_train, y_test = train_test_split(X, y, \n test_size = 0.25, \n random_state = 42)",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Kod 2\n\nknn = KNeighborsClassifier()\nknn.fit(X = X_train, y = y_train)\n\ntrain_score = knn.score(X = X_train, y = y_train)\ntest_score = knn.score(X = X_test, y = y_test)\n\ntrain_score, test_score",
"execution_count": 2,
"outputs": [
{
"data": {
"text/plain": "(0.9342723004694836, 0.965034965034965)"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "%%time\n# Kod 3\n\n\nfrom sklearn.model_selection import GridSearchCV\n\nparam_grid = {'n_neighbors': range(1, 101),\n 'weights': [\"uniform\", \"distance\"],\n 'p': [1, 2]\n }\n\nknn_grid_search = GridSearchCV(estimator = KNeighborsClassifier(), param_grid = param_grid, cv = 5, iid = False)\n\nknn_grid_search.fit(X = X_train, y = y_train)\n\ngrid_train_score = knn_grid_search.score(X = X_train, y = y_train)\ngrid_test_score = knn_grid_search.score(X = X_test, y = y_test)\n\nprint(grid_train_score, grid_test_score)\nprint(knn_grid_search.best_params_)",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "0.9436619718309859 0.972027972027972\n{'n_neighbors': 6, 'p': 1, 'weights': 'uniform'}\nCPU times: user 1min 8s, sys: 164 ms, total: 1min 9s\nWall time: 1min 9s\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"threshold": 4,
"number_sections": true,
"toc_cell": false,
"toc_window_display": false,
"toc_section_display": "block",
"sideBar": true,
"navigate_menu": true,
"moveMenuLeft": true,
"widenNotebook": false,
"colors": {
"hover_highlight": "#DAA520",
"selected_highlight": "#FFD700",
"running_highlight": "#FF0000",
"wrapper_background": "#FFFFFF",
"sidebar_border": "#EEEEEE",
"navigate_text": "#333333",
"navigate_num": "#000000"
},
"nav_menu": {
"height": "12px",
"width": "252px"
}
},
"gist": {
"id": "",
"data": {
"description": "K najbliższych sąsiadów – kto z kim przestaje, takim się staje",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment