{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":30804,"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\nfrom pathlib import Path\nfrom concurrent.futures import ThreadPoolExecutor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import mean_squared_error\nimport pyarrow.parquet as pq\nimport matplotlib as plt\nimport os\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.175053Z","iopub.execute_input":"2024-12-07T04:21:16.175531Z","iopub.status.idle":"2024-12-07T04:21:16.183331Z","shell.execute_reply.started":"2024-12-07T04:21:16.175490Z","shell.execute_reply":"2024-12-07T04:21:16.181822Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n    Demographics - Information about age and sex of participants.\n    Internet Use - Number of hours of using computer/internet per day.\n    Children's Global Assessment Scale - Numeric scale used by mental health clinicians to rate the general functioning of youths under the age of 18.\n    Physical Measures - Collection of blood pressure, heart rate, height, weight and waist, and hip measurements.\n    FitnessGram Vitals and Treadmill - Measurements of cardiovascular fitness assessed using the NHANES treadmill protocol.\n    FitnessGram Child - Health related physical fitness assessment measuring five different parameters including aerobic capacity, muscular strength, muscular endurance, flexibility, and body composition.\n    Bio-electric Impedance Analysis - Measure of key body composition elements, including BMI, fat, muscle, and water content.\n    Physical Activity Questionnaire - Information about children's participation in vigorous activities over the last 7 days.\n    Sleep Disturbance Scale - Scale to categorize sleep disorders in children.\n    Actigraphy - Objective measure of ecological physical activity through a research-grade biotracker.\n    Parent-Child Internet Addiction Test - 20-item scale that measures characteristics and behaviors associated with compulsive use of the Internet including compulsivity, escapism, and dependency.\n\nseries_{train|test}.parquet/id={id} - Series to be used as training data, partitioned by id. Each series is a continuous recording of accelerometer data for a single subject spanning many days.\n\n    id - The patient identifier corresponding to the id field in train/test.csv.\n    step - An integer timestep for each observation within a series.\n    X, Y, Z - Measure of acceleration, in g, experienced by the wrist-worn watch along each standard axis.\n    enmo - As calculated and described by the wristpy package, ENMO is the Euclidean Norm Minus One of all accelerometer signals (along each of the x-, y-, and z-axis, measured in g-force) with negative values rounded to zero. Zero values are indicative of periods of no motion. While no standard measure of acceleration exists in this space, this is one of the several commonly computed features.\n    anglez - As calculated and described by the wristpy package, Angle-Z is a metric derived from individual accelerometer components and refers to the angle of the arm relative to the horizontal plane.\n    non-wear_flag - A flag (0: watch is being worn, 1: the watch is not worn) to help determine periods when the watch has been removed, based on the GGIR definition, which uses the standard deviation and range of the accelerometer data.\n    light - Measure of ambient light in lux. See ​​here for details.\n    battery_voltage - A measure of the battery voltage in mV.\n    time_of_day - Time of day representing the start of a 5s window that the data has been sampled over, with format %H:%M:%S.%9f.\n    weekday - The day of the week, coded as an integer with 1 being Monday and 7 being Sunday.\n    quarter - The quarter of the year, an integer from 1 to 4.\n    relative_date_PCIAT - The number of days (integer) since the PCIAT test was administered (negative days indicate that the actigraphy data has been collected before the test was administered).","metadata":{}},{"cell_type":"code","source":"dataDict = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/data_dictionary.csv')\ndataDict","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.185368Z","iopub.execute_input":"2024-12-07T04:21:16.185784Z","iopub.status.idle":"2024-12-07T04:21:16.226643Z","shell.execute_reply.started":"2024-12-07T04:21:16.185746Z","shell.execute_reply":"2024-12-07T04:21:16.225435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Cargar datos tabulares\ntrain_df = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/train.csv')\ntest_df = pd.read_csv('/kaggle/input/child-mind-institute-problematic-internet-use/test.csv')\ntrain_df.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.228738Z","iopub.execute_input":"2024-12-07T04:21:16.229117Z","iopub.status.idle":"2024-12-07T04:21:16.284400Z","shell.execute_reply.started":"2024-12-07T04:21:16.229081Z","shell.execute_reply":"2024-12-07T04:21:16.283067Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Hay muchos datos nulos","metadata":{}},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)\npd.set_option('display.max_rows', None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.286054Z","iopub.execute_input":"2024-12-07T04:21:16.286536Z","iopub.status.idle":"2024-12-07T04:21:16.293247Z","shell.execute_reply.started":"2024-12-07T04:21:16.286461Z","shell.execute_reply":"2024-12-07T04:21:16.291881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.296609Z","iopub.execute_input":"2024-12-07T04:21:16.297135Z","iopub.status.idle":"2024-12-07T04:21:16.497244Z","shell.execute_reply.started":"2024-12-07T04:21:16.297075Z","shell.execute_reply":"2024-12-07T04:21:16.495821Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Problemas destacados: \n* Physical-BMI minimo 0\n* Physical-Weight minimo 0","metadata":{}},{"cell_type":"code","source":"print(train_df.isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.498950Z","iopub.execute_input":"2024-12-07T04:21:16.499324Z","iopub.status.idle":"2024-12-07T04:21:16.511095Z","shell.execute_reply.started":"2024-12-07T04:21:16.499288Z","shell.execute_reply":"2024-12-07T04:21:16.509640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Imputación con una categoría nueva\nfor column in train_df.select_dtypes(include=['object']).columns:\n    train_df[column] = train_df[column].fillna('Desconocido')\n\n# Verificar que no hay valores faltantes\nprint(train_df.isnull().sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.512944Z","iopub.execute_input":"2024-12-07T04:21:16.513450Z","iopub.status.idle":"2024-12-07T04:21:16.543708Z","shell.execute_reply.started":"2024-12-07T04:21:16.513412Z","shell.execute_reply":"2024-12-07T04:21:16.542528Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_df.describe())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.545157Z","iopub.execute_input":"2024-12-07T04:21:16.545538Z","iopub.status.idle":"2024-12-07T04:21:16.724839Z","shell.execute_reply.started":"2024-12-07T04:21:16.545464Z","shell.execute_reply":"2024-12-07T04:21:16.723553Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**codigo de preprocesamiento tomado de focalloss PB:0.342 por XuKong Ji**","metadata":{}},{"cell_type":"code","source":"def process_file(filename, dirname):\n    df = pd.read_parquet(os.path.join(dirname, filename, 'part-0.parquet'))\n    df.drop('step', axis=1, inplace=True)\n    \n    return df.describe().values.reshape(-1), filename.split('=')[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.726344Z","iopub.execute_input":"2024-12-07T04:21:16.726817Z","iopub.status.idle":"2024-12-07T04:21:16.734021Z","shell.execute_reply.started":"2024-12-07T04:21:16.726763Z","shell.execute_reply":"2024-12-07T04:21:16.732522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_time_series(dirname):\n    ids = os.listdir(dirname)\n    \n    with ThreadPoolExecutor() as executor:\n        results = list(tqdm(executor.map(lambda fname: process_file(fname, dirname), ids), total=len(ids)))\n    \n    stats, indexes = zip(*results)\n    df = pd.DataFrame(stats, columns=[f\"stat_{i}\" for i in range(len(stats[0]))])\n    df['id'] = indexes\n    \n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.735867Z","iopub.execute_input":"2024-12-07T04:21:16.736357Z","iopub.status.idle":"2024-12-07T04:21:16.749577Z","shell.execute_reply.started":"2024-12-07T04:21:16.736305Z","shell.execute_reply":"2024-12-07T04:21:16.748357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def quadratic_weighted_kappa(estimator, X, y_true):\n    y_pred = estimator.predict(X).round()\n    return cohen_kappa_score(y_true, y_pred, weights='quadratic')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.754913Z","iopub.execute_input":"2024-12-07T04:21:16.755338Z","iopub.status.idle":"2024-12-07T04:21:16.770341Z","shell.execute_reply.started":"2024-12-07T04:21:16.755302Z","shell.execute_reply":"2024-12-07T04:21:16.768850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def threshold_rounder(y_pred, thresholds):\n    return np.where(y_pred < thresholds[0], 0,\n                    np.where(y_pred < thresholds[1], 1,\n                             np.where(y_pred < thresholds[2], 2, 3)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.771796Z","iopub.execute_input":"2024-12-07T04:21:16.772170Z","iopub.status.idle":"2024-12-07T04:21:16.791796Z","shell.execute_reply.started":"2024-12-07T04:21:16.772132Z","shell.execute_reply":"2024-12-07T04:21:16.790516Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def eval_preds(thresholds, y_true, y_pred):\n    y_pred = threshold_rounder(y_pred, thresholds)\n    score = cohen_kappa_score(y_true, y_pred, weights='quadratic')\n    return -score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.793277Z","iopub.execute_input":"2024-12-07T04:21:16.793755Z","iopub.status.idle":"2024-12-07T04:21:16.805580Z","shell.execute_reply.started":"2024-12-07T04:21:16.793703Z","shell.execute_reply":"2024-12-07T04:21:16.804372Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"root = Path('/kaggle/input/child-mind-institute-problematic-internet-use')\ntrain_ts = load_time_series(root / \"series_train.parquet\")\ntest_ts = load_time_series(root / \"series_test.parquet\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:21:16.807372Z","iopub.execute_input":"2024-12-07T04:21:16.807864Z","iopub.status.idle":"2024-12-07T04:22:48.137727Z","shell.execute_reply.started":"2024-12-07T04:21:16.807812Z","shell.execute_reply":"2024-12-07T04:22:48.135866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.merge(train_df, train_ts, how=\"left\", on='id')\ntest_df = pd.merge(test_df, test_ts, how=\"left\", on='id')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.139726Z","iopub.execute_input":"2024-12-07T04:22:48.140316Z","iopub.status.idle":"2024-12-07T04:22:48.165450Z","shell.execute_reply.started":"2024-12-07T04:22:48.140272Z","shell.execute_reply":"2024-12-07T04:22:48.163624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for column in train_df.select_dtypes(include=['object']).columns:\n    if column in test_df.columns:\n        most_frequent = train_df[column].mode()[0]\n        train_df[column] = train_df[column].fillna(most_frequent)\n        test_df[column] = test_df[column].fillna(most_frequent)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.167763Z","iopub.execute_input":"2024-12-07T04:22:48.168312Z","iopub.status.idle":"2024-12-07T04:22:48.209729Z","shell.execute_reply.started":"2024-12-07T04:22:48.168266Z","shell.execute_reply":"2024-12-07T04:22:48.207972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.211514Z","iopub.execute_input":"2024-12-07T04:22:48.211930Z","iopub.status.idle":"2024-12-07T04:22:48.229132Z","shell.execute_reply.started":"2024-12-07T04:22:48.211895Z","shell.execute_reply":"2024-12-07T04:22:48.227924Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.230535Z","iopub.execute_input":"2024-12-07T04:22:48.230864Z","iopub.status.idle":"2024-12-07T04:22:48.248884Z","shell.execute_reply.started":"2024-12-07T04:22:48.230833Z","shell.execute_reply":"2024-12-07T04:22:48.246583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Codificación one-hot\ntrain_df = pd.get_dummies(train_df, drop_first=True)\ntest_df = pd.get_dummies(test_df, drop_first=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.250464Z","iopub.execute_input":"2024-12-07T04:22:48.250895Z","iopub.status.idle":"2024-12-07T04:22:48.307383Z","shell.execute_reply.started":"2024-12-07T04:22:48.250859Z","shell.execute_reply":"2024-12-07T04:22:48.306277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target = train_df['sii']\ntrain_df, test_df = train_df.align(test_df, join='inner', axis=1)\ntrain_df['sii'] = target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.308840Z","iopub.execute_input":"2024-12-07T04:22:48.309201Z","iopub.status.idle":"2024-12-07T04:22:48.320277Z","shell.execute_reply.started":"2024-12-07T04:22:48.309166Z","shell.execute_reply":"2024-12-07T04:22:48.318947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = train_df.dropna(subset=['sii'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.321955Z","iopub.execute_input":"2024-12-07T04:22:48.322544Z","iopub.status.idle":"2024-12-07T04:22:48.344951Z","shell.execute_reply.started":"2024-12-07T04:22:48.322506Z","shell.execute_reply":"2024-12-07T04:22:48.343353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = train_df.drop('sii', axis=1)\ny_train = train_df['sii']\nX_test = test_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.346508Z","iopub.execute_input":"2024-12-07T04:22:48.347071Z","iopub.status.idle":"2024-12-07T04:22:48.360285Z","shell.execute_reply.started":"2024-12-07T04:22:48.346883Z","shell.execute_reply":"2024-12-07T04:22:48.359075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imputer = SimpleImputer(strategy='mean')\nX_train = imputer.fit_transform(X_train)\nX_test = imputer.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.362330Z","iopub.execute_input":"2024-12-07T04:22:48.362790Z","iopub.status.idle":"2024-12-07T04:22:48.462671Z","shell.execute_reply.started":"2024-12-07T04:22:48.362753Z","shell.execute_reply":"2024-12-07T04:22:48.461367Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"NO ENTIENDO POR QUE AL HACER ESTA IMPUTACION SE AGRAGAN NUEVAS COLUMNAS","metadata":{}},{"cell_type":"code","source":"model = RandomForestRegressor(random_state=42)\nmodel.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:22:48.464103Z","iopub.execute_input":"2024-12-07T04:22:48.464574Z","iopub.status.idle":"2024-12-07T04:23:02.710903Z","shell.execute_reply.started":"2024-12-07T04:22:48.464523Z","shell.execute_reply":"2024-12-07T04:23:02.709363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sii_predictions = model.predict(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:23:02.712641Z","iopub.execute_input":"2024-12-07T04:23:02.713148Z","iopub.status.idle":"2024-12-07T04:23:02.726333Z","shell.execute_reply.started":"2024-12-07T04:23:02.713091Z","shell.execute_reply":"2024-12-07T04:23:02.724592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convertir predicciones a enteros\nsii_predictions = sii_predictions.round().astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:23:02.728586Z","iopub.execute_input":"2024-12-07T04:23:02.728991Z","iopub.status.idle":"2024-12-07T04:23:02.738267Z","shell.execute_reply.started":"2024-12-07T04:23:02.728954Z","shell.execute_reply":"2024-12-07T04:23:02.736855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'id': test_df.index,  # Asegúrate de que 'id' sea el índice o una columna en test_df\n    'sii': sii_predictions\n})\n\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T04:23:02.739660Z","iopub.execute_input":"2024-12-07T04:23:02.740147Z","iopub.status.idle":"2024-12-07T04:23:02.756173Z","shell.execute_reply.started":"2024-12-07T04:23:02.740108Z","shell.execute_reply":"2024-12-07T04:23:02.754587Z"}},"outputs":[],"execution_count":null}]}