{"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":"\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-03T09:54:15.916297Z","iopub.execute_input":"2024-11-03T09:54:15.916691Z","iopub.status.idle":"2024-11-03T09:54:19.574875Z","shell.execute_reply.started":"2024-11-03T09:54:15.916649Z","shell.execute_reply":"2024-11-03T09:54:19.573666Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np, pandas as pd, os\n\nimport plotly.express as px, seaborn as sns, matplotlib.pyplot as plt\nsns.set_style('darkgrid')\nfrom sklearn.metrics import make_scorer, cohen_kappa_score\nimport warnings\nwarnings.simplefilter('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:19.577300Z","iopub.execute_input":"2024-11-03T09:54:19.577919Z","iopub.status.idle":"2024-11-03T09:54:21.250695Z","shell.execute_reply.started":"2024-11-03T09:54:19.577865Z","shell.execute_reply":"2024-11-03T09:54:21.249739Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:54:21.251788Z","iopub.execute_input":"2024-11-03T09:54:21.252313Z","iopub.status.idle":"2024-11-03T09:54:21.319638Z","shell.execute_reply.started":"2024-11-03T09:54:21.252273Z","shell.execute_reply":"2024-11-03T09:54:21.318615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:54:21.321772Z","iopub.execute_input":"2024-11-03T09:54:21.322137Z","iopub.status.idle":"2024-11-03T09:54:21.338400Z","shell.execute_reply.started":"2024-11-03T09:54:21.322098Z","shell.execute_reply":"2024-11-03T09:54:21.337196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.339623Z","iopub.execute_input":"2024-11-03T09:54:21.339955Z","iopub.status.idle":"2024-11-03T09:54:21.385204Z","shell.execute_reply.started":"2024-11-03T09:54:21.339918Z","shell.execute_reply":"2024-11-03T09:54:21.384088Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:54:21.386521Z","iopub.execute_input":"2024-11-03T09:54:21.386863Z","iopub.status.idle":"2024-11-03T09:54:21.412119Z","shell.execute_reply.started":"2024-11-03T09:54:21.386825Z","shell.execute_reply":"2024-11-03T09:54:21.410835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.413578Z","iopub.execute_input":"2024-11-03T09:54:21.414037Z","iopub.status.idle":"2024-11-03T09:54:21.450085Z","shell.execute_reply.started":"2024-11-03T09:54:21.413994Z","shell.execute_reply":"2024-11-03T09:54:21.449011Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.nunique","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.451288Z","iopub.execute_input":"2024-11-03T09:54:21.451644Z","iopub.status.idle":"2024-11-03T09:54:21.475856Z","shell.execute_reply.started":"2024-11-03T09:54:21.451607Z","shell.execute_reply":"2024-11-03T09:54:21.474685Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.477223Z","iopub.execute_input":"2024-11-03T09:54:21.477830Z","iopub.status.idle":"2024-11-03T09:54:21.634925Z","shell.execute_reply.started":"2024-11-03T09:54:21.477666Z","shell.execute_reply":"2024-11-03T09:54:21.633809Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.639250Z","iopub.execute_input":"2024-11-03T09:54:21.639800Z","iopub.status.idle":"2024-11-03T09:54:21.646691Z","shell.execute_reply.started":"2024-11-03T09:54:21.639761Z","shell.execute_reply":"2024-11-03T09:54:21.645682Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.648119Z","iopub.execute_input":"2024-11-03T09:54:21.648837Z","iopub.status.idle":"2024-11-03T09:54:21.675013Z","shell.execute_reply.started":"2024-11-03T09:54:21.648777Z","shell.execute_reply":"2024-11-03T09:54:21.674018Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Separate numeric and categorical columns\nnumeric_columns = train.select_dtypes(include=['number']).columns.tolist()\ncategorical_columns = train.select_dtypes(include=['object', 'category']).columns.tolist()\n\n# Print the numeric and categorical columns\nprint(\"Numeric Columns:\")\nprint(numeric_columns)\n\nprint(\"\\nCategorical Columns:\")\nprint(categorical_columns)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.676374Z","iopub.execute_input":"2024-11-03T09:54:21.676779Z","iopub.status.idle":"2024-11-03T09:54:21.685204Z","shell.execute_reply.started":"2024-11-03T09:54:21.676729Z","shell.execute_reply":"2024-11-03T09:54:21.684148Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.686365Z","iopub.execute_input":"2024-11-03T09:54:21.686745Z","iopub.status.idle":"2024-11-03T09:54:21.694821Z","shell.execute_reply.started":"2024-11-03T09:54:21.686709Z","shell.execute_reply":"2024-11-03T09:54:21.693847Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Visualizing Numeric Columns\ndef plot_numeric_columns(train, numeric_columns):\n    # Histograms for Numeric Columns\n    for col in numeric_columns:\n        if col in train.columns:\n            plt.figure(figsize=(8, 4))\n            sns.histplot(train[col].dropna(), kde=True, bins=30)  \n            plt.title(f'Distribution of {col}')\n            plt.show()\n\n    # Correlation Heatmap for Numeric Columns\n    plt.figure(figsize=(10, 8))\n    sns.heatmap(train[numeric_columns].corr(), annot=True, cmap='coolwarm', fmt=\".2f\")\n    plt.title('Correlation Heatmap')\n    plt.show()\n\n# Visualizing Categorical Columns\ndef plot_categorical_columns(train, categorical_columns):\n    # Bar Plots for Categorical Columns\n    for col in categorical_columns:\n        if col in train.columns:\n            plt.figure(figsize=(8, 4))\n            sns.countplot(x=train[col], palette='Set2')\n            plt.title(f'Count of {col}')\n            plt.xticks(rotation=45)\n            plt.show()\n\n#  Checking for Outliers in Numeric Columns\ndef plot_outliers(train, numeric_columns):\n    # Boxplots for Numeric Columns to detect outliers\n    for col in numeric_columns:\n        if col in train.columns:\n            plt.figure(figsize=(8, 4))\n            sns.boxplot(x=train[col].dropna())  # Drop missing values to avoid errors\n            plt.title(f'Boxplot of {col}')\n            plt.show()\n\n# Visualizing Relationships Between Physical Activity and PCIAT (Internet Addiction)\ndef plot_relationships(train, target='PCIAT-PCIAT_Total'):\n    # Ensure the target column exists\n    if target in train.columns:\n        selected_columns = [col for col in numeric_columns if col in train.columns] + [target]\n        # Drop rows with missing values in selected columns\n        train_clean = train[selected_columns].dropna()\n        if not train_clean.empty:\n            sns.pairplot(train_clean)\n            plt.show()\n\n    # Scatter plots for potential relationships\n    if target in train.columns:\n        for col in numeric_columns:\n            if col in train.columns:\n                plt.figure(figsize=(8, 4))\n                sns.scatterplot(x=train[col].dropna(), y=train[target].dropna())  # Handle missing values\n                plt.title(f'{col} vs {target}')\n                plt.show()\n\n# Execute the functions for EDA\nplot_numeric_columns(train, numeric_columns)\nplot_categorical_columns(train, categorical_columns)\nplot_outliers(train, numeric_columns)\nplot_relationships(train)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:54:21.696355Z","iopub.execute_input":"2024-11-03T09:54:21.697123Z","iopub.status.idle":"2024-11-03T09:56:30.683139Z","shell.execute_reply.started":"2024-11-03T09:54:21.697045Z","shell.execute_reply":"2024-11-03T09:56:30.682087Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#  Function to draw pie charts for categorical columns\ndef plot_pie_charts(train, categorical_columns):\n    for col in categorical_columns:\n        if col in train.columns:\n            # Get value counts for the column\n            data = train[col].value_counts()\n            \n            # Create pie chart\n            plt.figure(figsize=(6, 6))\n            plt.pie(data, labels=data.index, autopct='%1.1f%%', startangle=90, colors=sns.color_palette(\"Set2\"))\n            plt.title(f'Distribution of {col}')\n            plt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.\n            plt.show()\n\n# Example usage for pie charts\nplot_pie_charts(train, categorical_columns)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:56:30.684718Z","iopub.execute_input":"2024-11-03T09:56:30.685170Z","iopub.status.idle":"2024-11-03T09:57:10.277890Z","shell.execute_reply.started":"2024-11-03T09:56:30.685119Z","shell.execute_reply":"2024-11-03T09:57:10.276751Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_train = train.copy()\nselected_train = selected_train[['Basic_Demos-Enroll_Season', 'Basic_Demos-Age', 'Basic_Demos-Sex','sii']]\nselected_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.279587Z","iopub.execute_input":"2024-11-03T09:57:10.280300Z","iopub.status.idle":"2024-11-03T09:57:10.306243Z","shell.execute_reply.started":"2024-11-03T09:57:10.280247Z","shell.execute_reply":"2024-11-03T09:57:10.305077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_test = test.copy()\nselected_test = selected_test[['Basic_Demos-Enroll_Season', 'Basic_Demos-Age', 'Basic_Demos-Sex']]\nselected_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.311508Z","iopub.execute_input":"2024-11-03T09:57:10.314355Z","iopub.status.idle":"2024-11-03T09:57:10.334534Z","shell.execute_reply.started":"2024-11-03T09:57:10.314283Z","shell.execute_reply":"2024-11-03T09:57:10.333481Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_train.fillna(0, inplace=True)\nselected_train = pd.get_dummies(selected_train, columns=['Basic_Demos-Enroll_Season'])\nselected_train['sii'] = selected_train.pop('sii')\nselected_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.336256Z","iopub.execute_input":"2024-11-03T09:57:10.336700Z","iopub.status.idle":"2024-11-03T09:57:10.359700Z","shell.execute_reply.started":"2024-11-03T09:57:10.336646Z","shell.execute_reply":"2024-11-03T09:57:10.358689Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"selected_test.fillna(0, inplace=True)\nselected_test = pd.get_dummies(selected_test, columns=['Basic_Demos-Enroll_Season'])\nselected_test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.361040Z","iopub.execute_input":"2024-11-03T09:57:10.361390Z","iopub.status.idle":"2024-11-03T09:57:10.379365Z","shell.execute_reply.started":"2024-11-03T09:57:10.361354Z","shell.execute_reply":"2024-11-03T09:57:10.378028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score, classification_report\n\nX = selected_train.drop(columns=['sii'])  \ny = selected_train['sii']  \n\nscaler = MinMaxScaler()\nX = scaler.fit_transform(X)\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n\nmodel = LogisticRegression()\n\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.380778Z","iopub.execute_input":"2024-11-03T09:57:10.381112Z","iopub.status.idle":"2024-11-03T09:57:10.579767Z","shell.execute_reply.started":"2024-11-03T09:57:10.381075Z","shell.execute_reply":"2024-11-03T09:57:10.578656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.581414Z","iopub.execute_input":"2024-11-03T09:57:10.582098Z","iopub.status.idle":"2024-11-03T09:57:10.587639Z","shell.execute_reply.started":"2024-11-03T09:57:10.582046Z","shell.execute_reply":"2024-11-03T09:57:10.586268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy = accuracy_score(y_test, y_pred)\nreport = classification_report(y_test, y_pred)\n\nprint(f\"Precisión: {accuracy*100}\")\nprint(\"Reporte de clasificación:\")\nprint(report)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.589430Z","iopub.execute_input":"2024-11-03T09:57:10.589849Z","iopub.status.idle":"2024-11-03T09:57:10.611175Z","shell.execute_reply.started":"2024-11-03T09:57:10.589800Z","shell.execute_reply":"2024-11-03T09:57:10.609895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(selected_test).astype(int)\n\npredictions_df = pd.DataFrame({\n    'id': test['id'],  \n    'sii': predictions  \n})\n\npredictions_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.612676Z","iopub.execute_input":"2024-11-03T09:57:10.613800Z","iopub.status.idle":"2024-11-03T09:57:10.630293Z","shell.execute_reply.started":"2024-11-03T09:57:10.613745Z","shell.execute_reply":"2024-11-03T09:57:10.628892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions_df.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-03T09:57:10.631836Z","iopub.execute_input":"2024-11-03T09:57:10.632496Z","iopub.status.idle":"2024-11-03T09:57:10.642167Z","shell.execute_reply.started":"2024-11-03T09:57:10.632446Z","shell.execute_reply":"2024-11-03T09:57:10.641107Z"}},"outputs":[],"execution_count":null}]}