{"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\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","execution":{"iopub.status.busy":"2022-08-02T14:41:42.871976Z","iopub.execute_input":"2022-08-02T14:41:42.872507Z","iopub.status.idle":"2022-08-02T14:41:42.881909Z","shell.execute_reply.started":"2022-08-02T14:41:42.872469Z","shell.execute_reply":"2022-08-02T14:41:42.880503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import RobustScaler, PowerTransformer\nfrom sklearn import preprocessing\nfrom sklearn.cluster import KMeans\nfrom matplotlib import cm\nfrom sklearn import metrics\nfrom sklearn.impute import SimpleImputer\nfrom scipy import stats","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:41:45.019517Z","iopub.execute_input":"2022-08-02T14:41:45.020578Z","iopub.status.idle":"2022-08-02T14:41:45.027404Z","shell.execute_reply.started":"2022-08-02T14:41:45.020533Z","shell.execute_reply":"2022-08-02T14:41:45.026189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Previous_results\nAbout Missing Value: https://www.kaggle.com/code/makotouchiyama/english-eda-1-missingval-r-tps-202208\n\nAbout FeatureSelection: https://www.kaggle.com/code/makotouchiyama/english-eda-2-prefeatureselect-tps-2208","metadata":{}},{"cell_type":"markdown","source":"# First trial of constructing model using LightGBM\n\n・Non-hyperparameter-tuning\n\n・Non cross validation\n\n\nLightGBMを用いた学習モデルの構築（最初のトライ）\n\n・ハイパーパラメータチューニングなし\n\n・クロスバリデーションなし","metadata":{}},{"cell_type":"code","source":"# data_import\ndata_path_train = '../input/tabular-playground-series-aug-2022/train.csv'\ndata_path_test = '../input/tabular-playground-series-aug-2022/test.csv'\ndata_path_sample_submission = '../input/tabular-playground-series-aug-2022/sample_submission.csv'\n\ndf_train = pd.read_csv(data_path_train)\ndf_test = pd.read_csv(data_path_test)\nsubmission=pd.read_csv(data_path_sample_submission)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:41:47.566477Z","iopub.execute_input":"2022-08-02T14:41:47.566910Z","iopub.status.idle":"2022-08-02T14:41:47.778859Z","shell.execute_reply.started":"2022-08-02T14:41:47.566878Z","shell.execute_reply":"2022-08-02T14:41:47.777694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# imputed by mode value\ndf_ip_train = pd.DataFrame(SimpleImputer(strategy='most_frequent').fit_transform(df_train),columns=df_train.columns)\ndf_ip_test = pd.DataFrame(SimpleImputer(strategy='most_frequent').fit_transform(df_test),columns=df_test.columns)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:41:50.879435Z","iopub.execute_input":"2022-08-02T14:41:50.879880Z","iopub.status.idle":"2022-08-02T14:41:51.484712Z","shell.execute_reply.started":"2022-08-02T14:41:50.879844Z","shell.execute_reply":"2022-08-02T14:41:51.483311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \nX_train = df_ip_train.drop(columns=['id','product_code','attribute_0','attribute_1','failure'])\nY_train = df_ip_train['failure']","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:06:18.377695Z","iopub.execute_input":"2022-08-02T14:06:18.378152Z","iopub.status.idle":"2022-08-02T14:06:18.391556Z","shell.execute_reply.started":"2022-08-02T14:06:18.378105Z","shell.execute_reply":"2022-08-02T14:06:18.390158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.astype({'loading': 'float', 'attribute_2': 'float',\n                'attribute_3': 'float','measurement_0': 'float',\n                'measurement_1': 'float', 'measurement_2': 'float',\n                'measurement_3': 'float', 'measurement_4': 'float',\n                'measurement_5': 'float', 'measurement_6': 'float',\n                'measurement_7': 'float', 'measurement_8': 'float',\n                'measurement_9': 'float', 'measurement_10': 'float',\n                'measurement_11': 'float', 'measurement_12': 'float',\n                'measurement_13': 'float', 'measurement_14': 'float',\n                'measurement_15': 'float', 'measurement_16': 'float',\n                'measurement_17': 'float'})\n\nY_train = Y_train.astype('bool')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:11:13.254107Z","iopub.execute_input":"2022-08-02T14:11:13.254596Z","iopub.status.idle":"2022-08-02T14:11:13.272460Z","shell.execute_reply.started":"2022-08-02T14:11:13.254556Z","shell.execute_reply":"2022-08-02T14:11:13.271566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split # データセット分割用\nx_train, x_test, y_train, y_test = train_test_split(X_train, Y_train,test_size=0.20, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:11:15.591401Z","iopub.execute_input":"2022-08-02T14:11:15.591869Z","iopub.status.idle":"2022-08-02T14:11:15.613840Z","shell.execute_reply.started":"2022-08-02T14:11:15.591832Z","shell.execute_reply":"2022-08-02T14:11:15.612585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import library\nimport pandas as pd \nimport numpy as np \nimport matplotlib.pyplot as plt \nimport seaborn as sns; sns.set() \nimport warnings \nwarnings.filterwarnings('ignore')\nimport lightgbm as lgb #LightGBM\nfrom sklearn import datasets\nfrom sklearn.model_selection import train_test_split \nfrom sklearn.metrics import accuracy_score \nfrom sklearn.metrics import log_loss    \nfrom sklearn.metrics import roc_auc_score \nfrom sklearn import metrics\nfrom sklearn.metrics import roc_curve","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:11:17.778441Z","iopub.execute_input":"2022-08-02T14:11:17.779843Z","iopub.status.idle":"2022-08-02T14:11:17.789364Z","shell.execute_reply.started":"2022-08-02T14:11:17.779788Z","shell.execute_reply":"2022-08-02T14:11:17.788532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlgb_train = lgb.Dataset(x_train, y_train)\nlgb_eval = lgb.Dataset(x_test, y_test, reference=lgb_train)\n\n# LightGBM parameters\nparams = {\n'task': 'train',\n'boosting_type': 'gbdt',\n'objective': 'binary', # binary\n'metric': {'binary_error'}, \n}\n\n# model trainging\nmodel = lgb.train(params,\ntrain_set=lgb_train, \nvalid_sets=lgb_eval,\n)\n\ny_pred_prob = model.predict(x_test)\ny_pred = np.where(y_pred_prob < 0.5, 0, 1)\n\ndf_pred = pd.DataFrame({'target':y_test,'target_pred':y_pred})\n\ndf_pred_prob = pd.DataFrame({'target':y_test, 'target0_prob':1-y_pred_prob, 'target1_prob':y_pred_prob})\n\n\n\n# Evaluation\n# acc : accuracy\nacc = accuracy_score(y_test,y_pred)\nprint('Acc :', acc)\n\n# logloss \nlogloss =  log_loss(y_test,y_pred_prob) \nprint('logloss :', logloss)\n\n# AUC \nauc = roc_auc_score(y_test,y_pred_prob) \nprint('AUC :', auc) \n\n\n# ROC\nfpr, tpr, thresholds = roc_curve(y_test,y_pred_prob)\nauc = metrics.auc(fpr, tpr)\nplt.plot(fpr, tpr, label='ROC curve (area = %.2f)'%auc)\nplt.legend()\nplt.xlabel('FPR: False positive rate')\nplt.ylabel('TPR: True positive rate')\nplt.grid()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:11:19.853772Z","iopub.execute_input":"2022-08-02T14:11:19.855006Z","iopub.status.idle":"2022-08-02T14:11:20.621887Z","shell.execute_reply.started":"2022-08-02T14:11:19.854944Z","shell.execute_reply":"2022-08-02T14:11:20.620593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = df_ip_test.drop(columns=['id','product_code','attribute_0','attribute_1'])\n\nX_test = X_test.astype({'loading': 'float', 'attribute_2': 'float',\n                'attribute_3': 'float','measurement_0': 'float',\n                'measurement_1': 'float', 'measurement_2': 'float',\n                'measurement_3': 'float', 'measurement_4': 'float',\n                'measurement_5': 'float', 'measurement_6': 'float',\n                'measurement_7': 'float', 'measurement_8': 'float',\n                'measurement_9': 'float', 'measurement_10': 'float',\n                'measurement_11': 'float', 'measurement_12': 'float',\n                'measurement_13': 'float', 'measurement_14': 'float',\n                'measurement_15': 'float', 'measurement_16': 'float',\n                'measurement_17': 'float'})\n\n\ny_pred_prob_test = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:22:35.749499Z","iopub.execute_input":"2022-08-02T14:22:35.750008Z","iopub.status.idle":"2022-08-02T14:22:35.847513Z","shell.execute_reply.started":"2022-08-02T14:22:35.749970Z","shell.execute_reply":"2022-08-02T14:22:35.840084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission\nsubmission['failure']=y_pred_prob_test\nsubmission.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T14:22:38.857791Z","iopub.execute_input":"2022-08-02T14:22:38.858240Z","iopub.status.idle":"2022-08-02T14:22:38.864950Z","shell.execute_reply.started":"2022-08-02T14:22:38.858201Z","shell.execute_reply":"2022-08-02T14:22:38.863521Z"},"trusted":true},"execution_count":null,"outputs":[]}]}