{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# import libraries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.svm import SVC\nimport tensorflow as tf\nfrom tensorflow.keras import layers, optimizers\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import classification_report, confusion_matrix, accuracy_score\nfrom sklearn.metrics import roc_curve, roc_auc_score\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.model_selection import cross_val_score, RepeatedStratifiedKFold\nfrom sklearn.preprocessing import MinMaxScaler","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:15:20.538056Z","iopub.execute_input":"2023-03-25T13:15:20.538505Z","iopub.status.idle":"2023-03-25T13:15:20.546590Z","shell.execute_reply.started":"2023-03-25T13:15:20.538462Z","shell.execute_reply":"2023-03-25T13:15:20.545305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# set files directories\nLANDMARK_FILES_DIR = \"/kaggle/input/asl-signs/train_landmark_files\"\nTRAIN_FILE = \"/kaggle/input/asl-signs/train.csv\"","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:04:51.337443Z","iopub.execute_input":"2023-03-25T13:04:51.337876Z","iopub.status.idle":"2023-03-25T13:04:51.343680Z","shell.execute_reply.started":"2023-03-25T13:04:51.337835Z","shell.execute_reply":"2023-03-25T13:04:51.342188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csv_data=pd.read_csv(TRAIN_FILE)\ncsv_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:04:51.999193Z","iopub.execute_input":"2023-03-25T13:04:51.999610Z","iopub.status.idle":"2023-03-25T13:04:52.126801Z","shell.execute_reply.started":"2023-03-25T13:04:51.999573Z","shell.execute_reply":"2023-03-25T13:04:52.125122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"code","source":"# Randomly select 1000 rows from the DataFrame\nsubset = csv_data.sample(n=1000, random_state=42)\n\n# Load each parquet file into a separate Pandas DataFrame\nparquet_data = []\nfor file_path in subset['path']:\n    df = pd.read_parquet('/kaggle/input/asl-signs/'+file_path)\n    parquet_data.append(df)\n\n# Concatenate the parquet data into a single DataFrame\ndata = pd.concat(parquet_data)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:04:52.339142Z","iopub.execute_input":"2023-03-25T13:04:52.339614Z","iopub.status.idle":"2023-03-25T13:05:09.729168Z","shell.execute_reply.started":"2023-03-25T13:04:52.339574Z","shell.execute_reply":"2023-03-25T13:05:09.727577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reset the index of the subset data frame\nsubset = subset.reset_index()\n\n# Merge the label information from the CSV file with the parquet data using the index\ndata_with_labels = pd.merge(data, subset, left_index=True, right_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:09.731446Z","iopub.execute_input":"2023-03-25T13:05:09.731981Z","iopub.status.idle":"2023-03-25T13:05:12.622763Z","shell.execute_reply.started":"2023-03-25T13:05:09.731936Z","shell.execute_reply":"2023-03-25T13:05:12.621198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_with_labels = data_with_labels.drop(['row_id','path','frame','index'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:32.549929Z","iopub.execute_input":"2023-03-25T13:05:32.550352Z","iopub.status.idle":"2023-03-25T13:05:32.826684Z","shell.execute_reply.started":"2023-03-25T13:05:32.550318Z","shell.execute_reply":"2023-03-25T13:05:32.825292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_with_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:33.086453Z","iopub.execute_input":"2023-03-25T13:05:33.087491Z","iopub.status.idle":"2023-03-25T13:05:33.109939Z","shell.execute_reply.started":"2023-03-25T13:05:33.087445Z","shell.execute_reply":"2023-03-25T13:05:33.108163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_with_labels.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:34.384788Z","iopub.execute_input":"2023-03-25T13:05:34.385981Z","iopub.status.idle":"2023-03-25T13:05:34.393491Z","shell.execute_reply.started":"2023-03-25T13:05:34.385933Z","shell.execute_reply":"2023-03-25T13:05:34.391633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_with_labels['type'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:34.917026Z","iopub.execute_input":"2023-03-25T13:05:34.918177Z","iopub.status.idle":"2023-03-25T13:05:35.008319Z","shell.execute_reply.started":"2023-03-25T13:05:34.918128Z","shell.execute_reply":"2023-03-25T13:05:35.003571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Only select right-hand and left-hand from dataset for our baseline model\n# Filter the rows corresponding to left_hand and right_hand\nhand_sign_data = data_with_labels[data_with_labels['type'].isin(['left_hand', 'right_hand'])]\n\n# Check the class distribution\nprint(hand_sign_data['type'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:36.110729Z","iopub.execute_input":"2023-03-25T13:05:36.111135Z","iopub.status.idle":"2023-03-25T13:05:36.144843Z","shell.execute_reply.started":"2023-03-25T13:05:36.111102Z","shell.execute_reply":"2023-03-25T13:05:36.143509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hand_sign_data","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:37.254292Z","iopub.execute_input":"2023-03-25T13:05:37.254697Z","iopub.status.idle":"2023-03-25T13:05:37.277042Z","shell.execute_reply.started":"2023-03-25T13:05:37.254663Z","shell.execute_reply":"2023-03-25T13:05:37.274921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Since the dataset is huge, as out baseline model we will take a sample of 5000 rows while shuffling the datasets to get different class labels in the datset.","metadata":{}},{"cell_type":"code","source":"# Shuffle the dataset\nshuffled_data = hand_sign_data.sample(frac=1)\n\n# Select a sample of 5000 rows\nsampled_data = shuffled_data.sample(n=20000)\n\n# Print the first 5 rows of the sampled dataset\nprint(sampled_data.head())","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:39.199804Z","iopub.execute_input":"2023-03-25T13:05:39.200541Z","iopub.status.idle":"2023-03-25T13:05:39.222352Z","shell.execute_reply.started":"2023-03-25T13:05:39.200484Z","shell.execute_reply":"2023-03-25T13:05:39.221324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a label encoder for the 'type' column\ntype_encoder = LabelEncoder()\n\n# Fit the encoder to the 'type' column and transform the values\nsampled_data['type'] = type_encoder.fit_transform(sampled_data['type'])\n\n# Create a label encoder for the 'sign' column\nsign_encoder = LabelEncoder()\n\n# Fit the encoder to the 'sign' column and transform the values\nsampled_data['sign'] = sign_encoder.fit_transform(sampled_data['sign'])","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:41.472916Z","iopub.execute_input":"2023-03-25T13:05:41.473774Z","iopub.status.idle":"2023-03-25T13:05:41.494132Z","shell.execute_reply.started":"2023-03-25T13:05:41.473717Z","shell.execute_reply":"2023-03-25T13:05:41.492496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sampled_data=sampled_data.dropna()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:41.841690Z","iopub.execute_input":"2023-03-25T13:05:41.842129Z","iopub.status.idle":"2023-03-25T13:05:41.852332Z","shell.execute_reply.started":"2023-03-25T13:05:41.842072Z","shell.execute_reply":"2023-03-25T13:05:41.850926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sampled_data","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:42.973884Z","iopub.execute_input":"2023-03-25T13:05:42.974386Z","iopub.status.idle":"2023-03-25T13:05:42.997891Z","shell.execute_reply.started":"2023-03-25T13:05:42.974334Z","shell.execute_reply":"2023-03-25T13:05:42.996909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# KNN","metadata":{}},{"cell_type":"code","source":"# Separate the features and labels into separate arrays\nX = sampled_data.drop('sign', axis=1).values\ny = sampled_data['sign'].values","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:45.940450Z","iopub.execute_input":"2023-03-25T13:05:45.941427Z","iopub.status.idle":"2023-03-25T13:05:45.948043Z","shell.execute_reply.started":"2023-03-25T13:05:45.941382Z","shell.execute_reply":"2023-03-25T13:05:45.946691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split and Scale the Data","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=1)\nscaler = StandardScaler()\nscaler.fit(X_train)\nX_train = scaler.transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:48.337654Z","iopub.execute_input":"2023-03-25T13:05:48.338966Z","iopub.status.idle":"2023-03-25T13:05:48.352094Z","shell.execute_reply.started":"2023-03-25T13:05:48.338908Z","shell.execute_reply":"2023-03-25T13:05:48.350731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train and Evaluate KNN with an arbitrary K","metadata":{}},{"cell_type":"code","source":"K = 5\nclf = KNeighborsClassifier(n_neighbors=K)\nclf.fit(X_train, y_train)\ny_pred = clf.predict(X_test)\nprint(confusion_matrix(y_test, y_pred))\nprint(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:05:50.950940Z","iopub.execute_input":"2023-03-25T13:05:50.951454Z","iopub.status.idle":"2023-03-25T13:05:51.053587Z","shell.execute_reply.started":"2023-03-25T13:05:50.951411Z","shell.execute_reply":"2023-03-25T13:05:51.052191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Evaluate KNN accuracy on validation for different K values","metadata":{}},{"cell_type":"code","source":"k_range = range(1, 40, 2)\nacc = []\nfor k in k_range:\n    knn = KNeighborsClassifier(n_neighbors=k)\n    knn.fit(X_train, y_train)\n    y_pred = knn.predict(X_test)\n    acc.append(accuracy_score(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:14:17.864050Z","iopub.execute_input":"2023-03-25T13:14:17.865351Z","iopub.status.idle":"2023-03-25T13:14:19.852704Z","shell.execute_reply.started":"2023-03-25T13:14:17.865290Z","shell.execute_reply":"2023-03-25T13:14:19.851292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nplt.plot(k_range, acc, color='red', linestyle='dashed', marker='o',\n         markerfacecolor='blue', markersize=10)\nplt.title('Knn Accuracy on Validation for Different K Values')\nplt.xlabel('K Value')\nplt.ylabel('Accuracy');\n\n#best k is the max accuracy that we may return","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:14:23.352318Z","iopub.execute_input":"2023-03-25T13:14:23.352730Z","iopub.status.idle":"2023-03-25T13:14:23.642631Z","shell.execute_reply.started":"2023-03-25T13:14:23.352696Z","shell.execute_reply":"2023-03-25T13:14:23.641309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# What is the Best K and its corresponding Accuracy?\n# go from list to numpy array because of the attributes that numpy have it, and functions.\nacc=np.array(acc)\nbest_acc = acc[acc.argmax()]\nbest_k = max(acc)\n\nprint(f'Best K={best_k} achieved an accuracy = {best_acc :.4f}')","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:14:27.568068Z","iopub.execute_input":"2023-03-25T13:14:27.568494Z","iopub.status.idle":"2023-03-25T13:14:27.576211Z","shell.execute_reply.started":"2023-03-25T13:14:27.568459Z","shell.execute_reply":"2023-03-25T13:14:27.574659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Repeat the Evaluation of AUC","metadata":{}},{"cell_type":"code","source":"k_range = range(1, 40, 2)\nauc = []\nfor k in k_range:\n    knn = KNeighborsClassifier(n_neighbors=k)\n    knn.fit(X_train, y_train)\n    y_prob = knn.predict_proba(X_test) # use predict_proba instead of predict\n    auc.append(roc_auc_score(y_test, y_prob, multi_class='ovr')) # use y_prob instead of y_pred","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:14:32.330144Z","iopub.execute_input":"2023-03-25T13:14:32.331143Z","iopub.status.idle":"2023-03-25T13:14:34.144163Z","shell.execute_reply.started":"2023-03-25T13:14:32.331093Z","shell.execute_reply":"2023-03-25T13:14:34.142738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nplt.plot(k_range, auc, color='red', linestyle='dashed', marker='o',\n         markerfacecolor='blue', markersize=10)\nplt.title('Knn AUC scores on Validation for Different K Values')\nplt.xlabel('K Value')\nplt.ylabel('AUC');","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:14:34.146380Z","iopub.execute_input":"2023-03-25T13:14:34.146776Z","iopub.status.idle":"2023-03-25T13:14:34.451049Z","shell.execute_reply.started":"2023-03-25T13:14:34.146738Z","shell.execute_reply":"2023-03-25T13:14:34.449753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# What is the Best K and its corresponding Accuracy?\n\nauc = np.array(auc)\nbest_auc = auc[auc.argmax()]\nbest_k = k_range[auc.argmax()]\n\nprint(f'Best K={best_k} achieved an accuracy = {best_auc :.4f}')","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:14:36.880540Z","iopub.execute_input":"2023-03-25T13:14:36.880941Z","iopub.status.idle":"2023-03-25T13:14:36.888311Z","shell.execute_reply.started":"2023-03-25T13:14:36.880906Z","shell.execute_reply":"2023-03-25T13:14:36.886961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Select Best Parameter(s) based on Cross-Validation","metadata":{}},{"cell_type":"code","source":"k_range = range(1, 40, 2)\nscores_mean = []\nscores_std = []\ncv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=1)\n# cv = 5\nfor k in k_range:\n    # scaler = StandardScaler()\n    scaler = MinMaxScaler()\n    knn = KNeighborsClassifier(n_neighbors=k)\n    pipeline = Pipeline(steps=[('scaler', scaler), ('knn', knn)])\n    scores = cross_val_score(pipeline, X_train, y_train, cv=cv, scoring='accuracy')\n    scores_mean.append(scores.mean())\n    scores_std.append(scores.std())\n\n# plt.plot(k_range, acc)\nplt.errorbar(k_range, acc, yerr=scores_std, fmt='o-', markersize=8, capsize=4)\nplt.xlabel('Value of K for KNN')\nplt.ylabel('Cross-Validated Accuracy')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:15:25.287100Z","iopub.execute_input":"2023-03-25T13:15:25.287571Z","iopub.status.idle":"2023-03-25T13:15:47.849279Z","shell.execute_reply.started":"2023-03-25T13:15:25.287532Z","shell.execute_reply":"2023-03-25T13:15:47.847694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Best K and AUC / ACC Values\nscores = np.array(scores_mean)\nidx_best = scores.argmax()\nbest_score = scores[idx_best]\nbest_k = k_range[idx_best]\n\nprint(f'Best K={best_k} achieved a score = {best_score :.4f}')","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:15:50.998642Z","iopub.execute_input":"2023-03-25T13:15:50.999088Z","iopub.status.idle":"2023-03-25T13:15:51.007390Z","shell.execute_reply.started":"2023-03-25T13:15:50.999052Z","shell.execute_reply":"2023-03-25T13:15:51.006007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check if Model is overfitting\n\nIf the training accuracy is much higher than the test accuracy, it suggests that the model is overfitting. You could also try plotting a learning curve to visualize the model's performance on both the training and test sets as a function of the number of training examples used. If the curves diverge significantly, it may indicate overfitting.","metadata":{}},{"cell_type":"code","source":"# Calculate train accuracy\ny_train_pred = clf.predict(X_train)\ntrain_accuracy = accuracy_score(y_train, y_train_pred)\n\n# Calculate test accuracy\ny_test_pred = clf.predict(X_test)\ntest_accuracy = accuracy_score(y_test, y_test_pred)\n\nprint(\"Train Accuracy:\", train_accuracy)\nprint(\"Test Accuracy:\", test_accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:03:19.102600Z","iopub.execute_input":"2023-03-25T14:03:19.104056Z","iopub.status.idle":"2023-03-25T14:03:19.436850Z","shell.execute_reply.started":"2023-03-25T14:03:19.104006Z","shell.execute_reply":"2023-03-25T14:03:19.435437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using Grid Search","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\nparam_grid = {'n_neighbors':  np.arange(1, 40, 2)}\n\n# cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=1)\ngrid_search = GridSearchCV(KNeighborsClassifier(), param_grid=param_grid, cv=5, return_train_score=False)\ngrid_search.fit(X_train, y_train)\n\nprint(f\"best parameters: {grid_search.best_params_}\")\nprint(f\"best parameters: {grid_search.best_score_}\")\n# print(f\"test-set score: {grid.score(X_test, y_test):.3f}\")","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:16:07.989345Z","iopub.execute_input":"2023-03-25T13:16:07.989821Z","iopub.status.idle":"2023-03-25T13:16:16.033457Z","shell.execute_reply.started":"2023-03-25T13:16:07.989782Z","shell.execute_reply":"2023-03-25T13:16:16.032071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid_search.cv_results_\ngrid_search_df = pd.DataFrame(grid_search.cv_results_)\ngrid_search_df","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:16:17.660249Z","iopub.execute_input":"2023-03-25T13:16:17.661122Z","iopub.status.idle":"2023-03-25T13:16:17.696374Z","shell.execute_reply.started":"2023-03-25T13:16:17.661072Z","shell.execute_reply":"2023-03-25T13:16:17.694936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SVM","metadata":{}},{"cell_type":"markdown","source":"## Select Best Parameter(s) on Cross Validation","metadata":{}},{"cell_type":"markdown","source":"Use an RBF Kernel and try the C and Gamma ranges below","metadata":{}},{"cell_type":"code","source":"scoring =  'accuracy'\ncv = 5\n\nc_range = [0.001,0.01,0.1,1,10,100]\ngamma_range = [0.0001,0.001,0.01,0.1,1,10,100,1000]\nscores_mean = np.empty([len(c_range), len(gamma_range)])\nscores_std = np.empty([len(c_range), len(gamma_range)])\nfor idx_c, c in enumerate(c_range):\n    for idx_gamma, gamma in enumerate(gamma_range):\n        scaler = StandardScaler()\n        svc = SVC(gamma=gamma, C=c)\n        pipeline = Pipeline(steps=[('scaler', scaler), ('svc', svc)])\n        scores_tmp = cross_val_score(pipeline, X_train, y_train, cv=cv, scoring=scoring)\n        scores_mean[idx_c, idx_gamma] = np.mean(scores_tmp)\n        scores_std[idx_c, idx_gamma] = np.std(scores_tmp)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:17:29.209718Z","iopub.execute_input":"2023-03-25T13:17:29.210132Z","iopub.status.idle":"2023-03-25T13:31:23.318379Z","shell.execute_reply.started":"2023-03-25T13:17:29.210097Z","shell.execute_reply":"2023-03-25T13:31:23.317084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.set()\nfig, ax = plt.subplots(figsize=(8,6))\nsns.heatmap(scores_mean, annot=True, fmt='.2f', cmap='coolwarm', \n            xticklabels=gamma_range, yticklabels=c_range, \n            cbar_kws={'label': 'Mean CV Scores'},\n            ax=ax)\nax.set_xlabel('Gamma')\nax.set_ylabel('C')\nax.set_title('SVC Hyperparmeters')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:47:58.346660Z","iopub.execute_input":"2023-03-25T13:47:58.347144Z","iopub.status.idle":"2023-03-25T13:47:59.197237Z","shell.execute_reply.started":"2023-03-25T13:47:58.347102Z","shell.execute_reply":"2023-03-25T13:47:59.195671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the best hyperparmaters\nbest_score = scores_mean.argmax()\nrow_index, col_index = np.unravel_index(best_score, scores_mean.shape)\nprint(row_index , col_index)\nprint(c_range[row_index] , gamma_range[col_index])","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:48:02.522161Z","iopub.execute_input":"2023-03-25T13:48:02.522657Z","iopub.status.idle":"2023-03-25T13:48:02.530295Z","shell.execute_reply.started":"2023-03-25T13:48:02.522616Z","shell.execute_reply":"2023-03-25T13:48:02.528822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\nsvc = SVC()\n\nc_range = [0.001,0.01,0.1,1,10,100]\ngamma_range = [0.0001,0.001,0.01,0.1,1,10,100,1000]\n\nparam_grid = {'C': c_range, 'gamma': gamma_range}\n\ngrid_search = GridSearchCV(svc, param_grid=param_grid, cv=5, return_train_score=False) \ngrid_search.fit(X_train, y_train)\n\n# Print the best hyperparameters and the corresponding score\nprint(\"Best parameters: {}\".format(grid_search.best_params_))\nprint(\"Best score: {:.2f}\".format(grid_search.best_score_))","metadata":{"execution":{"iopub.status.busy":"2023-03-25T13:48:03.420721Z","iopub.execute_input":"2023-03-25T13:48:03.421786Z","iopub.status.idle":"2023-03-25T14:01:54.125399Z","shell.execute_reply.started":"2023-03-25T13:48:03.421744Z","shell.execute_reply":"2023-03-25T14:01:54.124053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract the results of the grid search\nresults = grid_search.cv_results_\nscores_mean = np.array(results['mean_test_score']).reshape(len(c_range), len(gamma_range))","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:01:54.127671Z","iopub.execute_input":"2023-03-25T14:01:54.128046Z","iopub.status.idle":"2023-03-25T14:01:54.133803Z","shell.execute_reply.started":"2023-03-25T14:01:54.128008Z","shell.execute_reply":"2023-03-25T14:01:54.132672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.set()\nfig, ax = plt.subplots(figsize=(8,6))\nsns.heatmap(scores_mean, annot=True, fmt='.2f', cmap='coolwarm', \n            xticklabels=gamma_range, yticklabels=c_range, \n            cbar_kws={'label': 'Mean CV Scores'},\n            ax=ax)\nax.set_xlabel('Gamma')\nax.set_ylabel('C')\nax.set_title('SVC Hyperparmeters')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-25T14:01:54.135197Z","iopub.execute_input":"2023-03-25T14:01:54.136429Z","iopub.status.idle":"2023-03-25T14:01:54.659601Z","shell.execute_reply.started":"2023-03-25T14:01:54.136384Z","shell.execute_reply":"2023-03-25T14:01:54.657878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}