{"metadata":{"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"},"colab":{"provenance":[]},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":87232,"databundleVersionId":9912598,"sourceType":"competition"},{"sourceId":183669,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":156546,"modelId":178987}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport json\nimport re\nimport matplotlib.pyplot as plt\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense","metadata":{"id":"VdXf3LtU480Y"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_identity = pd.read_csv('/content/data_identification.csv')\ndf_emotion = pd.read_csv('/content/emotion.csv')\ndf_tweet = pd.read_json('/content/tweets_DM.json', lines=True)","metadata":{"id":"Yz3E8MQXj2q_"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_tweet_source = df_tweet[['_source']]\nprint(df_tweet_source.head())","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"1VHkyTI_sVkK","outputId":"6d019ab8-0d6b-4478-fb16-6d07bda7c4f4"},"outputs":[{"output_type":"stream","name":"stdout","text":["                                             _source","0  {'tweet': {'hashtags': ['Snapchat'], 'tweet_id...","1  {'tweet': {'hashtags': ['freepress', 'TrumpLeg...","2  {'tweet': {'hashtags': ['bibleverse'], 'tweet_...","3  {'tweet': {'hashtags': [], 'tweet_id': '0x1cd5...","4  {'tweet': {'hashtags': [], 'tweet_id': '0x2de2..."]}],"execution_count":null},{"cell_type":"code","source":"df_tweet['_source_str'] = df_tweet['_source'].apply(str)\ndf_tweet['_source_split'] = df_tweet['_source_str'].apply(lambda x: x.split(','))\nprint(df_tweet[['_source_split']].head())","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"k7kSBUrYsjtF","outputId":"30edd71c-c536-4f39-90db-60b7c9f7abb0"},"outputs":[{"output_type":"stream","name":"stdout","text":["                                       _source_split","0  [{'tweet': {'hashtags': ['Snapchat'],  'tweet_...","1  [{'tweet': {'hashtags': ['freepress',  'TrumpL...","2  [{'tweet': {'hashtags': ['bibleverse'],  'twee...","3  [{'tweet': {'hashtags': [],  'tweet_id': '0x1c...","4  [{'tweet': {'hashtags': [],  'tweet_id': '0x2d..."]}],"execution_count":null},{"cell_type":"code","source":"df_tweet['_source_cleaned'] = df_tweet['_source_str'].apply(lambda x: x.split(',', 1)[-1] if ',' in x else x)\nprint(df_tweet[['_source_cleaned']].head(20))","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"N3eo9AolsmqZ","outputId":"9aac26e1-32a0-4800-b930-da3a00ccc9ce"},"outputs":[{"output_type":"stream","name":"stdout","text":["                                      _source_cleaned","0    'tweet_id': '0x376b20', 'text': 'People who p...","1    'TrumpLegacy', 'CNN'], 'tweet_id': '0x2d5350'...","2    'tweet_id': '0x28b412', 'text': 'Confident of...","3    'tweet_id': '0x1cd5b0', 'text': 'Now ISSA is ...","4    'tweet_id': '0x2de201', 'text': '\"Trust is no...","5    'LaughOutLoud'], 'tweet_id': '0x1d755c', 'tex...","6    'tweet_id': '0x2c91a8', 'text': 'Still waitin...","7    'tweet_id': '0x368e95', 'text': 'Love knows n...","8    'tweet_id': '0x249c0c', 'text': '@DStvNgCare ...","9    'money', 'possessions'], 'tweet_id': '0x21844...","10   'gender', 'diversity'], 'tweet_id': '0x359db9...","11   'tweet_id': '0x23b037', 'text': \"I love suffe...","12   'tweet_id': '0x1fde89', 'text': 'Can someone ...","13   'ecology'], 'tweet_id': '0x37a0a9', 'text': '...","14   'tweet_id': '0x269112', 'text': \"My brother d...","15   'tweet_id': '0x360665', 'text': 'On a scale o...","16   'evatech', 'bendingcomposite', 'inovarsandton...","17   'tweet_id': '0x25be54', 'text': 'Vomi post bi...","18   'tweet_id': '0x33832f', 'text': 'One of my dr...","19   'tweet_id': '0x2e0b03', 'text': 'Thankful for..."]}],"execution_count":null},{"cell_type":"code","source":"import re\n\ndef extract_tweet_info(source_str):\n    # Regular expression to find 'tweet_id' and 'text' and their values\n    tweet_id_match = re.search(r\"'tweet_id':\\s*'([^']*)'\", source_str)\n    text_match = re.search(r\"'text':\\s*'([^']*)'\", source_str)\n\n    # Extract 'tweet_id' and 'text' if found\n    tweet_id = tweet_id_match.group(0) if tweet_id_match else None\n    text = text_match.group(0) if text_match else None\n\n    # Combine the results (only if both are found)\n    return ', '.join(filter(None, [tweet_id, text]))\n\n# Apply the function to the '_source' column\ndf_tweet['_source_cleaned'] = df_tweet['_source_str'].apply(extract_tweet_info)\n\n# Display the first few rows with the cleaned data\nprint(df_tweet[['_source_cleaned']].head())","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"fvtjD1v5s2ko","outputId":"d52b7737-6e79-493b-ad24-8a8697d53509"},"outputs":[{"output_type":"stream","name":"stdout","text":["                                     _source_cleaned","0  'tweet_id': '0x376b20', 'text': 'People who po...","1  'tweet_id': '0x2d5350', 'text': '@brianklaas A...","2  'tweet_id': '0x28b412', 'text': 'Confident of ...","3  'tweet_id': '0x1cd5b0', 'text': 'Now ISSA is s...","4  'tweet_id': '0x2de201', 'text': '\"Trust is not..."]}],"execution_count":null},{"cell_type":"code","source":"# Extract tweet_id and text into separate columns using regular expressions\ndf_tweet['tweet_id'] = df_tweet['_source_cleaned'].apply(lambda x: re.search(r\"'tweet_id':\\s*'([^']*)'\", x).group(1) if re.search(r\"'tweet_id':\\s*'([^']*)'\", x) else None)\ndf_tweet['text'] = df_tweet['_source_cleaned'].apply(lambda x: re.search(r\"'text':\\s*'([^']*)'\", x).group(1) if re.search(r\"'text':\\s*'([^']*)'\", x) else None)\ndf_tweet = df_tweet.drop(columns=['_source'])\n\n# Display the first few rows to verify the result\nprint(df_tweet[['tweet_id', 'text']].head())","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"mJO8gLO1s5V_","outputId":"1dda39e1-890f-4b04-8329-5a85dee9f89f"},"outputs":[{"output_type":"stream","name":"stdout","text":["   tweet_id                                               text","0  0x376b20  People who post \"add me on #Snapchat\" must be ...","1  0x2d5350  @brianklaas As we see, Trump is dangerous to #...","2  0x28b412  Confident of your obedience, I write to you, k...","3  0x1cd5b0                Now ISSA is stalking Tasha 😂😂😂 <LH>","4  0x2de201  \"Trust is not the same as faith. A friend is s..."]}],"execution_count":null},{"cell_type":"code","source":"# merge the datasets\ndf_identity['tweet_id'] = df_identity['tweet_id'].str.strip()\ndf_emotion['tweet_id'] = df_emotion['tweet_id'].str.strip()\ndf_tweet['tweet_id'] = df_tweet['tweet_id'].str.strip()\ndf_merged = pd.merge(df_identity, df_emotion, on='tweet_id', how='outer')\ndf_merged = pd.merge(df_merged, df_tweet, on='tweet_id', how='outer')\n\ndf_train = df_merged[df_merged['identification'] == 'train']\ndf_test = df_merged[df_merged['identification'] == 'test']","metadata":{"id":"AHCP8LHfj6Zb"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Drop unnecessary columns\ncolumns_to_drop = ['_crawldate', '_index', '_type', '_source_str', '_source_split', '_source_cleaned']\ndf_train = df_train.drop(columns=columns_to_drop, axis=1)\ndf_test = df_test.drop(columns=columns_to_drop, axis=1)","metadata":{"id":"wcM6SaWwj8-h"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# clean the 'NaN'\ndf_train['text'] = df_train['text'].fillna(\"\")\ndf_test['text'] = df_test['text'].fillna(\"\")","metadata":{"id":"HdkEKW92j_wq"},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BOW_vectorizer = CountVectorizer(max_features=500)  # use 500 features\nBOW_vectorizer.fit(df_train['text'])\n# feature extraction\nX_train = BOW_vectorizer.transform(df_train['text'])\nX_test = BOW_vectorizer.transform(df_test['text'])\n\ny_train = df_train['emotion']\n\n# label coding\nlabel_encoder = LabelEncoder()\ny_train_encoded = label_encoder.fit_transform(y_train)\n\n\ny_train = to_categorical(y_train_encoded)\n\n\nprint(f\"X_train shape: {X_train.shape}\")\nprint(f\"y_train shape: {y_train.shape}\")","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"dDkWHaT3kGbe","outputId":"2347eb79-ced9-42c0-847a-7423220f0637"},"outputs":[{"output_type":"stream","name":"stdout","text":["X_train shape: (1455563, 500)","y_train shape: (1455563, 8)"]}],"execution_count":null},{"cell_type":"code","source":"print(f\"X_test shape: {X_test.shape}\")","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"VCpgkR0IYmPd","outputId":"748dbbfc-7ec2-4d9f-a2ee-a3bd6abce62c"},"outputs":[{"output_type":"stream","name":"stdout","text":["X_test shape: (411972, 500)"]}],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# change the array to dense arrray, this step is added after an error occurred while training the model\nX_test_dense = X_test.toarray() if hasattr(X_test, 'toarray') else X_test\n\nprint(\"Any NaN in X_test:\", np.any(np.isnan(X_test_dense)))\nprint(\"Any Inf in X_test:\", np.any(np.isinf(X_test_dense)))","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"BWNe5gaKZf2a","outputId":"c76d0c8d-3d44-48f8-e462-e10989860f90"},"outputs":[{"output_type":"stream","name":"stdout","text":["Any NaN in X_test: False","Any Inf in X_test: False"]}],"execution_count":null},{"cell_type":"code","source":"# build the model\nmodel = Sequential()\nmodel.add(Dense(128, input_dim=X_train.shape[1], activation='relu'))  # X_train 的特徵數\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(y_train.shape[1], activation='softmax'))  # y_train 的列數對應於分類數\n\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"sLaJMOKWkNYz","outputId":"e10f2c03-3023-458b-b6b3-1def8ef658c2"},"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.","  super().__init__(activity_regularizer=activity_regularizer, **kwargs)"]}],"execution_count":null},{"cell_type":"code","source":"# train the model\nhistory = model.fit(\n    X_train,\n    y_train,\n    epochs=10,\n    batch_size=32,\n    validation_split=0.2,\n    verbose=1\n)\nimport os\n\n# path to save the model\nsave_dir = \"path_to_save_model\"\nos.makedirs(save_dir, exist_ok=True)  # 如果目錄不存在則創建\nmodel_filepath = os.path.join(save_dir, \"my_model.keras\")\n\nmodel.save(model_filepath)\nprint(f\"模型已保存至: {model_filepath}\")\n","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"MabMuEmikRyT","outputId":"2e33f392-796a-45eb-8203-b4fa9fd5e914"},"outputs":[{"output_type":"stream","name":"stdout","text":["Epoch 1/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m200s\u001b[0m 5ms/step - accuracy: 0.4410 - loss: 1.5313 - val_accuracy: 0.4606 - val_loss: 1.4771","Epoch 2/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m199s\u001b[0m 5ms/step - accuracy: 0.4647 - loss: 1.4628 - val_accuracy: 0.4629 - val_loss: 1.4674","Epoch 3/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m190s\u001b[0m 5ms/step - accuracy: 0.4694 - loss: 1.4476 - val_accuracy: 0.4636 - val_loss: 1.4655","Epoch 4/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m191s\u001b[0m 5ms/step - accuracy: 0.4739 - loss: 1.4375 - val_accuracy: 0.4646 - val_loss: 1.4632","Epoch 5/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m188s\u001b[0m 5ms/step - accuracy: 0.4756 - loss: 1.4314 - val_accuracy: 0.4647 - val_loss: 1.4669","Epoch 6/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m202s\u001b[0m 5ms/step - accuracy: 0.4782 - loss: 1.4257 - val_accuracy: 0.4652 - val_loss: 1.4654","Epoch 7/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m205s\u001b[0m 5ms/step - accuracy: 0.4797 - loss: 1.4224 - val_accuracy: 0.4649 - val_loss: 1.4677","Epoch 8/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m199s\u001b[0m 5ms/step - accuracy: 0.4807 - loss: 1.4202 - val_accuracy: 0.4647 - val_loss: 1.4694","Epoch 9/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m201s\u001b[0m 5ms/step - accuracy: 0.4816 - loss: 1.4167 - val_accuracy: 0.4647 - val_loss: 1.4681","Epoch 10/10","\u001b[1m36390/36390\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m192s\u001b[0m 5ms/step - accuracy: 0.4827 - loss: 1.4140 - val_accuracy: 0.4634 - val_loss: 1.4735","模型已保存至: path_to_save_model/my_model.keras"]}],"execution_count":null},{"cell_type":"code","source":"try:\n    y_pred = model.predict(X_test_dense)\nexcept Exception as e:\n    print(\"Error during prediction:\", e)\n    print(\"Ignoring problematic rows.\")\n    # exclude the data that has error, and only use the remaining datas\n    valid_indices = np.arange(X_test_dense.shape[0] - 11)\n    y_pred = model.predict(X_test_dense[valid_indices])\ny_pred_classes = label_encoder.inverse_transform(y_pred.argmax(axis=1))  # 解碼預測標籤\n\n# prediction output\ndf_test['predicted_emotion'] = y_pred_classes\ndf_test.to_csv('test_predictions.csv', index=False)","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"zQPPpa9-nb6B","outputId":"063d50fd-e47b-40bd-cb28-54a81b75ce05"},"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[1m12875/12875\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m21s\u001b[0m 2ms/step"]}],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":248},"id":"TUYVkexrR3ip","outputId":"5f824e6e-914d-4e1b-b820-d062e1492792"},"outputs":[{"output_type":"display_data","data":{"text/plain":["\u001b[1mModel: \"sequential\"\u001b[0m"],"text/html":["<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">Model: \"sequential\"</span>","</pre>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓","┃\u001b[1m \u001b[0m\u001b[1mLayer (type)                        \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape               \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m        Param #\u001b[0m\u001b[1m \u001b[0m┃","┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩","│ dense (\u001b[38;5;33mDense\u001b[0m)                        │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m128\u001b[0m)                 │          \u001b[38;5;34m64,128\u001b[0m │","├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤","│ dense_1 (\u001b[38;5;33mDense\u001b[0m)                      │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m64\u001b[0m)                  │           \u001b[38;5;34m8,256\u001b[0m │","├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤","│ dense_2 (\u001b[38;5;33mDense\u001b[0m)                      │ (\u001b[38;5;45mNone\u001b[0m, \u001b[38;5;34m8\u001b[0m)                   │             \u001b[38;5;34m520\u001b[0m │","└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘"],"text/html":["<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓","┃<span style=\"font-weight: bold\"> Layer (type)                         </span>┃<span style=\"font-weight: bold\"> Output Shape                </span>┃<span style=\"font-weight: bold\">         Param # </span>┃","┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩","│ dense (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>)                        │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">128</span>)                 │          <span style=\"color: #00af00; text-decoration-color: #00af00\">64,128</span> │","├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤","│ dense_1 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>)                      │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">64</span>)                  │           <span style=\"color: #00af00; text-decoration-color: #00af00\">8,256</span> │","├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤","│ dense_2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>)                      │ (<span style=\"color: #00d7ff; text-decoration-color: #00d7ff\">None</span>, <span style=\"color: #00af00; text-decoration-color: #00af00\">8</span>)                   │             <span style=\"color: #00af00; text-decoration-color: #00af00\">520</span> │","└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘","</pre>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["\u001b[1m Total params: \u001b[0m\u001b[38;5;34m218,714\u001b[0m (854.36 KB)"],"text/html":["<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Total params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">218,714</span> (854.36 KB)","</pre>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m72,904\u001b[0m (284.78 KB)"],"text/html":["<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">72,904</span> (284.78 KB)","</pre>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)"],"text/html":["<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Non-trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (0.00 B)","</pre>"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["\u001b[1m Optimizer params: \u001b[0m\u001b[38;5;34m145,810\u001b[0m (569.57 KB)"],"text/html":["<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Optimizer params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">145,810</span> (569.57 KB)","</pre>"]},"metadata":{}}],"execution_count":null},{"cell_type":"code","source":"print(df_test.head )","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"73U1riQ_i8nG","outputId":"862f30a6-6123-410a-dc15-19ef78178eda"},"outputs":[{"output_type":"stream","name":"stdout","text":["<bound method NDFrame.head of          tweet_id identification emotion  _score  \\","0        0x1c7f0f           test     NaN      62   ","3        0x1c7f12           test     NaN     756   ","4        0x1c7f13           test     NaN     213   ","8        0x1c7f17           test     NaN     603   ","9        0x1c7f18           test     NaN     609   ","...           ...            ...     ...     ...   ","1867509  0x38fe04           test     NaN     497   ","1867511  0x38fe06           test     NaN     187   ","1867524  0x38fe13           test     NaN     139   ","1867525  0x38fe14           test     NaN     251   ","1867532  0x38fe1b           test     NaN     611   ","","                                                      text predicted_emotion  ","0                                                                        joy  ","3                                                                        joy  ","4        The only “big plan” you ever had in your life,...      anticipation  ","8        Looking back on situations old & new, recent o...               joy  ","9        @jasoninthehouse Why do you insist on talking ...           disgust  ","...                                                    ...               ...  ","1867509  \"The Grand Bargain\" The Great American Betraya...           sadness  ","1867511                                                                  joy  ","1867524                                                                  joy  ","1867525  I think @kostakoufos might be the worst player...               joy  ","1867532                                                                  joy  ","","[411972 rows x 6 columns]>"]}],"execution_count":null},{"cell_type":"code","source":"\n\n# only keep tweet id and emotion\ndf_test_final = df_test[['tweet_id', 'predicted_emotion']]\n\n# save to csv\ndf_test_final.to_csv('test_predictions.csv', index=False)\n\nprint(df_test_final.head())\n# change the name of head\ndf_test_final.columns = ['id', 'emotion']\n\ndf_test_final.to_csv('test_predictions.csv', index=False)\n\nprint(df_test_final.head())","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"EOt5gBbBjmgy","outputId":"4a3a3345-4675-45f6-8657-745e7ae5e886"},"outputs":[{"output_type":"stream","name":"stdout","text":["   tweet_id predicted_emotion","0  0x1c7f0f               joy","3  0x1c7f12               joy","4  0x1c7f13      anticipation","8  0x1c7f17               joy","9  0x1c7f18           disgust","         id       emotion","0  0x1c7f0f           joy","3  0x1c7f12           joy","4  0x1c7f13  anticipation","8  0x1c7f17           joy","9  0x1c7f18       disgust"]}],"execution_count":null},{"cell_type":"code","source":"# plot accuracy curve\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Accuracy Over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()\n\n# plot loss curve\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Loss Over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"id":"2aaVON5MkYgQ"},"outputs":[],"execution_count":null}]}