{"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":"raw","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"}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Flatten\nfrom keras.layers import Dense,BatchNormalization, Dropout,MaxPool2D,LSTM","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:42:57.936037Z","iopub.execute_input":"2023-06-01T06:42:57.937165Z","iopub.status.idle":"2023-06-01T06:43:13.875317Z","shell.execute_reply.started":"2023-06-01T06:42:57.937124Z","shell.execute_reply":"2023-06-01T06:43:13.874092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:13.877325Z","iopub.execute_input":"2023-06-01T06:43:13.878243Z","iopub.status.idle":"2023-06-01T06:43:13.885082Z","shell.execute_reply.started":"2023-06-01T06:43:13.878185Z","shell.execute_reply":"2023-06-01T06:43:13.883659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nfashion_mnist = tf.keras.datasets.mnist.load_data()\n(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist\nX_train, y_train = X_train_full[:-5000], y_train_full[:-5000]\nX_valid, y_valid = X_train_full[-5000:], y_train_full[-5000:]","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:13.886949Z","iopub.execute_input":"2023-06-01T06:43:13.888112Z","iopub.status.idle":"2023-06-01T06:43:15.236224Z","shell.execute_reply.started":"2023-06-01T06:43:13.888070Z","shell.execute_reply":"2023-06-01T06:43:15.235037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(set(y_train_full))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:15.240574Z","iopub.execute_input":"2023-06-01T06:43:15.241025Z","iopub.status.idle":"2023-06-01T06:43:15.256552Z","shell.execute_reply.started":"2023-06-01T06:43:15.240980Z","shell.execute_reply":"2023-06-01T06:43:15.255367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[3], cmap=\"binary\")\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:15.258345Z","iopub.execute_input":"2023-06-01T06:43:15.259151Z","iopub.status.idle":"2023-06-01T06:43:15.382029Z","shell.execute_reply.started":"2023-06-01T06:43:15.259108Z","shell.execute_reply":"2023-06-01T06:43:15.380527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid, X_test = X_train / 255., X_valid / 255., X_test / 255.","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:15.388352Z","iopub.execute_input":"2023-06-01T06:43:15.389335Z","iopub.status.idle":"2023-06-01T06:43:15.566974Z","shell.execute_reply.started":"2023-06-01T06:43:15.389277Z","shell.execute_reply":"2023-06-01T06:43:15.565757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:15.568570Z","iopub.execute_input":"2023-06-01T06:43:15.568985Z","iopub.status.idle":"2023-06-01T06:43:15.579243Z","shell.execute_reply.started":"2023-06-01T06:43:15.568928Z","shell.execute_reply":"2023-06-01T06:43:15.577928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(y_train_full)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:15.580938Z","iopub.execute_input":"2023-06-01T06:43:15.581827Z","iopub.status.idle":"2023-06-01T06:43:15.600402Z","shell.execute_reply.started":"2023-06-01T06:43:15.581785Z","shell.execute_reply":"2023-06-01T06:43:15.599188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classifier = Sequential()\n# classifier.add(Conv2D(32, (3, 3), input_shape = (28,28,1), activation = 'relu'))\n# classifier.add(MaxPooling2D(pool_size = (2, 2)))\n# classifier.add(Conv2D(64, (3, 3),  activation = 'relu'))\n# classifier.add(MaxPooling2D(pool_size = (2, 2)))\n# classifier.add(Conv2D(64, (3, 3),  activation = 'relu'))\n# classifier.add(MaxPooling2D(pool_size = (2, 2)))\n# classifier.add(Flatten())9\n# classifier.add(Dense(units = 128, activation = 'relu'))\n# classifier.add(Dense(units = 10, activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:15.602120Z","iopub.execute_input":"2023-06-01T06:43:15.602502Z","iopub.status.idle":"2023-06-01T06:43:15.611984Z","shell.execute_reply.started":"2023-06-01T06:43:15.602461Z","shell.execute_reply":"2023-06-01T06:43:15.610588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classifier = Sequential()\n\n# classifier.add(Conv2D(filters=32, kernel_size=(5,5), kernel_initializer='he_uniform', padding='Same', activation='relu', input_shape=(28,28,1)))\n# classifier.add(BatchNormalization())\n# classifier.add(MaxPool2D(pool_size=(2,2)))\n\n# classifier.add(Conv2D(filters=64, kernel_size=(5,5), kernel_initializer='he_uniform', padding='Same', activation='relu'))\n# classifier.add(BatchNormalization())\n# classifier.add(MaxPool2D(pool_size=(2,2)))\n\n# classifier.add(Conv2D(filters=128, kernel_size=(5,5), kernel_initializer='he_uniform', padding='Same', activation='relu'))\n# classifier.add(BatchNormalization())\n# classifier.add(MaxPool2D(pool_size=(2,2)))\n\n\n# classifier.add(Flatten())\n# classifier.add(Dense(128, kernel_initializer='he_uniform', activation='relu'))\n# classifier.add(BatchNormalization())\n# classifier.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:15.613330Z","iopub.execute_input":"2023-06-01T06:43:15.613683Z","iopub.status.idle":"2023-06-01T06:43:15.624682Z","shell.execute_reply.started":"2023-06-01T06:43:15.613630Z","shell.execute_reply":"2023-06-01T06:43:15.623351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelr=Sequential()\nmodelr.add(LSTM(300,input_shape=(28,28),activation=\"relu\",return_sequences=True))\nmodelr.add(LSTM(100,activation=\"relu\"))\nmodelr.add(Dense(10,activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:15.626353Z","iopub.execute_input":"2023-06-01T06:43:15.626872Z","iopub.status.idle":"2023-06-01T06:43:21.273442Z","shell.execute_reply.started":"2023-06-01T06:43:15.626724Z","shell.execute_reply":"2023-06-01T06:43:21.272225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classifier.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:21.274933Z","iopub.execute_input":"2023-06-01T06:43:21.275331Z","iopub.status.idle":"2023-06-01T06:43:21.283370Z","shell.execute_reply.started":"2023-06-01T06:43:21.275292Z","shell.execute_reply":"2023-06-01T06:43:21.282030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classifier.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:21.288563Z","iopub.execute_input":"2023-06-01T06:43:21.289620Z","iopub.status.idle":"2023-06-01T06:43:21.294527Z","shell.execute_reply.started":"2023-06-01T06:43:21.289579Z","shell.execute_reply":"2023-06-01T06:43:21.293140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelr.compile(loss=\"sparse_categorical_crossentropy\",optimizer=\"adam\",metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:21.296542Z","iopub.execute_input":"2023-06-01T06:43:21.296916Z","iopub.status.idle":"2023-06-01T06:43:21.320493Z","shell.execute_reply.started":"2023-06-01T06:43:21.296876Z","shell.execute_reply":"2023-06-01T06:43:21.319547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(modelr, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:21.322013Z","iopub.execute_input":"2023-06-01T06:43:21.322373Z","iopub.status.idle":"2023-06-01T06:43:21.695376Z","shell.execute_reply.started":"2023-06-01T06:43:21.322337Z","shell.execute_reply":"2023-06-01T06:43:21.694002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classifier.fit(X_train,y_train,epochs = 60,validation_data = (X_valid,y_valid))","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:30.463887Z","iopub.execute_input":"2023-06-01T06:43:30.465020Z","iopub.status.idle":"2023-06-01T06:43:30.470454Z","shell.execute_reply.started":"2023-06-01T06:43:30.464971Z","shell.execute_reply":"2023-06-01T06:43:30.469007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.svm import SVC\n# from sklearn.model_selection import cross_val_score\n\n# svm_clf = SVC(random_state=60)\n# svm_clf_crossval_score = cross_val_score(svm_clf, X_train, y_train, cv=3, scoring=\"accuracy\",verbose=10)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:30.687890Z","iopub.execute_input":"2023-06-01T06:43:30.688626Z","iopub.status.idle":"2023-06-01T06:43:30.693296Z","shell.execute_reply.started":"2023-06-01T06:43:30.688586Z","shell.execute_reply":"2023-06-01T06:43:30.691926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# svm_clf.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:30.917484Z","iopub.execute_input":"2023-06-01T06:43:30.918640Z","iopub.status.idle":"2023-06-01T06:43:30.923467Z","shell.execute_reply.started":"2023-06-01T06:43:30.918597Z","shell.execute_reply":"2023-06-01T06:43:30.922029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelr.fit(X_train,y_train,batch_size=64,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T06:43:31.287611Z","iopub.execute_input":"2023-06-01T06:43:31.288768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_set.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = test_set.values.astype(\"float32\")\ntest_set=test_set.reshape((28000,28,28))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predictions_test = classifier.predict(test_set)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predictions_test = classifier.predict(test_set)\npredictions_test = modelr.predict(test_set)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_test[:10]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId = []\nLabel = []\nfor i in range(len(predictions_test)):\n    ImageId.append(i+1)\n    Label.append(predictions_test[i].argmax())\n    \nsubmissions=pd.DataFrame({\"ImageId\": ImageId,\n                         \"Label\": Label})\nsubmissions.to_csv(\"subm.csv\", index=False, header=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}