{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "715180ae", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "7cbd428b", "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": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"Iris.csv\")\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 3, "id": "dde58ea3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['setosa', 'versicolor', 'virginica'], dtype=object)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"species\"].unique()" ] }, { "cell_type": "code", "execution_count": 4, "id": "aa929de6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(150, 5)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shape" ] }, { "cell_type": "code", "execution_count": 5, "id": "5abb3f87", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 150 entries, 0 to 149\n", "Data columns (total 5 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 sepal_length 150 non-null float64\n", " 1 sepal_width 150 non-null float64\n", " 2 petal_length 150 non-null float64\n", " 3 petal_width 150 non-null float64\n", " 4 species 150 non-null object \n", "dtypes: float64(4), object(1)\n", "memory usage: 6.0+ KB\n" ] } ], "source": [ "df.info()" ] }, { "cell_type": "code", "execution_count": 6, "id": "d729bd8f", "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", "
sepal_lengthsepal_widthpetal_lengthpetal_width
count150.000000150.000000150.000000150.000000
mean5.8433333.0573333.7580001.199333
std0.8280660.4358661.7652980.762238
min4.3000002.0000001.0000000.100000
25%5.1000002.8000001.6000000.300000
50%5.8000003.0000004.3500001.300000
75%6.4000003.3000005.1000001.800000
max7.9000004.4000006.9000002.500000
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width\n", "count 150.000000 150.000000 150.000000 150.000000\n", "mean 5.843333 3.057333 3.758000 1.199333\n", "std 0.828066 0.435866 1.765298 0.762238\n", "min 4.300000 2.000000 1.000000 0.100000\n", "25% 5.100000 2.800000 1.600000 0.300000\n", "50% 5.800000 3.000000 4.350000 1.300000\n", "75% 6.400000 3.300000 5.100000 1.800000\n", "max 7.900000 4.400000 6.900000 2.500000" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.describe()" ] }, { "cell_type": "code", "execution_count": 7, "id": "543ec18e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "sepal_length 0\n", "sepal_width 0\n", "petal_length 0\n", "petal_width 0\n", "species 0\n", "dtype: int64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.isnull().sum()" ] }, { "cell_type": "code", "execution_count": 8, "id": "dff5150c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "species\n", "setosa 50\n", "versicolor 50\n", "virginica 50\n", "Name: count, dtype: int64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"species\"].value_counts()" ] }, { "cell_type": "code", "execution_count": 9, "id": "8cb61027", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "X = df.drop(\"species\",axis=1)\n", "y = df[\"species\"]" ] }, { "cell_type": "code", "execution_count": 10, "id": "23c57de4", "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": 11, "id": "604cd294", "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": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.neighbors import KNeighborsClassifier\n", "\n", "model_KNN = KNeighborsClassifier(algorithm=\"auto\")\n", "model_KNN.fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 12, "id": "fae36708", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.98" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model_KNN.score(X_test,y_test)" ] }, { "cell_type": "code", "execution_count": 13, "id": "eacdcf01", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['versicolor', 'setosa', 'virginica', 'versicolor', 'versicolor',\n", " 'setosa', 'versicolor', 'virginica', 'versicolor', 'versicolor',\n", " 'virginica', 'setosa', 'setosa', 'setosa', 'setosa', 'versicolor',\n", " 'virginica', 'versicolor', 'versicolor', 'virginica', 'setosa',\n", " 'virginica', 'setosa', 'virginica', 'virginica', 'virginica',\n", " 'virginica', 'virginica', 'setosa', 'setosa', 'setosa', 'setosa',\n", " 'versicolor', 'setosa', 'setosa', 'virginica', 'versicolor',\n", " 'setosa', 'setosa', 'setosa', 'virginica', 'versicolor',\n", " 'versicolor', 'setosa', 'setosa', 'versicolor', 'virginica',\n", " 'virginica', 'versicolor', 'virginica'], dtype=object)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.svm import SVC\n", "model_SVC = SVC(gamma=\"auto\")\n", "model_SVC.fit(X_train,y_train)\n", "model_SVC.predict(X_test)" ] }, { "cell_type": "code", "execution_count": 14, "id": "5b94d35b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model_SVC.score(X_test,y_test)" ] }, { "cell_type": "code", "execution_count": 15, "id": "b80530b8", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import GridSearchCV\n", "classifier = GridSearchCV((model_SVC),{\n", " \"C\" : [1,10,20,30],\n", " \"kernel\" : [\"linear\",\"rbf\"]\n", "},cv=5,return_train_score=False)" ] }, { "cell_type": "code", "execution_count": 16, "id": "1d3997e2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
GridSearchCV(cv=5, estimator=SVC(gamma='auto'),\n",
       "             param_grid={'C': [1, 10, 20, 30], 'kernel': ['linear', 'rbf']})
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': ['linear', 'rbf']})" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classifier.fit(X,y)" ] }, { "cell_type": "code", "execution_count": 17, "id": "7fa770b9", "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.0037860.0007000.0026620.0004111linear{'C': 1, 'kernel': 'linear'}0.9666671.00.9666670.9666671.00.9800000.0163301
10.0043530.0017440.0030220.0006781rbf{'C': 1, 'kernel': 'rbf'}0.9666671.00.9666670.9666671.00.9800000.0163301
20.0039370.0017130.0025010.00084310linear{'C': 10, 'kernel': 'linear'}1.0000001.00.9000000.9666671.00.9733330.0388734
30.0031470.0004630.0020880.00031510rbf{'C': 10, 'kernel': 'rbf'}0.9666671.00.9666670.9666671.00.9800000.0163301
40.0027630.0006040.0020180.00047120linear{'C': 20, 'kernel': 'linear'}1.0000001.00.9000000.9333331.00.9666670.0421646
50.0024550.0003020.0019460.00025320rbf{'C': 20, 'kernel': 'rbf'}0.9666671.00.9000000.9666671.00.9666670.0365155
60.0025950.0007230.0019350.00045230linear{'C': 30, 'kernel': 'linear'}1.0000001.00.9000000.9000001.00.9600000.0489907
70.0023630.0003760.0017170.00009930rbf{'C': 30, 'kernel': 'rbf'}0.9666671.00.9000000.9333331.00.9600000.0388737
\n", "
" ], "text/plain": [ " mean_fit_time std_fit_time mean_score_time std_score_time param_C \\\n", "0 0.003786 0.000700 0.002662 0.000411 1 \n", "1 0.004353 0.001744 0.003022 0.000678 1 \n", "2 0.003937 0.001713 0.002501 0.000843 10 \n", "3 0.003147 0.000463 0.002088 0.000315 10 \n", "4 0.002763 0.000604 0.002018 0.000471 20 \n", "5 0.002455 0.000302 0.001946 0.000253 20 \n", "6 0.002595 0.000723 0.001935 0.000452 30 \n", "7 0.002363 0.000376 0.001717 0.000099 30 \n", "\n", " param_kernel params split0_test_score \\\n", "0 linear {'C': 1, 'kernel': 'linear'} 0.966667 \n", "1 rbf {'C': 1, 'kernel': 'rbf'} 0.966667 \n", "2 linear {'C': 10, 'kernel': 'linear'} 1.000000 \n", "3 rbf {'C': 10, 'kernel': 'rbf'} 0.966667 \n", "4 linear {'C': 20, 'kernel': 'linear'} 1.000000 \n", "5 rbf {'C': 20, 'kernel': 'rbf'} 0.966667 \n", "6 linear {'C': 30, 'kernel': 'linear'} 1.000000 \n", "7 rbf {'C': 30, 'kernel': 'rbf'} 0.966667 \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.900000 0.966667 1.0 \n", "3 1.0 0.966667 0.966667 1.0 \n", "4 1.0 0.900000 0.933333 1.0 \n", "5 1.0 0.900000 0.966667 1.0 \n", "6 1.0 0.900000 0.900000 1.0 \n", "7 1.0 0.900000 0.933333 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.973333 0.038873 4 \n", "3 0.980000 0.016330 1 \n", "4 0.966667 0.042164 6 \n", "5 0.966667 0.036515 5 \n", "6 0.960000 0.048990 7 \n", "7 0.960000 0.038873 7 " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = pd.DataFrame(classifier.cv_results_)\n", "result" ] }, { "cell_type": "code", "execution_count": 18, "id": "a54f67eb", "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
01linear{'C': 1, 'kernel': 'linear'}0.980000
11rbf{'C': 1, 'kernel': 'rbf'}0.980000
210linear{'C': 10, 'kernel': 'linear'}0.973333
310rbf{'C': 10, 'kernel': 'rbf'}0.980000
420linear{'C': 20, 'kernel': 'linear'}0.966667
520rbf{'C': 20, 'kernel': 'rbf'}0.966667
630linear{'C': 30, 'kernel': 'linear'}0.960000
730rbf{'C': 30, 'kernel': 'rbf'}0.960000
\n", "
" ], "text/plain": [ " param_C param_kernel params mean_test_score\n", "0 1 linear {'C': 1, 'kernel': 'linear'} 0.980000\n", "1 1 rbf {'C': 1, 'kernel': 'rbf'} 0.980000\n", "2 10 linear {'C': 10, 'kernel': 'linear'} 0.973333\n", "3 10 rbf {'C': 10, 'kernel': 'rbf'} 0.980000\n", "4 20 linear {'C': 20, 'kernel': 'linear'} 0.966667\n", "5 20 rbf {'C': 20, 'kernel': 'rbf'} 0.966667\n", "6 30 linear {'C': 30, 'kernel': 'linear'} 0.960000\n", "7 30 rbf {'C': 30, 'kernel': 'rbf'} 0.960000" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result[[\"param_C\",\"param_kernel\",\"params\",\"mean_test_score\"]]" ] }, { "cell_type": "code", "execution_count": 19, "id": "c61f041d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
GridSearchCV(cv=5, estimator=KNeighborsClassifier(),\n",
       "             param_grid={'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": [ "GridSearchCV(cv=5, estimator=KNeighborsClassifier(),\n", " param_grid={'n_neighbors': [1, 10, 20, 30],\n", " 'weights': ['uniform', 'distance']})" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classifier_2 = GridSearchCV((model_KNN),{\n", " \"n_neighbors\":[1,10,20,30],\n", " \"weights\":['uniform', 'distance'] \n", "},cv=5,return_train_score=False)\n", "\n", "classifier_2.fit(X,y)" ] }, { "cell_type": "code", "execution_count": 20, "id": "3fb1ece3", "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_n_neighborsparam_weightsparamssplit0_test_scoresplit1_test_scoresplit2_test_scoresplit3_test_scoresplit4_test_scoremean_test_scorestd_test_scorerank_test_score
00.0034840.0006870.0052640.0011361uniform{'n_neighbors': 1, 'weights': 'uniform'}0.9666670.9666670.9333330.9333331.00.9600000.0249444
10.0036650.0013100.0040290.0007921distance{'n_neighbors': 1, 'weights': 'distance'}0.9666670.9666670.9333330.9333331.00.9600000.0249444
20.0027060.0004790.0048420.00121810uniform{'n_neighbors': 10, 'weights': 'uniform'}0.9666671.0000001.0000000.9333331.00.9800000.0266672
30.0022240.0001910.0026820.00041310distance{'n_neighbors': 10, 'weights': 'distance'}0.9666671.0000001.0000000.9666671.00.9866670.0163301
40.0024510.0005520.0033100.00034120uniform{'n_neighbors': 20, 'weights': 'uniform'}0.9333331.0000000.9333330.9333331.00.9600000.0326604
50.0023830.0003630.0025980.00041020distance{'n_neighbors': 20, 'weights': 'distance'}0.9666671.0000000.9333330.9666671.00.9733330.0249443
60.0023830.0003500.0031490.00037230uniform{'n_neighbors': 30, 'weights': 'uniform'}0.9000000.9666670.9333330.9000001.00.9400000.0388738
70.0022400.0006330.0028220.00089930distance{'n_neighbors': 30, 'weights': 'distance'}0.9666670.9666670.9333330.9333331.00.9600000.0249444
\n", "
" ], "text/plain": [ " mean_fit_time std_fit_time mean_score_time std_score_time \\\n", "0 0.003484 0.000687 0.005264 0.001136 \n", "1 0.003665 0.001310 0.004029 0.000792 \n", "2 0.002706 0.000479 0.004842 0.001218 \n", "3 0.002224 0.000191 0.002682 0.000413 \n", "4 0.002451 0.000552 0.003310 0.000341 \n", "5 0.002383 0.000363 0.002598 0.000410 \n", "6 0.002383 0.000350 0.003149 0.000372 \n", "7 0.002240 0.000633 0.002822 0.000899 \n", "\n", " param_n_neighbors param_weights \\\n", "0 1 uniform \n", "1 1 distance \n", "2 10 uniform \n", "3 10 distance \n", "4 20 uniform \n", "5 20 distance \n", "6 30 uniform \n", "7 30 distance \n", "\n", " params split0_test_score \\\n", "0 {'n_neighbors': 1, 'weights': 'uniform'} 0.966667 \n", "1 {'n_neighbors': 1, 'weights': 'distance'} 0.966667 \n", "2 {'n_neighbors': 10, 'weights': 'uniform'} 0.966667 \n", "3 {'n_neighbors': 10, 'weights': 'distance'} 0.966667 \n", "4 {'n_neighbors': 20, 'weights': 'uniform'} 0.933333 \n", "5 {'n_neighbors': 20, 'weights': 'distance'} 0.966667 \n", "6 {'n_neighbors': 30, 'weights': 'uniform'} 0.900000 \n", "7 {'n_neighbors': 30, 'weights': 'distance'} 0.966667 \n", "\n", " split1_test_score split2_test_score split3_test_score split4_test_score \\\n", "0 0.966667 0.933333 0.933333 1.0 \n", "1 0.966667 0.933333 0.933333 1.0 \n", "2 1.000000 1.000000 0.933333 1.0 \n", "3 1.000000 1.000000 0.966667 1.0 \n", "4 1.000000 0.933333 0.933333 1.0 \n", "5 1.000000 0.933333 0.966667 1.0 \n", "6 0.966667 0.933333 0.900000 1.0 \n", "7 0.966667 0.933333 0.933333 1.0 \n", "\n", " mean_test_score std_test_score rank_test_score \n", "0 0.960000 0.024944 4 \n", "1 0.960000 0.024944 4 \n", "2 0.980000 0.026667 2 \n", "3 0.986667 0.016330 1 \n", "4 0.960000 0.032660 4 \n", "5 0.973333 0.024944 3 \n", "6 0.940000 0.038873 8 \n", "7 0.960000 0.024944 4 " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_2 = pd.DataFrame(classifier_2.cv_results_)\n", "result_2" ] }, { "cell_type": "code", "execution_count": 21, "id": "f6f8302a", "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_n_neighborsparam_weightsparamsmean_test_score
01uniform{'n_neighbors': 1, 'weights': 'uniform'}0.960000
11distance{'n_neighbors': 1, 'weights': 'distance'}0.960000
210uniform{'n_neighbors': 10, 'weights': 'uniform'}0.980000
310distance{'n_neighbors': 10, 'weights': 'distance'}0.986667
420uniform{'n_neighbors': 20, 'weights': 'uniform'}0.960000
520distance{'n_neighbors': 20, 'weights': 'distance'}0.973333
630uniform{'n_neighbors': 30, 'weights': 'uniform'}0.940000
730distance{'n_neighbors': 30, 'weights': 'distance'}0.960000
\n", "
" ], "text/plain": [ " param_n_neighbors param_weights \\\n", "0 1 uniform \n", "1 1 distance \n", "2 10 uniform \n", "3 10 distance \n", "4 20 uniform \n", "5 20 distance \n", "6 30 uniform \n", "7 30 distance \n", "\n", " params mean_test_score \n", "0 {'n_neighbors': 1, 'weights': 'uniform'} 0.960000 \n", "1 {'n_neighbors': 1, 'weights': 'distance'} 0.960000 \n", "2 {'n_neighbors': 10, 'weights': 'uniform'} 0.980000 \n", "3 {'n_neighbors': 10, 'weights': 'distance'} 0.986667 \n", "4 {'n_neighbors': 20, 'weights': 'uniform'} 0.960000 \n", "5 {'n_neighbors': 20, 'weights': 'distance'} 0.973333 \n", "6 {'n_neighbors': 30, 'weights': 'uniform'} 0.940000 \n", "7 {'n_neighbors': 30, 'weights': 'distance'} 0.960000 " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_2[[\"param_n_neighbors\",\"param_weights\",\"params\",\"mean_test_score\"]]" ] }, { "cell_type": "code", "execution_count": 22, "id": "f8d6afad", "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import RandomizedSearchCV\n", "classifier_r = RandomizedSearchCV((model_SVC),{\n", " \"C\" : [1,10,20,30],\n", " \"kernel\" : [\"rbf\",\"linear\"]\n", "},n_iter=4,cv=5,return_train_score=False)" ] }, { "cell_type": "code", "execution_count": 23, "id": "52b0109c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
RandomizedSearchCV(cv=5, estimator=SVC(gamma='auto'), n_iter=4,\n",
       "                   param_distributions={'C': [1, 10, 20, 30],\n",
       "                                        '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": [ "RandomizedSearchCV(cv=5, estimator=SVC(gamma='auto'), n_iter=4,\n", " param_distributions={'C': [1, 10, 20, 30],\n", " 'kernel': ['rbf', 'linear']})" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classifier_r.fit(X,y)" ] }, { "cell_type": "code", "execution_count": 24, "id": "f23c0530", "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_kernelparam_Cparamssplit0_test_scoresplit1_test_scoresplit2_test_scoresplit3_test_scoresplit4_test_scoremean_test_scorestd_test_scorerank_test_score
00.0041690.0022650.0031270.001470linear20{'kernel': 'linear', 'C': 20}1.0000001.00.9000000.9333331.00.9666670.0421643
10.0039830.0008180.0029060.000561linear10{'kernel': 'linear', 'C': 10}1.0000001.00.9000000.9666671.00.9733330.0388732
20.0071780.0026330.0039920.001910rbf10{'kernel': 'rbf', 'C': 10}0.9666671.00.9666670.9666671.00.9800000.0163301
30.0028660.0004060.0024720.000658rbf30{'kernel': 'rbf', 'C': 30}0.9666671.00.9000000.9333331.00.9600000.0388734
\n", "
" ], "text/plain": [ " mean_fit_time std_fit_time mean_score_time std_score_time param_kernel \\\n", "0 0.004169 0.002265 0.003127 0.001470 linear \n", "1 0.003983 0.000818 0.002906 0.000561 linear \n", "2 0.007178 0.002633 0.003992 0.001910 rbf \n", "3 0.002866 0.000406 0.002472 0.000658 rbf \n", "\n", " param_C params split0_test_score \\\n", "0 20 {'kernel': 'linear', 'C': 20} 1.000000 \n", "1 10 {'kernel': 'linear', 'C': 10} 1.000000 \n", "2 10 {'kernel': 'rbf', 'C': 10} 0.966667 \n", "3 30 {'kernel': 'rbf', 'C': 30} 0.966667 \n", "\n", " split1_test_score split2_test_score split3_test_score split4_test_score \\\n", "0 1.0 0.900000 0.933333 1.0 \n", "1 1.0 0.900000 0.966667 1.0 \n", "2 1.0 0.966667 0.966667 1.0 \n", "3 1.0 0.900000 0.933333 1.0 \n", "\n", " mean_test_score std_test_score rank_test_score \n", "0 0.966667 0.042164 3 \n", "1 0.973333 0.038873 2 \n", "2 0.980000 0.016330 1 \n", "3 0.960000 0.038873 4 " ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_r = pd.DataFrame(classifier_r.cv_results_)\n", "result_r" ] }, { "cell_type": "code", "execution_count": 25, "id": "027be1a3", "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_Cparam_kernelparamsmean_test_score
020linear{'kernel': 'linear', 'C': 20}0.966667
110linear{'kernel': 'linear', 'C': 10}0.973333
210rbf{'kernel': 'rbf', 'C': 10}0.980000
330rbf{'kernel': 'rbf', 'C': 30}0.960000
\n", "
" ], "text/plain": [ " param_C param_kernel params mean_test_score\n", "0 20 linear {'kernel': 'linear', 'C': 20} 0.966667\n", "1 10 linear {'kernel': 'linear', 'C': 10} 0.973333\n", "2 10 rbf {'kernel': 'rbf', 'C': 10} 0.980000\n", "3 30 rbf {'kernel': 'rbf', 'C': 30} 0.960000" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_r[[\"param_C\",\"param_kernel\",\"params\",\"mean_test_score\"]]" ] }, { "cell_type": "code", "execution_count": 26, "id": "c803d0fc", "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": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classifier_r2 = RandomizedSearchCV((model_KNN),{\n", " \"n_neighbors\":[1,10,20,30],\n", " \"weights\":['uniform', 'distance'] \n", "},n_iter=4,cv=5,return_train_score=False)\n", "\n", "classifier_r2.fit(X,y)" ] }, { "cell_type": "code", "execution_count": 27, "id": "4b5c7d25", "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.0028670.0004910.0032160.000418distance30{'weights': 'distance', 'n_neighbors': 30}0.9666670.9666670.9333330.9333331.00.9600000.0249442
10.0024140.0004110.0028130.000458distance1{'weights': 'distance', 'n_neighbors': 1}0.9666670.9666670.9333330.9333331.00.9600000.0249442
20.0021990.0002970.0031390.000432uniform1{'weights': 'uniform', 'n_neighbors': 1}0.9666670.9666670.9333330.9333331.00.9600000.0249442
30.0022520.0005750.0026120.000682distance20{'weights': 'distance', 'n_neighbors': 20}0.9666671.0000000.9333330.9666671.00.9733330.0249441
\n", "
" ], "text/plain": [ " mean_fit_time std_fit_time mean_score_time std_score_time param_weights \\\n", "0 0.002867 0.000491 0.003216 0.000418 distance \n", "1 0.002414 0.000411 0.002813 0.000458 distance \n", "2 0.002199 0.000297 0.003139 0.000432 uniform \n", "3 0.002252 0.000575 0.002612 0.000682 distance \n", "\n", " param_n_neighbors params \\\n", "0 30 {'weights': 'distance', 'n_neighbors': 30} \n", "1 1 {'weights': 'distance', 'n_neighbors': 1} \n", "2 1 {'weights': 'uniform', 'n_neighbors': 1} \n", "3 20 {'weights': 'distance', 'n_neighbors': 20} \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 0.966667 0.933333 0.933333 \n", "3 0.966667 1.000000 0.933333 0.966667 \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.960000 0.024944 2 \n", "3 1.0 0.973333 0.024944 1 " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_r2= pd.DataFrame(classifier_r2.cv_results_)\n", "result_r2" ] }, { "cell_type": "code", "execution_count": 28, "id": "84d996d1", "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
030distance{'weights': 'distance', 'n_neighbors': 30}0.960000
11distance{'weights': 'distance', 'n_neighbors': 1}0.960000
21uniform{'weights': 'uniform', 'n_neighbors': 1}0.960000
320distance{'weights': 'distance', 'n_neighbors': 20}0.973333
\n", "
" ], "text/plain": [ " param_n_neighbors param_weights \\\n", "0 30 distance \n", "1 1 distance \n", "2 1 uniform \n", "3 20 distance \n", "\n", " params mean_test_score \n", "0 {'weights': 'distance', 'n_neighbors': 30} 0.960000 \n", "1 {'weights': 'distance', 'n_neighbors': 1} 0.960000 \n", "2 {'weights': 'uniform', 'n_neighbors': 1} 0.960000 \n", "3 {'weights': 'distance', 'n_neighbors': 20} 0.973333 " ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_r2[[\"param_n_neighbors\",\"param_weights\",\"params\",\"mean_test_score\"]]" ] }, { "cell_type": "code", "execution_count": null, "id": "945c895a", "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.14.3" } }, "nbformat": 4, "nbformat_minor": 5 }