<jupyter_start><jupyter_text>Get The Data To retrieve the data from kaggle via kaggle api, the kaggle credentials file should been in colab's virtual machine. In this project, the credentials file was in the drive. Therefore, the drive should be mounted to the virtual machine.<jupyter_code>from google.colab import drive
drive.mount("/content/drive")<jupyter_output>Mounted at /content/drive<jupyter_text>Than, copy the credentials file into the .kaggle folder where in the root folder. (Firstly you have to create that .kaggle folder in the root folder)<jupyter_code>!mkdir /root/.kaggle
!cp drive/MyDrive/kaggle/kaggle.json /root/.kaggle/
!kaggle competitions download -c digit-recognizer  # see the kaggle api documentation<jupyter_output>Warning: Looks like you're using an outdated API Version, please consider updating (server 1.5.10 / client 1.5.4)
Downloading train.csv.zip to /content
 55% 5.00M/9.16M [00:00<00:00, 10.3MB/s]
100% 9.16M/9.16M [00:00<00:00, 17.0MB/s]
Downloading test.csv.zip to /content
 82% 5.00M/6.09M [00:00<00:00, 16.6MB/s]
100% 6.09M/6.09M [00:00<00:00, 17.5MB/s]
Downloading sample_submission.csv to /content
  0% 0.00/235k [00:00<?, ?B/s]
100% 235k/235k [00:00<00:00, 66.2MB/s]<jupyter_text>As you might expect, the .csv files are extracting from the archives.<jupyter_code>!unzip test.csv.zip 
!unzip train.csv.zip<jupyter_output>Archive:  test.csv.zip
  inflating: test.csv                
Archive:  train.csv.zip
  inflating: train.csv<jupyter_text>Prepare The Data<jupyter_code>import pandas as pd
import numpy as np
train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")
train.head(5)
train_label = train["label"].copy()  # putting labels apart from the features.
train_features = train.drop(labels="label", axis=1) # putting features apart from the labels
X_train, X_valid = train_features[5000:], train_features[:5000]  # creating a validation set
y_train, y_valid = train_label[5000:], train_label[:5000]        # that composed of 5000 instances
X_train = X_train / 255    # since values are between 0 and 255, they can be normalized by dividing 255
X_valid = X_valid / 255    # this dividing operation ensures that whole values will be between 0 and 1
X_test = test / 255        # as another option, you can use sklearn.preprocessing.StandardScaler class
X_train.shape, X_valid.shape, X_test.shape<jupyter_output><empty_output><jupyter_text>This operation ends up with convert the sets from dataframe to numpy array and also reshape the sets to ("number of instances", 28, 28)<jupyter_code>X_train = np.array(X_train).reshape([X_train.shape[0], 28, 28])
X_valid = np.array(X_valid).reshape([X_valid.shape[0], 28, 28])
X_test = np.array(X_test).reshape([X_test.shape[0], 28, 28])<jupyter_output><empty_output><jupyter_text>Build a CNN Architecture<jupyter_code>from tensorflow import keras
model = keras.models.Sequential()  # creating a sequential model
model.add(keras.layers.Input(shape=X_train.shape[1:])) # an input layer with (28, 28) 
model.add(keras.layers.Conv1D(filters=64, kernel_size=4, activation="relu", padding="same"))
model.add(keras.layers.Conv1D(filters=128, kernel_size=4, activation="relu", padding="same"))
model.add(keras.layers.MaxPooling1D(1))
model.add(keras.layers.Conv1D(filters=256, kernel_size=2, activation="relu", padding="same"))
model.add(keras.layers.Conv1D(filters=128, kernel_size=2, activation="relu", padding="same"))
model.add(keras.layers.Conv1D(filters=64, kernel_size=2, activation="relu", padding="same"))
model.add(keras.layers.MaxPooling1D(1))
model.add(keras.layers.Flatten()) # to feed the dense layers, the inputs have to convert to flatten shape
model.add(keras.layers.Dense(units=32, activation="relu"))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(units=10, activation="softmax")) # since there are 10 classes, the dense layer
                                                              # has 10 nuerons as output neurons 
                                                              # with softmax activation function
model.compile(optimizer="nadam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) # self-explained
model.fit(x=X_train, y=y_train, epochs=10, validation_data=(X_valid, y_valid))<jupyter_output>Epoch 1/10
1157/1157 [==============================] - 20s 11ms/step - loss: 0.8782 - accuracy: 0.6977 - val_loss: 0.1108 - val_accuracy: 0.9690
Epoch 2/10
1157/1157 [==============================] - 11s 10ms/step - loss: 0.2736 - accuracy: 0.9160 - val_loss: 0.1027 - val_accuracy: 0.9748
Epoch 3/10
1157/1157 [==============================] - 11s 10ms/step - loss: 0.1971 - accuracy: 0.9374 - val_loss: 0.0659 - val_accuracy: 0.9812
Epoch 4/10
1157/1157 [==============================] - 11s 10ms/step - loss: 0.1652 - accuracy: 0.9447 - val_loss: 0.0520 - val_accuracy: 0.9862
Epoch 5/10
1157/1157 [==============================] - 11s 10ms/step - loss: 0.1502 - accuracy: 0.9494 - val_loss: 0.0720 - val_accuracy: 0.9816
Epoch 6/10
1157/1157 [==============================] - 11s 10ms/step - loss: 0.1376 - accuracy: 0.9533 - val_loss: 0.0811 - val_accuracy: 0.9822
Epoch 7/10
1157/1157 [==============================] - 11s 10ms/step - loss: 0.1278 - accuracy: 0.9589 - val_loss: 0.0602 -[...]<jupyter_text>Prediction and Submitting<jupyter_code>y_pred = model.predict(X_test)
predicted_classes = [prediction.argmax() for prediction in y_pred] # selecting the most possible class
# competition's submission procedures
submission = pd.DataFrame(predicted_classes, columns=["Label"])
submission.index.name = "ImageId"
submission.index += 1
submission.to_csv("submission.csv")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15828480677607198, 'fraction_numerical': 0.08117169578260103, 'mean_word_length': 4.760162601626016, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '160629', 'license': 'MIT', 'n_tokens_mistral': 2244, 'n_tokens_neox': 1918, 'n_words': 489, 'path': 'Digit Recognizer/solution.ipynb', 'repo_name': 'recep-yildirim/Kaggle', 'size': '5667'}