{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":81933,"databundleVersionId":9643020,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\ntrain_data = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv', index_col='id')\ntest_data = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv', index_col='id')\ndict = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/data_dictionary.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:05:20.875007Z","iopub.execute_input":"2024-11-29T12:05:20.875408Z","iopub.status.idle":"2024-11-29T12:05:20.936690Z","shell.execute_reply.started":"2024-11-29T12:05:20.875366Z","shell.execute_reply":"2024-11-29T12:05:20.935557Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Porównanie datasetów","metadata":{}},{"cell_type":"code","source":"print(f'train_data {train_data.shape}')\nprint(f'test_data {test_data.shape}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:19:26.030935Z","iopub.execute_input":"2024-11-29T10:19:26.031314Z","iopub.status.idle":"2024-11-29T10:19:26.037573Z","shell.execute_reply.started":"2024-11-29T10:19:26.031279Z","shell.execute_reply":"2024-11-29T10:19:26.036215Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:14:26.956425Z","iopub.execute_input":"2024-11-29T10:14:26.956824Z","iopub.status.idle":"2024-11-29T10:14:26.965030Z","shell.execute_reply.started":"2024-11-29T10:14:26.956785Z","shell.execute_reply":"2024-11-29T10:14:26.963582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:16:26.068095Z","iopub.execute_input":"2024-11-29T10:16:26.068527Z","iopub.status.idle":"2024-11-29T10:16:26.077095Z","shell.execute_reply.started":"2024-11-29T10:16:26.068476Z","shell.execute_reply":"2024-11-29T10:16:26.075726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in train_data.columns: print(f'{i} {train_data[i].isnull().sum()}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:20:06.587720Z","iopub.execute_input":"2024-11-29T10:20:06.589087Z","iopub.status.idle":"2024-11-29T10:20:06.617121Z","shell.execute_reply.started":"2024-11-29T10:20:06.589024Z","shell.execute_reply":"2024-11-29T10:20:06.615719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in test_data.columns: print(f'{i} {test_data[i].isnull().sum()}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:20:10.034855Z","iopub.execute_input":"2024-11-29T10:20:10.035229Z","iopub.status.idle":"2024-11-29T10:20:10.051522Z","shell.execute_reply.started":"2024-11-29T10:20:10.035199Z","shell.execute_reply":"2024-11-29T10:20:10.050388Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Wypełnianie columny sii","metadata":{}},{"cell_type":"code","source":"nan_sii_data = train_data.loc[train_data['sii'].isnull()]\nsii_fit_data = train_data.loc[train_data['sii'] >= 0, :]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:05:37.145628Z","iopub.execute_input":"2024-11-29T12:05:37.146105Z","iopub.status.idle":"2024-11-29T12:05:37.158285Z","shell.execute_reply.started":"2024-11-29T12:05:37.146049Z","shell.execute_reply":"2024-11-29T12:05:37.156576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"nan_sii_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:20:57.423362Z","iopub.execute_input":"2024-11-29T10:20:57.423883Z","iopub.status.idle":"2024-11-29T10:20:57.466622Z","shell.execute_reply.started":"2024-11-29T10:20:57.423811Z","shell.execute_reply":"2024-11-29T10:20:57.465648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sii_fit_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:21:12.931164Z","iopub.execute_input":"2024-11-29T10:21:12.931691Z","iopub.status.idle":"2024-11-29T10:21:12.972667Z","shell.execute_reply.started":"2024-11-29T10:21:12.931636Z","shell.execute_reply":"2024-11-29T10:21:12.971411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sii_categorical_cols = [cname for cname in sii_fit_data.columns if sii_fit_data[cname].dtype == \"object\"]\nsii_numerical_cols = [cname for cname in sii_fit_data.columns[:-1] if sii_fit_data[cname].dtype in ['int64', 'float64']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:05:44.751404Z","iopub.execute_input":"2024-11-29T12:05:44.751836Z","iopub.status.idle":"2024-11-29T12:05:44.764496Z","shell.execute_reply.started":"2024-11-29T12:05:44.751795Z","shell.execute_reply":"2024-11-29T12:05:44.762716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.ensemble import RandomForestClassifier\n\nnumerical_transformer = SimpleImputer(strategy='mean')\n\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])\n\nsii_preprocessor = ColumnTransformer(transformers=[\n    ('num', numerical_transformer, sii_numerical_cols), \n    ('categorical', categorical_transformer, sii_categorical_cols)])\n\nsii_forest_model = RandomForestClassifier(n_estimators=50, random_state=0)\n\nsii_pipeline = Pipeline(steps=[('preprocess', sii_preprocessor), ('model', sii_forest_model)])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:05:47.119498Z","iopub.execute_input":"2024-11-29T12:05:47.119940Z","iopub.status.idle":"2024-11-29T12:05:47.128536Z","shell.execute_reply.started":"2024-11-29T12:05:47.119897Z","shell.execute_reply":"2024-11-29T12:05:47.127034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nsii_X = sii_fit_data.drop('sii', axis=1)\nsii_y = sii_fit_data.sii\n\nsii_X_train, sii_X_test, sii_y_train, sii_y_test = train_test_split(sii_X, sii_y, random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:05:51.735253Z","iopub.execute_input":"2024-11-29T12:05:51.735649Z","iopub.status.idle":"2024-11-29T12:05:51.748662Z","shell.execute_reply.started":"2024-11-29T12:05:51.735614Z","shell.execute_reply":"2024-11-29T12:05:51.746957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sii_pipeline.fit(sii_X_train, sii_y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:28:13.147961Z","iopub.execute_input":"2024-11-29T10:28:13.148368Z","iopub.status.idle":"2024-11-29T10:28:13.492287Z","shell.execute_reply.started":"2024-11-29T10:28:13.148330Z","shell.execute_reply":"2024-11-29T10:28:13.491149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nsii_pred = sii_pipeline.predict(sii_X_test)\n\nprint(f\"Accuracy is: {accuracy_score(sii_y_test, sii_pred)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:28:22.480962Z","iopub.execute_input":"2024-11-29T10:28:22.481374Z","iopub.status.idle":"2024-11-29T10:28:22.509148Z","shell.execute_reply.started":"2024-11-29T10:28:22.481329Z","shell.execute_reply":"2024-11-29T10:28:22.507941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sii_pred","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T10:30:24.099440Z","iopub.execute_input":"2024-11-29T10:30:24.100020Z","iopub.status.idle":"2024-11-29T10:30:24.119554Z","shell.execute_reply.started":"2024-11-29T10:30:24.099963Z","shell.execute_reply":"2024-11-29T10:30:24.118090Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sii_pipeline.fit(sii_X, sii_y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:01.495393Z","iopub.execute_input":"2024-11-29T12:06:01.495842Z","iopub.status.idle":"2024-11-29T12:06:02.023622Z","shell.execute_reply.started":"2024-11-29T12:06:01.495800Z","shell.execute_reply":"2024-11-29T12:06:02.021957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"nan_sii_data['sii'] = sii_pipeline.predict(nan_sii_data.drop('sii', axis=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:04.691185Z","iopub.execute_input":"2024-11-29T12:06:04.691629Z","iopub.status.idle":"2024-11-29T12:06:04.728710Z","shell.execute_reply.started":"2024-11-29T12:06:04.691588Z","shell.execute_reply":"2024-11-29T12:06:04.726593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"nan_sii_data['sii'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T11:53:39.150082Z","iopub.execute_input":"2024-11-29T11:53:39.150507Z","iopub.status.idle":"2024-11-29T11:53:39.159777Z","shell.execute_reply.started":"2024-11-29T11:53:39.150467Z","shell.execute_reply":"2024-11-29T11:53:39.158394Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data['sii'] = train_data['sii'].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:11.472325Z","iopub.execute_input":"2024-11-29T12:06:11.472798Z","iopub.status.idle":"2024-11-29T12:06:11.482516Z","shell.execute_reply.started":"2024-11-29T12:06:11.472757Z","shell.execute_reply":"2024-11-29T12:06:11.480792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T11:49:25.658493Z","iopub.execute_input":"2024-11-29T11:49:25.658913Z","iopub.status.idle":"2024-11-29T11:49:25.701627Z","shell.execute_reply.started":"2024-11-29T11:49:25.658855Z","shell.execute_reply":"2024-11-29T11:49:25.700481Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Przygotowanie danych","metadata":{}},{"cell_type":"code","source":"common_columns = set(train_data.columns) & set(test_data.columns)\ncommon_columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:15.711712Z","iopub.execute_input":"2024-11-29T12:06:15.712188Z","iopub.status.idle":"2024-11-29T12:06:15.723342Z","shell.execute_reply.started":"2024-11-29T12:06:15.712141Z","shell.execute_reply":"2024-11-29T12:06:15.721312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"main_categorical_cols = [cname for cname in common_columns if train_data[cname].dtype == \"object\"]\nmain_numerical_cols = [cname for cname in common_columns if train_data[cname].dtype in ['int64', 'float64']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:20.890996Z","iopub.execute_input":"2024-11-29T12:06:20.891431Z","iopub.status.idle":"2024-11-29T12:06:20.901278Z","shell.execute_reply.started":"2024-11-29T12:06:20.891388Z","shell.execute_reply":"2024-11-29T12:06:20.899787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numerical_transformer = SimpleImputer(strategy='mean')\n\ncategorical_transformer = Pipeline(steps=[\n    ('imputer', SimpleImputer(strategy='most_frequent')),\n    ('onehot', OneHotEncoder(handle_unknown='ignore'))\n])\n\npreprocessor = ColumnTransformer(transformers=[\n    ('num', numerical_transformer, main_numerical_cols), \n    ('categorical', categorical_transformer, main_categorical_cols)])\n\nforest_model = RandomForestClassifier(n_estimators=50, random_state=0)\n\npipeline = Pipeline(steps=[('preprocess', preprocessor), ('model', forest_model)])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:23.086812Z","iopub.execute_input":"2024-11-29T12:06:23.087290Z","iopub.status.idle":"2024-11-29T12:06:23.095930Z","shell.execute_reply.started":"2024-11-29T12:06:23.087247Z","shell.execute_reply":"2024-11-29T12:06:23.093654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_data[list(common_columns)]\ny = train_data.sii\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:25.611210Z","iopub.execute_input":"2024-11-29T12:06:25.611662Z","iopub.status.idle":"2024-11-29T12:06:25.627688Z","shell.execute_reply.started":"2024-11-29T12:06:25.611619Z","shell.execute_reply":"2024-11-29T12:06:25.625747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pipeline.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:27.059187Z","iopub.execute_input":"2024-11-29T12:06:27.059631Z","iopub.status.idle":"2024-11-29T12:06:27.615803Z","shell.execute_reply.started":"2024-11-29T12:06:27.059588Z","shell.execute_reply":"2024-11-29T12:06:27.614316Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = pipeline.predict(X_test)\n\nprint(f\"Accuracy is: {accuracy_score(y_test, pred)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:06:46.763813Z","iopub.execute_input":"2024-11-29T12:06:46.764283Z","iopub.status.idle":"2024-11-29T12:06:46.799796Z","shell.execute_reply.started":"2024-11-29T12:06:46.764245Z","shell.execute_reply":"2024-11-29T12:06:46.798574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pipeline.fit(X, y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:07:25.899385Z","iopub.execute_input":"2024-11-29T12:07:25.899841Z","iopub.status.idle":"2024-11-29T12:07:26.550048Z","shell.execute_reply.started":"2024-11-29T12:07:25.899799Z","shell.execute_reply":"2024-11-29T12:07:26.548721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"main_pred = pipeline.predict(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:07:50.224234Z","iopub.execute_input":"2024-11-29T12:07:50.224630Z","iopub.status.idle":"2024-11-29T12:07:50.248347Z","shell.execute_reply.started":"2024-11-29T12:07:50.224595Z","shell.execute_reply":"2024-11-29T12:07:50.246969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.DataFrame({'id': test_data.index, 'sii': main_pred})\nsub.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-29T12:09:16.253346Z","iopub.execute_input":"2024-11-29T12:09:16.253755Z","iopub.status.idle":"2024-11-29T12:09:16.264837Z","shell.execute_reply.started":"2024-11-29T12:09:16.253714Z","shell.execute_reply":"2024-11-29T12:09:16.263375Z"}},"outputs":[],"execution_count":null}]}