{"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":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-05T15:29:26.015847Z","iopub.execute_input":"2024-05-05T15:29:26.016263Z","iopub.status.idle":"2024-05-05T15:29:26.027273Z","shell.execute_reply.started":"2024-05-05T15:29:26.016223Z","shell.execute_reply":"2024-05-05T15:29:26.025894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.datasets import fetch_openml\nimport pandas as pd\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:26.029604Z","iopub.execute_input":"2024-05-05T15:29:26.030156Z","iopub.status.idle":"2024-05-05T15:29:26.035118Z","shell.execute_reply.started":"2024-05-05T15:29:26.03012Z","shell.execute_reply":"2024-05-05T15:29:26.034289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:26.036551Z","iopub.execute_input":"2024-05-05T15:29:26.036909Z","iopub.status.idle":"2024-05-05T15:29:28.59544Z","shell.execute_reply.started":"2024-05-05T15:29:26.036879Z","shell.execute_reply":"2024-05-05T15:29:28.594565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test= pd.read_csv(\"../input/digit-recognizer/test.csv\")\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:28.597614Z","iopub.execute_input":"2024-05-05T15:29:28.59828Z","iopub.status.idle":"2024-05-05T15:29:30.171005Z","shell.execute_reply.started":"2024-05-05T15:29:28.598246Z","shell.execute_reply":"2024-05-05T15:29:30.169545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# put labels into y_train variable\ny = train[\"label\"]\n# Drop 'label' column\nX = train.drop(labels = [\"label\"],axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:30.17236Z","iopub.execute_input":"2024-05-05T15:29:30.17279Z","iopub.status.idle":"2024-05-05T15:29:30.311151Z","shell.execute_reply.started":"2024-05-05T15:29:30.172753Z","shell.execute_reply":"2024-05-05T15:29:30.309879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:30.313107Z","iopub.execute_input":"2024-05-05T15:29:30.313489Z","iopub.status.idle":"2024-05-05T15:29:30.321172Z","shell.execute_reply.started":"2024-05-05T15:29:30.31346Z","shell.execute_reply":"2024-05-05T15:29:30.32008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:30.322367Z","iopub.execute_input":"2024-05-05T15:29:30.323096Z","iopub.status.idle":"2024-05-05T15:29:30.333453Z","shell.execute_reply.started":"2024-05-05T15:29:30.323066Z","shell.execute_reply":"2024-05-05T15:29:30.332342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\nsome_digit = X.iloc[0]\nsome_digit_array = np.array(some_digit)  # Convert Series to NumPy array\nsome_digit_image = some_digit_array.reshape(28, 28)\n\nplt.imshow(some_digit_image, cmap=\"binary\")\nplt.axis(\"off\")  # Turn off axis\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:30.335099Z","iopub.execute_input":"2024-05-05T15:29:30.335811Z","iopub.status.idle":"2024-05-05T15:29:30.411826Z","shell.execute_reply.started":"2024-05-05T15:29:30.335769Z","shell.execute_reply":"2024-05-05T15:29:30.410786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X[:30000]\t#training set 60k\nX_test = X[30000:]\t#testing set 10k\ny_train = y[:30000]\t#training set label \ny_test = y[30000:]\t#testing set label","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:30.415454Z","iopub.execute_input":"2024-05-05T15:29:30.41604Z","iopub.status.idle":"2024-05-05T15:29:30.426143Z","shell.execute_reply.started":"2024-05-05T15:29:30.415999Z","shell.execute_reply":"2024-05-05T15:29:30.424717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_5 = (y_train == '5')\ny_test_5 = (y_test == '5')","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:30.428113Z","iopub.execute_input":"2024-05-05T15:29:30.428574Z","iopub.status.idle":"2024-05-05T15:29:30.442377Z","shell.execute_reply.started":"2024-05-05T15:29:30.428537Z","shell.execute_reply":"2024-05-05T15:29:30.440729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import SGDClassifier","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:29:30.448231Z","iopub.execute_input":"2024-05-05T15:29:30.448727Z","iopub.status.idle":"2024-05-05T15:29:30.455338Z","shell.execute_reply.started":"2024-05-05T15:29:30.448687Z","shell.execute_reply":"2024-05-05T15:29:30.454048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd_clf = SGDClassifier(random_state=42)\nsgd_clf.fit (X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:33:43.514057Z","iopub.execute_input":"2024-05-05T15:33:43.514456Z","iopub.status.idle":"2024-05-05T15:34:41.218057Z","shell.execute_reply.started":"2024-05-05T15:33:43.514427Z","shell.execute_reply":"2024-05-05T15:34:41.2165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd_clf.predict([some_digit])","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:35:34.143474Z","iopub.execute_input":"2024-05-05T15:35:34.14388Z","iopub.status.idle":"2024-05-05T15:35:34.155949Z","shell.execute_reply.started":"2024-05-05T15:35:34.14385Z","shell.execute_reply":"2024-05-05T15:35:34.154698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\n\ncross_val_score(sgd_clf,X_train,y_train_5, cv=3, scoring=\"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:35:37.396999Z","iopub.execute_input":"2024-05-05T15:35:37.397399Z","iopub.status.idle":"2024-05-05T15:35:38.197697Z","shell.execute_reply.started":"2024-05-05T15:35:37.397368Z","shell.execute_reply":"2024-05-05T15:35:38.195465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Cross Validation (Self Implementation)**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nfrom sklearn.base import clone\n\nskfolds = StratifiedKFold(n_splits=3)\n\nfor train_index, test_index in skfolds.split(X_train, y_train_5):\n    clone_clf = clone(sgd_clf)\n    X_train_folds = X_train.iloc[train_index]\n    y_train_folds = y_train_5.iloc[train_index]\n    X_test_fold = X_train.iloc[test_index]\n    y_test_fold = y_train_5.iloc[test_index]\n    \n    \n    clone_clf.fit(X_train_folds,y_train_folds)\n    y_pred = clone_clf.predict(X_test_fold)\n    n_correct = sum(y_pred == y_test_fold)\n    print (n_correct / len(y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-05-05T15:35:46.380934Z","iopub.execute_input":"2024-05-05T15:35:46.381335Z","iopub.status.idle":"2024-05-05T15:35:46.652099Z","shell.execute_reply.started":"2024-05-05T15:35:46.381308Z","shell.execute_reply":"2024-05-05T15:35:46.650301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}