{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"raw","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\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"}},{"cell_type":"markdown","source":"Thank you Abhishek Thakur for your book Approaching (Almost) Any Machine Learning Problem!","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn import linear_model\nfrom sklearn import metrics\nfrom sklearn import preprocessing","metadata":{"execution":{"iopub.status.busy":"2022-12-12T11:29:44.117605Z","iopub.execute_input":"2022-12-12T11:29:44.118120Z","iopub.status.idle":"2022-12-12T11:29:44.124456Z","shell.execute_reply.started":"2022-12-12T11:29:44.118078Z","shell.execute_reply":"2022-12-12T11:29:44.123158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# in Create folds notebook we made dataset with folds\ndf = pd.read_csv(\"/kaggle/input/logistic-regression/folds.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-12T11:29:46.198360Z","iopub.execute_input":"2022-12-12T11:29:46.199129Z","iopub.status.idle":"2022-12-12T11:29:51.133353Z","shell.execute_reply.started":"2022-12-12T11:29:46.199085Z","shell.execute_reply":"2022-12-12T11:29:51.131570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T11:31:35.371266Z","iopub.execute_input":"2022-12-12T11:31:35.371628Z","iopub.status.idle":"2022-12-12T11:31:35.400605Z","shell.execute_reply.started":"2022-12-12T11:31:35.371596Z","shell.execute_reply":"2022-12-12T11:31:35.399661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run(fold):\n    # all columns are features, except id, target and kfold\n    features = [f for f in df.columns if f not in (\"id\", \"target\", \"kfold\")]\n    \n    # fill all NaN values with NONE\n    # I am converting all columns to strings\n    # it does not matter because all are categories\n    for col in features:\n        df.loc[:, col] = df[col].astype(str).fillna(\"NONE\")\n        \n    # get trainint data using folds\n    df_train = df[df.kfold != fold].reset_index(drop=True)\n    \n    # get test data using folds\n    df_valid = df[df.kfold == fold].reset_index(drop=True)\n    \n    # initialize OneHotEncoder from scikit-learn\n    ohe = preprocessing.OneHotEncoder()\n    \n    # fit ohe on training + validation features\n    full_data = pd.concat([df_train[features], df_valid[features]], axis=0)\n    ohe.fit(full_data[features])\n    \n    # transform training data\n    x_train = ohe.transform(df_train[features])\n    \n    # transform test data\n    x_valid = ohe.transform(df_valid[features])\n    \n    # initialize Logistic Regression model\n    model = linear_model.LogisticRegression()\n    \n    # fit model on training data (ohe)\n    model.fit(x_train, df_train.target.values)\n    \n    # predict on validation data\n    # we need the probabilitiy values as we are calculating AUC\n    # we will use the probability of 1s\n    valid_preds = model.predict_proba(x_valid)[:, 1]\n    \n    # get roc auc score\n    auc = metrics.roc_auc_score(df_valid.target.values, valid_preds)\n    \n    # print auc\n    print(auc)\n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-12-12T11:49:49.536394Z","iopub.execute_input":"2022-12-12T11:49:49.536915Z","iopub.status.idle":"2022-12-12T11:49:49.549956Z","shell.execute_reply.started":"2022-12-12T11:49:49.536866Z","shell.execute_reply":"2022-12-12T11:49:49.548474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if __name__ == \"__main__\":\n    run(0)","metadata":{"execution":{"iopub.status.busy":"2022-12-12T11:49:52.535925Z","iopub.execute_input":"2022-12-12T11:49:52.537170Z","iopub.status.idle":"2022-12-12T11:50:10.991237Z","shell.execute_reply.started":"2022-12-12T11:49:52.537125Z","shell.execute_reply":"2022-12-12T11:50:10.989064Z"},"trusted":true},"execution_count":null,"outputs":[]}]}