{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30683,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!unzip -qq /kaggle/input/dogs-vs-cats/test1.zip\n!unzip -qq /kaggle/input/dogs-vs-cats/train.zip","metadata":{"execution":{"iopub.status.busy":"2024-04-17T07:23:53.283443Z","iopub.execute_input":"2024-04-17T07:23:53.283719Z","iopub.status.idle":"2024-04-17T07:24:13.588420Z","shell.execute_reply.started":"2024-04-17T07:23:53.283685Z","shell.execute_reply":"2024-04-17T07:24:13.587332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport os\nimport numpy as np\nimport pandas as pd \nimport seaborn as sns\nfrom PIL import Image, ImageFilter\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score, classification_report","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-17T08:40:37.645949Z","iopub.execute_input":"2024-04-17T08:40:37.646679Z","iopub.status.idle":"2024-04-17T08:40:37.653563Z","shell.execute_reply.started":"2024-04-17T08:40:37.646647Z","shell.execute_reply":"2024-04-17T08:40:37.652600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to read and flatten images\ndef read_process_and_flatten_image(file_path, target_size=(350, 350), blur_radius=2):\n    with Image.open(file_path) as img:\n        # Resize image\n        img = img.resize(target_size, Image.ANTIALIAS)\n        # Apply blurring\n        img = img.filter(ImageFilter.GaussianBlur(blur_radius))\n        # Normalize pixel values\n        img_array = np.array(img) / 255.0\n        # Flatten image\n        img_array_flattened = img_array.flatten()\n    return img_array_flattened\n\n# Define the directories containing the images\ntrain_dir = \"/kaggle/working/train/\"\n\n# Initialize lists to store file paths, pixel values, and labels\nfile_paths = []\npixels = []\nlabels = []\n\n# Iterate over the train directory to collect file paths, pixel values, and labels\nfor file_name in os.listdir(train_dir)[:1100]:  # Selecting 1000 images for training\n    file_path = os.path.join(train_dir, file_name)\n    file_paths.append(file_path)\n    image_pixels = read_process_and_flatten_image(file_path)\n    pixels.append(image_pixels)\n    labels.append(file_name.split('.')[0])  # Assuming label is before the first '.'\n\n# Apply PCA to reduce dimensionality of the pixel values\npca = PCA(n_components=100)  # Adjust the number of components as needed\npixels_pca = pca.fit_transform(pixels)\n\n# Create a DataFrame from the collected data\ndata = pd.DataFrame({'file_path': file_paths, 'pixels_pca': pixels_pca.tolist(), 'label': labels})\n\n# Shuffle the DataFrame to randomize the order of the data\ndata = data.sample(frac=1).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:26:46.472047Z","iopub.execute_input":"2024-04-17T08:26:46.473011Z","iopub.status.idle":"2024-04-17T08:27:40.374654Z","shell.execute_reply.started":"2024-04-17T08:26:46.472968Z","shell.execute_reply":"2024-04-17T08:27:40.373725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:15:47.184204Z","iopub.execute_input":"2024-04-17T08:15:47.184563Z","iopub.status.idle":"2024-04-17T08:15:47.193434Z","shell.execute_reply.started":"2024-04-17T08:15:47.184535Z","shell.execute_reply":"2024-04-17T08:15:47.192458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"whitegrid\")  # Set the style\nplt.figure(figsize=(10, 6))  # Set the figure size\n\ncustom_cmap = \"viridis\"  # You can choose any colormap you prefer\n\nax = sns.countplot(x='label', data=data, order=data['label'].value_counts().index, palette=custom_cmap)\n\nfor p in ax.patches:\n    ax.annotate(f'{p.get_height()}', (p.get_x() + p.get_width() / 2., p.get_height()),\n                ha='center', va='center', fontsize=11, color='black', xytext=(0, 5),\n                textcoords='offset points')\n\nplt.xlabel('Label', fontsize=14)\nplt.ylabel('Count', fontsize=14)\nplt.title('Count of Label', fontsize=16)\n\nplt.xticks(rotation=45)\n\n# Show plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:15:49.093346Z","iopub.execute_input":"2024-04-17T08:15:49.093702Z","iopub.status.idle":"2024-04-17T08:15:49.367357Z","shell.execute_reply.started":"2024-04-17T08:15:49.093673Z","shell.execute_reply":"2024-04-17T08:15:49.366451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:16:16.667387Z","iopub.execute_input":"2024-04-17T08:16:16.667744Z","iopub.status.idle":"2024-04-17T08:16:16.675991Z","shell.execute_reply.started":"2024-04-17T08:16:16.667716Z","shell.execute_reply":"2024-04-17T08:16:16.675021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dog Image\nplt.figure(figsize=(15, 6))\nplt.suptitle('Random Dog Images', fontsize=16)\nfor i, img_file in enumerate(random.sample(list(data[data['label'] == 'dog']['file_path']), 5), 1):\n    plt.subplot(2, 5, i)\n    img = mpimg.imread(img_file)\n    plt.imshow(img)\n    plt.axis('off')\n\n\n## Cat Image    \nplt.figure(figsize=(15, 6))\nplt.suptitle('Random Cat Images', fontsize=16)\nfor i, img_file in enumerate(random.sample(list(data[data['label'] == 'cat']['file_path']), 5), 1):\n    img_path = os.path.join(\"/kaggle/working/train\", img_file)\n    plt.subplot(2, 5, i)\n    img = mpimg.imread(img_path)\n    plt.imshow(img)\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:16:48.005741Z","iopub.execute_input":"2024-04-17T08:16:48.006209Z","iopub.status.idle":"2024-04-17T08:16:49.422183Z","shell.execute_reply.started":"2024-04-17T08:16:48.006171Z","shell.execute_reply":"2024-04-17T08:16:49.421329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(data['pixels_pca'].tolist(), data['label'], test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:37:38.950150Z","iopub.execute_input":"2024-04-17T08:37:38.950507Z","iopub.status.idle":"2024-04-17T08:37:38.957906Z","shell.execute_reply.started":"2024-04-17T08:37:38.950477Z","shell.execute_reply":"2024-04-17T08:37:38.956688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training data ', len(X_train))\nprint('Testing data ', len(X_test))","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:37:39.854411Z","iopub.execute_input":"2024-04-17T08:37:39.855250Z","iopub.status.idle":"2024-04-17T08:37:39.859873Z","shell.execute_reply.started":"2024-04-17T08:37:39.855217Z","shell.execute_reply":"2024-04-17T08:37:39.858862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Standardize the pixel values\nscaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:46:22.471206Z","iopub.execute_input":"2024-04-17T08:46:22.471942Z","iopub.status.idle":"2024-04-17T08:46:22.502310Z","shell.execute_reply.started":"2024-04-17T08:46:22.471907Z","shell.execute_reply":"2024-04-17T08:46:22.501589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(X_train_scaled)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:51:46.106341Z","iopub.execute_input":"2024-04-17T08:51:46.107033Z","iopub.status.idle":"2024-04-17T08:51:46.138820Z","shell.execute_reply.started":"2024-04-17T08:51:46.107001Z","shell.execute_reply":"2024-04-17T08:51:46.137735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Logistic Regression\nlog_reg = LogisticRegression(max_iter=1000)\nlog_reg.fit(X_train_scaled, y_train)\nlog_reg_pred = log_reg.predict(X_test_scaled)\nlog_reg_accuracy = accuracy_score(y_test, log_reg_pred)\nprint(\"Logistic Regression Accuracy:\", log_reg_accuracy)\nprint(\"Logistic Regression Classification Report:\")\nprint(classification_report(y_test, log_reg_pred))","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:46:41.720591Z","iopub.execute_input":"2024-04-17T08:46:41.720964Z","iopub.status.idle":"2024-04-17T08:46:41.762860Z","shell.execute_reply.started":"2024-04-17T08:46:41.720934Z","shell.execute_reply":"2024-04-17T08:46:41.761642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm_clf = SVC()\nsvm_clf.fit(X_train_scaled, y_train)\nsvm_pred = svm_clf.predict(X_test_scaled)\nsvm_accuracy = accuracy_score(y_test, svm_pred)\nprint(\"SVM Accuracy:\", svm_accuracy)\nprint(\"SVM Classification Report:\")\nprint(classification_report(y_test, svm_pred))","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:46:44.753744Z","iopub.execute_input":"2024-04-17T08:46:44.754107Z","iopub.status.idle":"2024-04-17T08:46:44.875418Z","shell.execute_reply.started":"2024-04-17T08:46:44.754080Z","shell.execute_reply":"2024-04-17T08:46:44.874476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Decision Tree Classifier\ntree_clf = DecisionTreeClassifier()\ntree_clf.fit(X_train_scaled, y_train)\ntree_pred = tree_clf.predict(X_test_scaled)\ntree_accuracy = accuracy_score(y_test, tree_pred)\nprint(\"\\nDecision Tree Accuracy:\", tree_accuracy)\nprint(\"Decision Tree Classification Report:\")\nprint(classification_report(y_test, tree_pred))","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:46:47.482489Z","iopub.execute_input":"2024-04-17T08:46:47.482882Z","iopub.status.idle":"2024-04-17T08:46:47.591284Z","shell.execute_reply.started":"2024-04-17T08:46:47.482848Z","shell.execute_reply":"2024-04-17T08:46:47.590245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Random Forest Classifier\nrf_clf = RandomForestClassifier()\nrf_clf.fit(X_train_scaled, y_train)\nrf_pred = rf_clf.predict(X_test_scaled)\nrf_accuracy = accuracy_score(y_test, rf_pred)\nprint(\"\\nRandom Forest Accuracy:\", rf_accuracy)\nprint(\"Random Forest Classification Report:\")\nprint(classification_report(y_test, rf_pred))","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:46:50.182666Z","iopub.execute_input":"2024-04-17T08:46:50.183356Z","iopub.status.idle":"2024-04-17T08:46:50.989662Z","shell.execute_reply.started":"2024-04-17T08:46:50.183322Z","shell.execute_reply":"2024-04-17T08:46:50.988687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyperparameter tuning for Logistic Regression\nparam_grid_log_reg = {'C': [0.001, 0.01, 0.1, 1, 10, 100]}\ngrid_search_log_reg = GridSearchCV(LogisticRegression(max_iter=1000), param_grid_log_reg, cv=5, scoring='accuracy')\ngrid_search_log_reg.fit(X_train_scaled, y_train)\n\nprint(\"Best parameters for Logistic Regression:\", grid_search_log_reg.best_params_)\nprint(\"Best cross-validation score for Logistic Regression:\", grid_search_log_reg.best_score_)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:46:55.432138Z","iopub.execute_input":"2024-04-17T08:46:55.432967Z","iopub.status.idle":"2024-04-17T08:46:56.032391Z","shell.execute_reply.started":"2024-04-17T08:46:55.432926Z","shell.execute_reply":"2024-04-17T08:46:56.031184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the parameter grid for Decision Tree Classifier\nparam_grid_decision_tree = {\n    'max_depth': [None, 10, 20, 30, 40, 50],\n    'min_samples_split': [2, 5, 10],\n    'min_samples_leaf': [1, 2, 4],\n    'max_features': ['auto', 'sqrt', 'log2', None]\n}\n\n# Perform GridSearchCV for Decision Tree Classifier\ngrid_search_decision_tree = GridSearchCV(\n    DecisionTreeClassifier(), \n    param_grid_decision_tree, \n    cv=5, \n    scoring='accuracy'\n)\n\n\ngrid_search_decision_tree.fit(X_train_scaled, y_train)\n\n# Print the best parameters and best cross-validation score\nprint(\"Best parameters for Decision Tree Classifier:\")\nprint(grid_search_decision_tree.best_params_)\nprint(\"Best cross-validation score for Decision Tree Classifier:\")\nprint(grid_search_decision_tree.best_score_)","metadata":{"execution":{"iopub.status.busy":"2024-04-17T08:47:01.884674Z","iopub.execute_input":"2024-04-17T08:47:01.885508Z","iopub.status.idle":"2024-04-17T08:47:26.555226Z","shell.execute_reply.started":"2024-04-17T08:47:01.885475Z","shell.execute_reply":"2024-04-17T08:47:26.554109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}