{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport csv\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-01-07T20:17:47.219714Z","iopub.execute_input":"2023-01-07T20:17:47.220549Z","iopub.status.idle":"2023-01-07T20:17:47.254497Z","shell.execute_reply.started":"2023-01-07T20:17:47.220438Z","shell.execute_reply":"2023-01-07T20:17:47.252970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\")\n\ntrain = train_df\n\ntest = test_df","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:19:09.747784Z","iopub.execute_input":"2023-01-07T20:19:09.748485Z","iopub.status.idle":"2023-01-07T20:19:17.768111Z","shell.execute_reply.started":"2023-01-07T20:19:09.748383Z","shell.execute_reply":"2023-01-07T20:19:17.767091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:19:21.133207Z","iopub.execute_input":"2023-01-07T20:19:21.134378Z","iopub.status.idle":"2023-01-07T20:19:21.482022Z","shell.execute_reply.started":"2023-01-07T20:19:21.134325Z","shell.execute_reply":"2023-01-07T20:19:21.480980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:19:24.333579Z","iopub.execute_input":"2023-01-07T20:19:24.333971Z","iopub.status.idle":"2023-01-07T20:19:24.607823Z","shell.execute_reply.started":"2023-01-07T20:19:24.333939Z","shell.execute_reply":"2023-01-07T20:19:24.606645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Select the string column\nstring_column = train_df['f_27']\n\n# Transform the strings into ASCII values\nascii_strings = string_column.apply(lambda x: ''.join([str(ord(c)) for c in x]))\n\n# Update the original column with the ASCII strings\ntrain_df['f_27'] = ascii_strings\n","metadata":{"execution":{"iopub.status.busy":"2023-01-06T15:08:10.047046Z","iopub.execute_input":"2023-01-06T15:08:10.047486Z","iopub.status.idle":"2023-01-06T15:08:13.230488Z","shell.execute_reply.started":"2023-01-06T15:08:10.047448Z","shell.execute_reply":"2023-01-06T15:08:13.229433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-01-06T14:35:44.687352Z","iopub.execute_input":"2023-01-06T14:35:44.688113Z","iopub.status.idle":"2023-01-06T14:35:44.900192Z","shell.execute_reply.started":"2023-01-06T14:35:44.688045Z","shell.execute_reply":"2023-01-06T14:35:44.898993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select the string column\nstring_column = test_df['f_27']\n\n# Transform the strings into ASCII values\nascii_strings = string_column.apply(lambda x: ''.join([str(ord(c)) for c in x]))\n\n# Update the original column with the ASCII strings\ntest_df['f_27'] = ascii_strings","metadata":{"execution":{"iopub.status.busy":"2023-01-06T15:08:16.393681Z","iopub.execute_input":"2023-01-06T15:08:16.394071Z","iopub.status.idle":"2023-01-06T15:08:18.786020Z","shell.execute_reply.started":"2023-01-06T15:08:16.394039Z","shell.execute_reply":"2023-01-06T15:08:18.784870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-01-06T14:35:53.055939Z","iopub.execute_input":"2023-01-06T14:35:53.056509Z","iopub.status.idle":"2023-01-06T14:35:53.230370Z","shell.execute_reply.started":"2023-01-06T14:35:53.056469Z","shell.execute_reply":"2023-01-06T14:35:53.228989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transforming the f_27 column into 10 seperate columns each column bears the orther of the letter\nfrom sklearn.preprocessing import OrdinalEncoder\nenc = OrdinalEncoder()\ndef feature_eng(df):\n    df=df.copy()\n    df['char_unique']=df['f_27'].apply(lambda x: len(set(x)))\n    for i in range(df.f_27.str.len().max()):\n        df['f_27_char{}'.format(i+1)]=enc.fit_transform(df['f_27'].str.get(i).values.reshape(-1,1))\n    return df.drop(['f_27'],axis=1)\n\ntrain_df=feature_eng(df=train)\ntest_df=feature_eng(df=test)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:22:52.118601Z","iopub.execute_input":"2023-01-07T20:22:52.119027Z","iopub.status.idle":"2023-01-07T20:23:07.845100Z","shell.execute_reply.started":"2023-01-07T20:22:52.118993Z","shell.execute_reply":"2023-01-07T20:23:07.843880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:23:47.699610Z","iopub.execute_input":"2023-01-07T20:23:47.700022Z","iopub.status.idle":"2023-01-07T20:23:47.954830Z","shell.execute_reply.started":"2023-01-07T20:23:47.699989Z","shell.execute_reply":"2023-01-07T20:23:47.953822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:24:00.503961Z","iopub.execute_input":"2023-01-07T20:24:00.504539Z","iopub.status.idle":"2023-01-07T20:24:00.724680Z","shell.execute_reply.started":"2023-01-07T20:24:00.504497Z","shell.execute_reply":"2023-01-07T20:24:00.723836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.ensemble import RandomForestRegressor\n# from sklearn.model_selection import train_test_split\n\n# Select features and target variable\n# X = train_df.drop(columns=['id','target'])\n# y = train_df['target']\n# \n# Split data into training and test sets\n# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Fit random forest model\n# model = RandomForestRegressor()\n# model.fit(X_train, y_train)\n\n# Score model on test data\n# score = model.score(X_test, y_test)\n# print(f'Test score: {score:.2f}')","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:37:08.861335Z","iopub.status.idle":"2023-01-03T19:37:08.863047Z","shell.execute_reply.started":"2023-01-03T19:37:08.862646Z","shell.execute_reply":"2023-01-03T19:37:08.862683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predictions = model.predict(X_test)\n# sub = pd.DataFrame({'target': predictions})\n# sub.to_csv('subResult.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.ensemble import RandomForestRegressor\n#from sklearn.model_selection import train_test_split\n#from sklearn.metrics import mean_absolute_error\n\n# Select most imporatant features and target variable\n#X = train_df[['f_26', 'f_21', 'f_22', 'f_30']]\n#y = train_df['target']\n\n# Split data into training and test sets\n#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Fit random forest model\n#model = RandomForestRegressor()\n#model.fit(X_train, y_train)\n\n# Score model on test data\n#predictions = model.predict(X_test)\n#score = mean_absolute_error(y_test, predictions)\n#print(f'Test score: {score:.2f}')\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T21:27:33.195491Z","iopub.execute_input":"2023-01-03T21:27:33.195992Z","iopub.status.idle":"2023-01-03T21:39:04.358341Z","shell.execute_reply.started":"2023-01-03T21:27:33.195951Z","shell.execute_reply":"2023-01-03T21:39:04.356765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predictions = model.predict(X_test)\n#sub = pd.DataFrame({'target': predictions})\n#sub.to_csv('subTraining.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:07:37.000464Z","iopub.execute_input":"2023-01-03T22:07:37.001005Z","iopub.status.idle":"2023-01-03T22:07:51.669758Z","shell.execute_reply.started":"2023-01-03T22:07:37.000965Z","shell.execute_reply":"2023-01-03T22:07:51.668355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sub\n#print(\"Training score :\", model.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:11:45.485302Z","iopub.execute_input":"2023-01-03T22:11:45.485811Z","iopub.status.idle":"2023-01-03T22:12:44.369385Z","shell.execute_reply.started":"2023-01-03T22:11:45.485760Z","shell.execute_reply":"2023-01-03T22:12:44.367969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select features\n#T_test = test_df[['f_26','f_21','f_22','f_30']]\n\n# Make predictions on test data\n#predictions = model.predict(T_test)\n\n# Create a dataframe with the predictions\n#sub2 = pd.DataFrame({'target': predictions})\n\n# Save the predictions to a CSV file\n#sub2.to_csv('subResult.csv', index=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:35:48.128530Z","iopub.execute_input":"2023-01-03T22:35:48.129261Z","iopub.status.idle":"2023-01-03T22:36:48.225181Z","shell.execute_reply.started":"2023-01-03T22:35:48.129202Z","shell.execute_reply":"2023-01-03T22:36:48.223898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sub2","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:36:51.368313Z","iopub.execute_input":"2023-01-03T22:36:51.369822Z","iopub.status.idle":"2023-01-03T22:36:51.383729Z","shell.execute_reply.started":"2023-01-03T22:36:51.369747Z","shell.execute_reply":"2023-01-03T22:36:51.382858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sub2 = pd.DataFrame({'id': test_df['id'], 'target': predictions})\n\n# Save the dataframe to a CSV file\n#sub2.to_csv('finalSubmition.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:42:53.143366Z","iopub.execute_input":"2023-01-03T22:42:53.144119Z","iopub.status.idle":"2023-01-03T22:42:54.171725Z","shell.execute_reply.started":"2023-01-03T22:42:53.144078Z","shell.execute_reply":"2023-01-03T22:42:54.170118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sub2","metadata":{"execution":{"iopub.status.busy":"2023-01-03T22:43:03.008872Z","iopub.execute_input":"2023-01-03T22:43:03.010810Z","iopub.status.idle":"2023-01-03T22:43:03.029275Z","shell.execute_reply.started":"2023-01-03T22:43:03.010601Z","shell.execute_reply":"2023-01-03T22:43:03.027872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\n\n# Select all features and target variable\nX = train_df.drop(columns=['id', 'target'])\ny = train_df['target']\n\n# Split data into training and test sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Fit random forest model\nmodel = RandomForestRegressor()\nmodel.fit(X_train, y_train)\n\n# Score model on test data\nscore = model.score(X_test, y_test)\nprint(f'Test score: {score:.2f}')\npredictions = model.predict(X_test)\nsub = pd.DataFrame({'target': predictions})\nsub.to_csv('subResult.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:24:21.003112Z","iopub.execute_input":"2023-01-07T20:24:21.003504Z","iopub.status.idle":"2023-01-07T21:20:05.189693Z","shell.execute_reply.started":"2023-01-07T20:24:21.003473Z","shell.execute_reply":"2023-01-07T21:20:05.188493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub\nprint(\"Training score :\", model.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:20:31.976016Z","iopub.execute_input":"2023-01-07T21:20:31.976408Z","iopub.status.idle":"2023-01-07T21:21:08.332719Z","shell.execute_reply.started":"2023-01-07T21:20:31.976378Z","shell.execute_reply":"2023-01-07T21:21:08.330998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Select features from test data\nX_test = test_df.drop(columns=['id'])\n\n# Make predictions on test data\npredictions = model.predict(X_test)\n\n# Create a dataframe with the predictions\nsubf = pd.DataFrame({'target': predictions})\n\n# Save the dataframe as a CSV file\nsubf.to_csv('finalSubmition_v2.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:21:20.355691Z","iopub.execute_input":"2023-01-07T21:21:20.356075Z","iopub.status.idle":"2023-01-07T21:21:55.655691Z","shell.execute_reply.started":"2023-01-07T21:21:20.356045Z","shell.execute_reply":"2023-01-07T21:21:55.654616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subf = pd.DataFrame({'id': test_df['id'], 'target': predictions})\n\n# Save the dataframe to a CSV file\nsubf.to_csv('anotherOne.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:22:39.517026Z","iopub.execute_input":"2023-01-07T21:22:39.517467Z","iopub.status.idle":"2023-01-07T21:22:40.546532Z","shell.execute_reply.started":"2023-01-07T21:22:39.517417Z","shell.execute_reply":"2023-01-07T21:22:40.545341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subf","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:22:44.335794Z","iopub.execute_input":"2023-01-07T21:22:44.336228Z","iopub.status.idle":"2023-01-07T21:22:44.349122Z","shell.execute_reply.started":"2023-01-07T21:22:44.336193Z","shell.execute_reply":"2023-01-07T21:22:44.348211Z"},"trusted":true},"execution_count":null,"outputs":[]}]}