{"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":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from torch.utils.data import DataLoader\nfrom torchvision import transforms, datasets\nimport numpy as np\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.model_selection import GridSearchCV\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-15T06:22:14.164452Z","iopub.execute_input":"2024-04-15T06:22:14.164857Z","iopub.status.idle":"2024-04-15T06:22:24.481121Z","shell.execute_reply.started":"2024-04-15T06:22:14.164817Z","shell.execute_reply":"2024-04-15T06:22:24.479877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_rows', 200)\npd.set_option('display.max_columns', 50)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:22:30.241066Z","iopub.execute_input":"2024-04-15T06:22:30.241645Z","iopub.status.idle":"2024-04-15T06:22:30.247857Z","shell.execute_reply.started":"2024-04-15T06:22:30.241613Z","shell.execute_reply":"2024-04-15T06:22:30.246738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CIFAR-10\nCIFAR_transform_train = transforms.Compose([transforms.ToTensor()])\nCIFAR_transform_test =transforms.Compose([transforms.ToTensor()])\ntrainset_CIFAR = datasets.CIFAR10(root='./data', train=True, download=True, transform=\nCIFAR_transform_train)\ntestset_CIFAR = datasets.CIFAR10(root='./data', train=False, download=True,\ntransform=CIFAR_transform_test)\nCIFAR_train = DataLoader(trainset_CIFAR, batch_size=32, shuffle=True, num_workers=2)\nCIFAR_test = DataLoader(testset_CIFAR, batch_size=32, shuffle=False, num_workers=2)\nCIFAR_train_images = []\nCIFAR_train_labels = []\nfor batch in CIFAR_train:\n    images, labels = batch\n    images_flat = images.view(images.shape[0], -1)\n    CIFAR_train_images.append(images_flat.numpy())\n    CIFAR_train_labels.append(labels.numpy())\nCIFAR_train_images = np.vstack(CIFAR_train_images)\nCIFAR_train_labels = np.concatenate(CIFAR_train_labels)\nCIFAR_test_images = []\nCIFAR_test_labels = []\nfor batch in CIFAR_test:\n    images, labels = batch\n    images_flat = images.view(images.shape[0], -1)\n    CIFAR_test_images.append(images_flat.numpy())\n    CIFAR_test_labels.append(labels.numpy())\nCIFAR_test_images = np.vstack(CIFAR_test_images)\nCIFAR_test_labels = np.concatenate(CIFAR_test_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:22:31.274808Z","iopub.execute_input":"2024-04-15T06:22:31.275188Z","iopub.status.idle":"2024-04-15T06:22:57.384300Z","shell.execute_reply.started":"2024-04-15T06:22:31.275158Z","shell.execute_reply":"2024-04-15T06:22:57.382540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_depths = [3, 6, 9, 12]\nparam_grid = {\n    'min_samples_split': [2, 5, 10],\n    'min_samples_leaf': [1, 2, 4],\n    'max_leaf_nodes': [5, 10, None]\n}","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:22:57.387588Z","iopub.execute_input":"2024-04-15T06:22:57.388119Z","iopub.status.idle":"2024-04-15T06:22:57.398292Z","shell.execute_reply.started":"2024-04-15T06:22:57.388069Z","shell.execute_reply":"2024-04-15T06:22:57.396662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_results = []\n\nfor depth in max_depths:\n    grid_search = GridSearchCV(estimator=DecisionTreeClassifier(max_depth=depth, random_state=42),\n                               param_grid=param_grid,\n                               cv=5,\n                               scoring='accuracy',\n                               verbose=1)\n    grid_search.fit(CIFAR_train_images, CIFAR_train_labels)\n    best_model = grid_search.best_estimator_\n\n    train_predictions = best_model.predict(CIFAR_train_images)\n    test_predictions = best_model.predict(CIFAR_test_images)\n\n    train_accuracy = accuracy_score(CIFAR_train_labels, train_predictions)\n    test_accuracy = accuracy_score(CIFAR_test_labels, test_predictions)\n    \n    print(\"Max Depth:\", depth)\n    print(\"Best parameters found:\", grid_search.best_params_)\n    print(\"Accuracy on training set:\", train_accuracy * 100)\n    print(\"Accuracy on test set:\", test_accuracy * 100)\n    print(\"\\n\\n\")\n    \n    results = pd.DataFrame(grid_search.cv_results_)\n    selected_columns = ['param_min_samples_split', 'param_min_samples_leaf', 'param_max_leaf_nodes', 'mean_test_score']\n    filtered_results = results.loc[:, selected_columns]\n    filtered_results['mean_test_score'] = filtered_results['mean_test_score'] * 100\n    filtered_results.insert(0, 'max_depth', depth)\n    all_results.append(filtered_results)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:22:57.399726Z","iopub.execute_input":"2024-04-15T06:22:57.400081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_results = pd.concat(all_results, ignore_index=True)\nfinal_results","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}