{"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)\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\n#import os\n#for 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-02-23T22:44:34.094546Z","iopub.execute_input":"2023-02-23T22:44:34.094978Z","iopub.status.idle":"2023-02-23T22:44:34.105442Z","shell.execute_reply.started":"2023-02-23T22:44:34.094897Z","shell.execute_reply":"2023-02-23T22:44:34.104388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read dataset\ntrain = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\ntrain.tail()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.15154Z","iopub.execute_input":"2023-02-23T22:44:34.151823Z","iopub.status.idle":"2023-02-23T22:44:34.229455Z","shell.execute_reply.started":"2023-02-23T22:44:34.151799Z","shell.execute_reply":"2023-02-23T22:44:34.228384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read test validation data\ntest = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/test.csv\")\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.231268Z","iopub.execute_input":"2023-02-23T22:44:34.231842Z","iopub.status.idle":"2023-02-23T22:44:34.248312Z","shell.execute_reply.started":"2023-02-23T22:44:34.231805Z","shell.execute_reply":"2023-02-23T22:44:34.24731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocesado\n\n* Seleccionar características relevantes.\n* Dividir dataset train / test.\n* Normalizar valores.","metadata":{}},{"cell_type":"code","source":"display(train.info())","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.25011Z","iopub.execute_input":"2023-02-23T22:44:34.250538Z","iopub.status.idle":"2023-02-23T22:44:34.275159Z","shell.execute_reply.started":"2023-02-23T22:44:34.250504Z","shell.execute_reply":"2023-02-23T22:44:34.274106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fill empty age\ndef fill_age(dtf):\n    media = dtf['age'].mean()\n    dtf['age'].fillna(value=media, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.279789Z","iopub.execute_input":"2023-02-23T22:44:34.28004Z","iopub.status.idle":"2023-02-23T22:44:34.285204Z","shell.execute_reply.started":"2023-02-23T22:44:34.280017Z","shell.execute_reply":"2023-02-23T22:44:34.283951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_age(train)\ndisplay(train.info())","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.310573Z","iopub.execute_input":"2023-02-23T22:44:34.310837Z","iopub.status.idle":"2023-02-23T22:44:34.331151Z","shell.execute_reply.started":"2023-02-23T22:44:34.310813Z","shell.execute_reply":"2023-02-23T22:44:34.330196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.348978Z","iopub.execute_input":"2023-02-23T22:44:34.349609Z","iopub.status.idle":"2023-02-23T22:44:34.358757Z","shell.execute_reply.started":"2023-02-23T22:44:34.349583Z","shell.execute_reply":"2023-02-23T22:44:34.357829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.398813Z","iopub.execute_input":"2023-02-23T22:44:34.399067Z","iopub.status.idle":"2023-02-23T22:44:34.404881Z","shell.execute_reply.started":"2023-02-23T22:44:34.399043Z","shell.execute_reply":"2023-02-23T22:44:34.403947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#features = ['site_id', 'patient_id', 'image_id', 'age',\n#         'implant', 'machine_id']\nfeatures = ['age', 'implant', 'machine_id']\ntarget = ['cancer']","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.436493Z","iopub.execute_input":"2023-02-23T22:44:34.438336Z","iopub.status.idle":"2023-02-23T22:44:34.443062Z","shell.execute_reply.started":"2023-02-23T22:44:34.438309Z","shell.execute_reply":"2023-02-23T22:44:34.442298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dummies(dtf):\n    return pd.get_dummies(dtf, columns=['machine_id'], prefix='machine_')","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.462479Z","iopub.execute_input":"2023-02-23T22:44:34.463457Z","iopub.status.idle":"2023-02-23T22:44:34.468498Z","shell.execute_reply.started":"2023-02-23T22:44:34.463423Z","shell.execute_reply":"2023-02-23T22:44:34.467499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.500532Z","iopub.execute_input":"2023-02-23T22:44:34.500795Z","iopub.status.idle":"2023-02-23T22:44:34.515972Z","shell.execute_reply.started":"2023-02-23T22:44:34.500772Z","shell.execute_reply":"2023-02-23T22:44:34.514819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelo","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.ensemble import RandomForestRegressor","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.545721Z","iopub.execute_input":"2023-02-23T22:44:34.545987Z","iopub.status.idle":"2023-02-23T22:44:34.934112Z","shell.execute_reply.started":"2023-02-23T22:44:34.545963Z","shell.execute_reply":"2023-02-23T22:44:34.933186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[features]\ny = train[target]","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.935948Z","iopub.execute_input":"2023-02-23T22:44:34.936411Z","iopub.status.idle":"2023-02-23T22:44:34.944553Z","shell.execute_reply.started":"2023-02-23T22:44:34.936375Z","shell.execute_reply":"2023-02-23T22:44:34.94342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.20,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.9461Z","iopub.execute_input":"2023-02-23T22:44:34.946588Z","iopub.status.idle":"2023-02-23T22:44:34.962384Z","shell.execute_reply.started":"2023-02-23T22:44:34.946543Z","shell.execute_reply":"2023-02-23T22:44:34.961358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr=LinearRegression()\n#lr=RandomForestRegressor()\nmodel=lr.fit(X_train,y_train.values.ravel())\n","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.96596Z","iopub.execute_input":"2023-02-23T22:44:34.966218Z","iopub.status.idle":"2023-02-23T22:44:34.977286Z","shell.execute_reply.started":"2023-02-23T22:44:34.966195Z","shell.execute_reply":"2023-02-23T22:44:34.976365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.978853Z","iopub.execute_input":"2023-02-23T22:44:34.979291Z","iopub.status.idle":"2023-02-23T22:44:34.986385Z","shell.execute_reply.started":"2023-02-23T22:44:34.979232Z","shell.execute_reply":"2023-02-23T22:44:34.985343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:34.988237Z","iopub.execute_input":"2023-02-23T22:44:34.988779Z","iopub.status.idle":"2023-02-23T22:44:35.000124Z","shell.execute_reply.started":"2023-02-23T22:44:34.988738Z","shell.execute_reply":"2023-02-23T22:44:34.998987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:35.002107Z","iopub.execute_input":"2023-02-23T22:44:35.002464Z","iopub.status.idle":"2023-02-23T22:44:35.022627Z","shell.execute_reply.started":"2023-02-23T22:44:35.002432Z","shell.execute_reply":"2023-02-23T22:44:35.02148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fill_age(test)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:35.024173Z","iopub.execute_input":"2023-02-23T22:44:35.024732Z","iopub.status.idle":"2023-02-23T22:44:35.029661Z","shell.execute_reply.started":"2023-02-23T22:44:35.0247Z","shell.execute_reply":"2023-02-23T22:44:35.028525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:35.031226Z","iopub.execute_input":"2023-02-23T22:44:35.032357Z","iopub.status.idle":"2023-02-23T22:44:35.041683Z","shell.execute_reply.started":"2023-02-23T22:44:35.032323Z","shell.execute_reply":"2023-02-23T22:44:35.040509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(test[features])","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:35.046547Z","iopub.execute_input":"2023-02-23T22:44:35.047877Z","iopub.status.idle":"2023-02-23T22:44:35.061962Z","shell.execute_reply.started":"2023-02-23T22:44:35.047828Z","shell.execute_reply":"2023-02-23T22:44:35.060702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r=y_pred.flatten()\nt=pd.Series(r)\nw=test['prediction_id']","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:35.065042Z","iopub.execute_input":"2023-02-23T22:44:35.067925Z","iopub.status.idle":"2023-02-23T22:44:35.07671Z","shell.execute_reply.started":"2023-02-23T22:44:35.067877Z","shell.execute_reply":"2023-02-23T22:44:35.074891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.concat([t, test], join='outer') # This eliminates any missing prediction ids\n\nsub_df = sub_df.sort_values(by=['prediction_id']).reset_index(drop=True) # Reordering by prediction id as required\n\nsub_df = sub_df.fillna(0) # Fill missing prediction values","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:44:35.078485Z","iopub.execute_input":"2023-02-23T22:44:35.079461Z","iopub.status.idle":"2023-02-23T22:44:35.104865Z","shell.execute_reply.started":"2023-02-23T22:44:35.079413Z","shell.execute_reply":"2023-02-23T22:44:35.103342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.DataFrame()\nsample['prediction_id']=test.index.map(w)\nsample['cancer']=t\n","metadata":{"execution":{"iopub.status.busy":"2023-02-23T22:53:16.569809Z","iopub.execute_input":"2023-02-23T22:53:16.570486Z","iopub.status.idle":"2023-02-23T22:53:16.581226Z","shell.execute_reply.started":"2023-02-23T22:53:16.570427Z","shell.execute_reply":"2023-02-23T22:53:16.580298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample = sample.replace('_','-', regex=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:09:45.232117Z","iopub.execute_input":"2023-02-23T23:09:45.232828Z","iopub.status.idle":"2023-02-23T23:09:45.23798Z","shell.execute_reply.started":"2023-02-23T23:09:45.232791Z","shell.execute_reply":"2023-02-23T23:09:45.236896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:10:35.493448Z","iopub.execute_input":"2023-02-23T23:10:35.493848Z","iopub.status.idle":"2023-02-23T23:10:35.500743Z","shell.execute_reply.started":"2023-02-23T23:10:35.493817Z","shell.execute_reply":"2023-02-23T23:10:35.499575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test.to_csv('submission.csv',index=False)\nque = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv')\nprint(que.columns)\nque['cancer']=0.776564\nque.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(sample.info())\ndisplay(sample.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:09:51.025647Z","iopub.execute_input":"2023-02-23T23:09:51.026171Z","iopub.status.idle":"2023-02-23T23:09:51.046768Z","shell.execute_reply.started":"2023-02-23T23:09:51.026128Z","shell.execute_reply":"2023-02-23T23:09:51.045835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(pd.read_csv('submission.csv').head())","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:10:53.496056Z","iopub.execute_input":"2023-02-23T23:10:53.496435Z","iopub.status.idle":"2023-02-23T23:10:53.508948Z","shell.execute_reply.started":"2023-02-23T23:10:53.496403Z","shell.execute_reply":"2023-02-23T23:10:53.507826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}