{"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":"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 pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nimport numpy as np\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.callbacks import EarlyStopping\n\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":"2023-08-17T07:46:17.933401Z","iopub.execute_input":"2023-08-17T07:46:17.934098Z","iopub.status.idle":"2023-08-17T07:46:17.945907Z","shell.execute_reply.started":"2023-08-17T07:46:17.934050Z","shell.execute_reply":"2023-08-17T07:46:17.944144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/chat-gpt-generated-text-detection/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/chat-gpt-generated-text-detection/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:17.948357Z","iopub.execute_input":"2023-08-17T07:46:17.948792Z","iopub.status.idle":"2023-08-17T07:46:19.590387Z","shell.execute_reply.started":"2023-08-17T07:46:17.948760Z","shell.execute_reply":"2023-08-17T07:46:19.588766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:19.593221Z","iopub.execute_input":"2023-08-17T07:46:19.593948Z","iopub.status.idle":"2023-08-17T07:46:19.606847Z","shell.execute_reply.started":"2023-08-17T07:46:19.593907Z","shell.execute_reply":"2023-08-17T07:46:19.605682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing the data","metadata":{}},{"cell_type":"code","source":"vectorizer = TfidfVectorizer(max_features=1000)  # Adjust max_features as needed\nX_train = vectorizer.fit_transform(train_data['text'])\nX_test = vectorizer.transform(test_data['text'])","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:19.608295Z","iopub.execute_input":"2023-08-17T07:46:19.608665Z","iopub.status.idle":"2023-08-17T07:46:44.718149Z","shell.execute_reply.started":"2023-08-17T07:46:19.608634Z","shell.execute_reply":"2023-08-17T07:46:44.716840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert sparse matrix to dense array","metadata":{}},{"cell_type":"code","source":"X_train = X_train.toarray()\nX_test = X_test.toarray()","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:44.721569Z","iopub.execute_input":"2023-08-17T07:46:44.722056Z","iopub.status.idle":"2023-08-17T07:46:45.871978Z","shell.execute_reply.started":"2023-08-17T07:46:44.722015Z","shell.execute_reply":"2023-08-17T07:46:45.870600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Map author to binary labels","metadata":{}},{"cell_type":"code","source":"author_map = {\"Chatgpt\": 0, \"person\": 1}\ntrain_data['author'] = train_data['author'].map(author_map)\ny_train = train_data['author']","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:45.874152Z","iopub.execute_input":"2023-08-17T07:46:45.874518Z","iopub.status.idle":"2023-08-17T07:46:45.894692Z","shell.execute_reply.started":"2023-08-17T07:46:45.874489Z","shell.execute_reply":"2023-08-17T07:46:45.893621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split the dataset","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:45.896562Z","iopub.execute_input":"2023-08-17T07:46:45.897366Z","iopub.status.idle":"2023-08-17T07:46:46.250367Z","shell.execute_reply.started":"2023-08-17T07:46:45.897320Z","shell.execute_reply":"2023-08-17T07:46:46.249071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build the Model","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(128, activation='relu', input_shape=(X_train.shape[1],)))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:46.253375Z","iopub.execute_input":"2023-08-17T07:46:46.254178Z","iopub.status.idle":"2023-08-17T07:46:46.319928Z","shell.execute_reply.started":"2023-08-17T07:46:46.254133Z","shell.execute_reply":"2023-08-17T07:46:46.318455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:46.323256Z","iopub.execute_input":"2023-08-17T07:46:46.323963Z","iopub.status.idle":"2023-08-17T07:46:46.337473Z","shell.execute_reply.started":"2023-08-17T07:46:46.323919Z","shell.execute_reply":"2023-08-17T07:46:46.336182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Early stopping to prevent overfitting","metadata":{}},{"cell_type":"code","source":"early_stopping = EarlyStopping(patience=2, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:46.339194Z","iopub.execute_input":"2023-08-17T07:46:46.341254Z","iopub.status.idle":"2023-08-17T07:46:46.347363Z","shell.execute_reply.started":"2023-08-17T07:46:46.341134Z","shell.execute_reply":"2023-08-17T07:46:46.345683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train the Model","metadata":{}},{"cell_type":"code","source":"model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, batch_size=64, callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:46:46.353108Z","iopub.execute_input":"2023-08-17T07:46:46.353578Z","iopub.status.idle":"2023-08-17T07:47:02.164940Z","shell.execute_reply.started":"2023-08-17T07:46:46.353540Z","shell.execute_reply":"2023-08-17T07:47:02.163671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:47:02.166630Z","iopub.execute_input":"2023-08-17T07:47:02.167128Z","iopub.status.idle":"2023-08-17T07:47:09.287699Z","shell.execute_reply.started":"2023-08-17T07:47:02.167057Z","shell.execute_reply":"2023-08-17T07:47:09.286202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert probabilities to binary predictions","metadata":{}},{"cell_type":"code","source":"binary_predictions = [0 if pred < 0.5 else 1 for pred in predictions]","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:47:09.289240Z","iopub.execute_input":"2023-08-17T07:47:09.289614Z","iopub.status.idle":"2023-08-17T07:47:09.328423Z","shell.execute_reply.started":"2023-08-17T07:47:09.289583Z","shell.execute_reply":"2023-08-17T07:47:09.327256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create submission DataFrame","metadata":{}},{"cell_type":"code","source":"submission_df = pd.DataFrame({'id': test_data['id'], 'author': binary_predictions})","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:47:09.329895Z","iopub.execute_input":"2023-08-17T07:47:09.331246Z","iopub.status.idle":"2023-08-17T07:47:09.342780Z","shell.execute_reply.started":"2023-08-17T07:47:09.331208Z","shell.execute_reply":"2023-08-17T07:47:09.341447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save submission","metadata":{}},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-17T07:47:09.344840Z","iopub.execute_input":"2023-08-17T07:47:09.345243Z","iopub.status.idle":"2023-08-17T07:47:40.271555Z","shell.execute_reply.started":"2023-08-17T07:47:09.345211Z","shell.execute_reply":"2023-08-17T07:47:40.269147Z"},"trusted":true},"execution_count":null,"outputs":[]}]}