{ "cells": [ { "cell_type": "code", "execution_count": 35, "id": "92dfb1a6", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.svm import SVC\n", "from sklearn.model_selection import GridSearchCV,RandomizedSearchCV\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.neighbors import KNeighborsClassifier" ] }, { "cell_type": "code", "execution_count": 36, "id": "b9c7965e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 5.1 3.5 1.4 0.2 setosa\n", "1 4.9 3.0 1.4 0.2 setosa\n", "2 4.7 3.2 1.3 0.2 setosa\n", "3 4.6 3.1 1.5 0.2 setosa\n", "4 5.0 3.6 1.4 0.2 setosa" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"Iris.csv\")\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 37, "id": "4b11a56a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "species\n", "setosa 50\n", "versicolor 50\n", "virginica 50\n", "Name: count, dtype: int64" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"species\"].value_counts()" ] }, { "cell_type": "code", "execution_count": 38, "id": "02c74504", "metadata": {}, "outputs": [], "source": [ "X = df.drop(\"species\",axis=1)\n", "y = df[\"species\"]" ] }, { "cell_type": "code", "execution_count": 39, "id": "8dcaf367", "metadata": {}, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)" ] }, { "cell_type": "code", "execution_count": 40, "id": "7ed57a02", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
SVC(gamma='auto')
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.
" ], "text/plain": [ "SVC(gamma='auto')" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "SVC_model = SVC(gamma=\"auto\")\n", "SVC_model.fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 41, "id": "c0a3917c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
GridSearchCV(cv=5, estimator=SVC(gamma='auto'),\n",
       "             param_grid={'C': [1, 10, 20, 30], 'kernel': ['rbf', 'linear']})
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.
" ], "text/plain": [ "GridSearchCV(cv=5, estimator=SVC(gamma='auto'),\n", " param_grid={'C': [1, 10, 20, 30], 'kernel': ['rbf', 'linear']})" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Grid_SVC_model = GridSearchCV((SVC_model),{\n", " \"C\":[1,10,20,30],\n", " \"kernel\" : [\"rbf\",\"linear\"]\n", "},cv=5,return_train_score=False)\n", "Grid_SVC_model.fit(X,y)" ] }, { "cell_type": "code", "execution_count": 42, "id": "1876fc53", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_fit_timestd_fit_timemean_score_timestd_score_timeparam_Cparam_kernelparamssplit0_test_scoresplit1_test_scoresplit2_test_scoresplit3_test_scoresplit4_test_scoremean_test_scorestd_test_scorerank_test_score
00.0034440.0004160.0027060.0004171rbf{'C': 1, 'kernel': 'rbf'}0.9666671.00.9666670.9666671.00.9800000.0163301
10.0035130.0006970.0031930.0005771linear{'C': 1, 'kernel': 'linear'}0.9666671.00.9666670.9666671.00.9800000.0163301
20.0033070.0003900.0107620.01619410rbf{'C': 10, 'kernel': 'rbf'}0.9666671.00.9666670.9666671.00.9800000.0163301
30.0037970.0011910.0105230.01313410linear{'C': 10, 'kernel': 'linear'}1.0000001.00.9000000.9666671.00.9733330.0388734
40.0050790.0030610.0021180.00009720rbf{'C': 20, 'kernel': 'rbf'}0.9666671.00.9000000.9666671.00.9666670.0365155
50.0065310.0078970.0026490.00054420linear{'C': 20, 'kernel': 'linear'}1.0000001.00.9000000.9333331.00.9666670.0421646
60.0024420.0001280.0020340.00040630rbf{'C': 30, 'kernel': 'rbf'}0.9666671.00.9000000.9333331.00.9600000.0388737
70.0024920.0004210.0019250.00040230linear{'C': 30, 'kernel': 'linear'}1.0000001.00.9000000.9000001.00.9600000.0489907
\n", "
" ], "text/plain": [ " mean_fit_time std_fit_time mean_score_time std_score_time param_C \\\n", "0 0.003444 0.000416 0.002706 0.000417 1 \n", "1 0.003513 0.000697 0.003193 0.000577 1 \n", "2 0.003307 0.000390 0.010762 0.016194 10 \n", "3 0.003797 0.001191 0.010523 0.013134 10 \n", "4 0.005079 0.003061 0.002118 0.000097 20 \n", "5 0.006531 0.007897 0.002649 0.000544 20 \n", "6 0.002442 0.000128 0.002034 0.000406 30 \n", "7 0.002492 0.000421 0.001925 0.000402 30 \n", "\n", " param_kernel params split0_test_score \\\n", "0 rbf {'C': 1, 'kernel': 'rbf'} 0.966667 \n", "1 linear {'C': 1, 'kernel': 'linear'} 0.966667 \n", "2 rbf {'C': 10, 'kernel': 'rbf'} 0.966667 \n", "3 linear {'C': 10, 'kernel': 'linear'} 1.000000 \n", "4 rbf {'C': 20, 'kernel': 'rbf'} 0.966667 \n", "5 linear {'C': 20, 'kernel': 'linear'} 1.000000 \n", "6 rbf {'C': 30, 'kernel': 'rbf'} 0.966667 \n", "7 linear {'C': 30, 'kernel': 'linear'} 1.000000 \n", "\n", " split1_test_score split2_test_score split3_test_score split4_test_score \\\n", "0 1.0 0.966667 0.966667 1.0 \n", "1 1.0 0.966667 0.966667 1.0 \n", "2 1.0 0.966667 0.966667 1.0 \n", "3 1.0 0.900000 0.966667 1.0 \n", "4 1.0 0.900000 0.966667 1.0 \n", "5 1.0 0.900000 0.933333 1.0 \n", "6 1.0 0.900000 0.933333 1.0 \n", "7 1.0 0.900000 0.900000 1.0 \n", "\n", " mean_test_score std_test_score rank_test_score \n", "0 0.980000 0.016330 1 \n", "1 0.980000 0.016330 1 \n", "2 0.980000 0.016330 1 \n", "3 0.973333 0.038873 4 \n", "4 0.966667 0.036515 5 \n", "5 0.966667 0.042164 6 \n", "6 0.960000 0.038873 7 \n", "7 0.960000 0.048990 7 " ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results = pd.DataFrame(Grid_SVC_model.cv_results_)\n", "results" ] }, { "cell_type": "code", "execution_count": 43, "id": "0e653afb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['mean_fit_time', 'std_fit_time', 'mean_score_time', 'std_score_time',\n", " 'param_C', 'param_kernel', 'params', 'split0_test_score',\n", " 'split1_test_score', 'split2_test_score', 'split3_test_score',\n", " 'split4_test_score', 'mean_test_score', 'std_test_score',\n", " 'rank_test_score'],\n", " dtype='object')" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results.columns" ] }, { "cell_type": "code", "execution_count": 44, "id": "1fd35075", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
param_Cparam_kernelparamsmean_test_score
01rbf{'C': 1, 'kernel': 'rbf'}0.980000
11linear{'C': 1, 'kernel': 'linear'}0.980000
210rbf{'C': 10, 'kernel': 'rbf'}0.980000
310linear{'C': 10, 'kernel': 'linear'}0.973333
420rbf{'C': 20, 'kernel': 'rbf'}0.966667
520linear{'C': 20, 'kernel': 'linear'}0.966667
630rbf{'C': 30, 'kernel': 'rbf'}0.960000
730linear{'C': 30, 'kernel': 'linear'}0.960000
\n", "
" ], "text/plain": [ " param_C param_kernel params mean_test_score\n", "0 1 rbf {'C': 1, 'kernel': 'rbf'} 0.980000\n", "1 1 linear {'C': 1, 'kernel': 'linear'} 0.980000\n", "2 10 rbf {'C': 10, 'kernel': 'rbf'} 0.980000\n", "3 10 linear {'C': 10, 'kernel': 'linear'} 0.973333\n", "4 20 rbf {'C': 20, 'kernel': 'rbf'} 0.966667\n", "5 20 linear {'C': 20, 'kernel': 'linear'} 0.966667\n", "6 30 rbf {'C': 30, 'kernel': 'rbf'} 0.960000\n", "7 30 linear {'C': 30, 'kernel': 'linear'} 0.960000" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results[['param_C', 'param_kernel', 'params','mean_test_score']]" ] }, { "cell_type": "code", "execution_count": 45, "id": "ec70847f", "metadata": {}, "outputs": [ { "data": { "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.
" ], "text/plain": [ "KNeighborsClassifier()" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "KNN_model = KNeighborsClassifier(algorithm=\"auto\")\n", "KNN_model.fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 46, "id": "c4e3ab3c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
RandomizedSearchCV(cv=5, estimator=KNeighborsClassifier(), n_iter=4,\n",
       "                   param_distributions={'n_neighbors': [1, 10, 20, 30],\n",
       "                                        'weights': ['uniform', 'distance']})
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.
" ], "text/plain": [ "RandomizedSearchCV(cv=5, estimator=KNeighborsClassifier(), n_iter=4,\n", " param_distributions={'n_neighbors': [1, 10, 20, 30],\n", " 'weights': ['uniform', 'distance']})" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Random_KNN_model = RandomizedSearchCV((KNN_model),{\n", " \"n_neighbors\":[1,10,20,30],\n", " \"weights\":['uniform', 'distance'] \n", "},cv=5,n_iter=4,return_train_score=False)\n", "Random_KNN_model.fit(X,y)" ] }, { "cell_type": "code", "execution_count": 47, "id": "d602bdc0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mean_fit_timestd_fit_timemean_score_timestd_score_timeparam_weightsparam_n_neighborsparamssplit0_test_scoresplit1_test_scoresplit2_test_scoresplit3_test_scoresplit4_test_scoremean_test_scorestd_test_scorerank_test_score
00.0024900.0002140.0037540.000479uniform1{'weights': 'uniform', 'n_neighbors': 1}0.9666670.9666670.9333330.9333331.00.9600000.0249442
10.0021550.0001500.0027030.000403distance30{'weights': 'distance', 'n_neighbors': 30}0.9666670.9666670.9333330.9333331.00.9600000.0249442
20.0022570.0003120.0028020.000605distance10{'weights': 'distance', 'n_neighbors': 10}0.9666671.0000001.0000000.9666671.00.9866670.0163301
30.0022560.0002810.0032220.000916distance1{'weights': 'distance', 'n_neighbors': 1}0.9666670.9666670.9333330.9333331.00.9600000.0249442
\n", "
" ], "text/plain": [ " mean_fit_time std_fit_time mean_score_time std_score_time param_weights \\\n", "0 0.002490 0.000214 0.003754 0.000479 uniform \n", "1 0.002155 0.000150 0.002703 0.000403 distance \n", "2 0.002257 0.000312 0.002802 0.000605 distance \n", "3 0.002256 0.000281 0.003222 0.000916 distance \n", "\n", " param_n_neighbors params \\\n", "0 1 {'weights': 'uniform', 'n_neighbors': 1} \n", "1 30 {'weights': 'distance', 'n_neighbors': 30} \n", "2 10 {'weights': 'distance', 'n_neighbors': 10} \n", "3 1 {'weights': 'distance', 'n_neighbors': 1} \n", "\n", " split0_test_score split1_test_score split2_test_score split3_test_score \\\n", "0 0.966667 0.966667 0.933333 0.933333 \n", "1 0.966667 0.966667 0.933333 0.933333 \n", "2 0.966667 1.000000 1.000000 0.966667 \n", "3 0.966667 0.966667 0.933333 0.933333 \n", "\n", " split4_test_score mean_test_score std_test_score rank_test_score \n", "0 1.0 0.960000 0.024944 2 \n", "1 1.0 0.960000 0.024944 2 \n", "2 1.0 0.986667 0.016330 1 \n", "3 1.0 0.960000 0.024944 2 " ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results_ = pd.DataFrame(Random_KNN_model.cv_results_)\n", "results_" ] }, { "cell_type": "code", "execution_count": 48, "id": "12fe8314", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
param_n_neighborsparam_weightsparamsmean_test_score
01uniform{'weights': 'uniform', 'n_neighbors': 1}0.960000
130distance{'weights': 'distance', 'n_neighbors': 30}0.960000
210distance{'weights': 'distance', 'n_neighbors': 10}0.986667
31distance{'weights': 'distance', 'n_neighbors': 1}0.960000
\n", "
" ], "text/plain": [ " param_n_neighbors param_weights \\\n", "0 1 uniform \n", "1 30 distance \n", "2 10 distance \n", "3 1 distance \n", "\n", " params mean_test_score \n", "0 {'weights': 'uniform', 'n_neighbors': 1} 0.960000 \n", "1 {'weights': 'distance', 'n_neighbors': 30} 0.960000 \n", "2 {'weights': 'distance', 'n_neighbors': 10} 0.986667 \n", "3 {'weights': 'distance', 'n_neighbors': 1} 0.960000 " ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results_[['param_n_neighbors', 'param_weights', 'params','mean_test_score']]" ] } ], "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.14.2" } }, "nbformat": 4, "nbformat_minor": 5 }