{"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":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom sklearn.preprocessing import LabelEncoder\nimport lightgbm as lgbm\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-14T04:19:18.970636Z","iopub.execute_input":"2022-07-14T04:19:18.971138Z","iopub.status.idle":"2022-07-14T04:19:18.978316Z","shell.execute_reply.started":"2022-07-14T04:19:18.971101Z","shell.execute_reply":"2022-07-14T04:19:18.976853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/house-prices-advanced-regression-techniques/train.csv')\ntest = pd.read_csv('../input/house-prices-advanced-regression-techniques/test.csv')\nsubmission = pd.read_csv(\"../input/house-prices-advanced-regression-techniques/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:21.191051Z","iopub.execute_input":"2022-07-14T04:19:21.191436Z","iopub.status.idle":"2022-07-14T04:19:21.241480Z","shell.execute_reply.started":"2022-07-14T04:19:21.191405Z","shell.execute_reply":"2022-07-14T04:19:21.240611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Type']='Train'\ntest['Type']='Test'","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:25.360952Z","iopub.execute_input":"2022-07-14T04:19:25.361461Z","iopub.status.idle":"2022-07-14T04:19:25.370253Z","shell.execute_reply.started":"2022-07-14T04:19:25.361417Z","shell.execute_reply":"2022-07-14T04:19:25.368880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Columnas que no pueden tener NA\n\ndf = df[df['LotFrontage'].notna()] \ndf = df[df['MasVnrType'].notna()] \ndf = df[df['Electrical'].notna()] \ndf.drop(columns=[\"Id\"],inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:28.208413Z","iopub.execute_input":"2022-07-14T04:19:28.209724Z","iopub.status.idle":"2022-07-14T04:19:28.225231Z","shell.execute_reply.started":"2022-07-14T04:19:28.209672Z","shell.execute_reply":"2022-07-14T04:19:28.223655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.box(df, y=\"SalePrice\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:34.539614Z","iopub.execute_input":"2022-07-14T04:19:34.540109Z","iopub.status.idle":"2022-07-14T04:19:34.610598Z","shell.execute_reply.started":"2022-07-14T04:19:34.540073Z","shell.execute_reply":"2022-07-14T04:19:34.609430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Removing the outliers\ndf =  df[df['SalePrice']<341000]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:37.172200Z","iopub.execute_input":"2022-07-14T04:19:37.172622Z","iopub.status.idle":"2022-07-14T04:19:37.183075Z","shell.execute_reply.started":"2022-07-14T04:19:37.172590Z","shell.execute_reply":"2022-07-14T04:19:37.181345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['GarageYrBlt'] = pd.to_numeric(df[\"GarageYrBlt\"])\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:38.354635Z","iopub.execute_input":"2022-07-14T04:19:38.355272Z","iopub.status.idle":"2022-07-14T04:19:38.388728Z","shell.execute_reply.started":"2022-07-14T04:19:38.355221Z","shell.execute_reply":"2022-07-14T04:19:38.387071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"matrix=pd.concat([df,test]).reset_index(drop=True).drop(columns='Id')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:40.915273Z","iopub.execute_input":"2022-07-14T04:19:40.915784Z","iopub.status.idle":"2022-07-14T04:19:40.957296Z","shell.execute_reply.started":"2022-07-14T04:19:40.915739Z","shell.execute_reply":"2022-07-14T04:19:40.956001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Compare_types(df1, df2,matrix):\n    for column in matrix.columns: \n        if column!=\"SalePrice\":\n            type1=df1[column].dtype\n            type2=df2[column].dtype\n            if type1!=type2:\n                print(f\"la columna {column} es diferente\")\n    return \nCompare_types(df, test,matrix)   ","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:42.024851Z","iopub.execute_input":"2022-07-14T04:19:42.025306Z","iopub.status.idle":"2022-07-14T04:19:42.040826Z","shell.execute_reply.started":"2022-07-14T04:19:42.025272Z","shell.execute_reply":"2022-07-14T04:19:42.039875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Setting the columns with the same data type\n\nmatrix[\"BsmtFinSF1\"] = matrix[\"BsmtFinSF1\"].astype(np.float64)\nmatrix[\"BsmtFinSF2\"] = matrix[\"BsmtFinSF2\"].astype(np.float64)\nmatrix[\"BsmtUnfSF\"] = matrix[\"BsmtUnfSF\"].astype(np.float64)\nmatrix[\"TotalBsmtSF\"] = matrix[\"TotalBsmtSF\"].astype(np.float64)\nmatrix[\"BsmtFullBath\"] = matrix[\"BsmtFullBath\"].astype(np.float64)\nmatrix[\"BsmtHalfBath\"] = matrix[\"BsmtHalfBath\"].astype(np.float64)\nmatrix[\"GarageCars\"] = matrix[\"GarageCars\"].astype(np.float64)\nmatrix[\"GarageArea\"] = matrix[\"GarageArea\"].astype(np.float64)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:43.719504Z","iopub.execute_input":"2022-07-14T04:19:43.720246Z","iopub.status.idle":"2022-07-14T04:19:43.730856Z","shell.execute_reply.started":"2022-07-14T04:19:43.720207Z","shell.execute_reply":"2022-07-14T04:19:43.729947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Encoder (df):\n    objects = ['object']\n    temp = df.select_dtypes(include=objects)\n    temp.drop(columns='Type',inplace=True)\n    for column in temp.columns:\n        df[column]=LabelEncoder().fit_transform(df[column])\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:45.320669Z","iopub.execute_input":"2022-07-14T04:19:45.321129Z","iopub.status.idle":"2022-07-14T04:19:45.329646Z","shell.execute_reply.started":"2022-07-14T04:19:45.321094Z","shell.execute_reply":"2022-07-14T04:19:45.327608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2=Encoder (matrix)\ndf2=df2.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:46.571666Z","iopub.execute_input":"2022-07-14T04:19:46.572277Z","iopub.status.idle":"2022-07-14T04:19:46.650860Z","shell.execute_reply.started":"2022-07-14T04:19:46.572225Z","shell.execute_reply":"2022-07-14T04:19:46.650044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:48.196970Z","iopub.execute_input":"2022-07-14T04:19:48.197613Z","iopub.status.idle":"2022-07-14T04:19:48.221256Z","shell.execute_reply.started":"2022-07-14T04:19:48.197560Z","shell.execute_reply":"2022-07-14T04:19:48.220120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set=df2[df2['Type']=='Train'].drop(columns='Type')\ntest_set=df2[df2['Type']=='Test'].drop(columns='Type')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:49.771701Z","iopub.execute_input":"2022-07-14T04:19:49.772163Z","iopub.status.idle":"2022-07-14T04:19:49.784578Z","shell.execute_reply.started":"2022-07-14T04:19:49.772128Z","shell.execute_reply":"2022-07-14T04:19:49.783247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_set.loc[0:900].drop(columns=['SalePrice'])\nY_train = train_set.loc[0:900]['SalePrice']\nX_valid = train_set.loc[900:].drop(columns=['SalePrice'])\nY_valid = train_set.loc[900:]['SalePrice']\nX_test = test_set.drop(columns=['SalePrice'])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:51.051785Z","iopub.execute_input":"2022-07-14T04:19:51.052277Z","iopub.status.idle":"2022-07-14T04:19:51.066290Z","shell.execute_reply.started":"2022-07-14T04:19:51.052240Z","shell.execute_reply":"2022-07-14T04:19:51.064815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = lgbm.Dataset(data=X_train, label=Y_train)\nvalid_data = lgbm.Dataset(data=X_valid, label=Y_valid)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:19:52.637140Z","iopub.execute_input":"2022-07-14T04:19:52.637506Z","iopub.status.idle":"2022-07-14T04:19:52.644504Z","shell.execute_reply.started":"2022-07-14T04:19:52.637477Z","shell.execute_reply":"2022-07-14T04:19:52.642899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# configuracion de los parametros\nparams = {\"objective\" : \"regression\", \n          \"metric\" : \"rmse\", \n          'n_estimators':10000, \n          'early_stopping_rounds':50,\n          \"num_leaves\" : 2**7-1, \n          \"learning_rate\" : 0.01, \n          \"bagging_fraction\" : 0.9,\n          \"feature_fraction\" : 0.3, \n          \"bagging_seed\" : 0,\n          'verbose': -1,\n         }\n         \n         \n          \n# entrenamiento\nlgbm_model = lgbm.train(params, \n                        train_data, \n                        valid_sets=[train_data, valid_data], \n                        verbose_eval=1000,\n                        ) ","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:22:36.939580Z","iopub.execute_input":"2022-07-14T04:22:36.940193Z","iopub.status.idle":"2022-07-14T04:22:53.576131Z","shell.execute_reply.started":"2022-07-14T04:22:36.940143Z","shell.execute_reply":"2022-07-14T04:22:53.574878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred= lgbm_model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:25:43.805383Z","iopub.execute_input":"2022-07-14T04:25:43.805872Z","iopub.status.idle":"2022-07-14T04:25:45.583654Z","shell.execute_reply.started":"2022-07-14T04:25:43.805836Z","shell.execute_reply":"2022-07-14T04:25:45.582561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'Id': test['Id'], 'SalePrice': Y_pred})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:25:45.585819Z","iopub.execute_input":"2022-07-14T04:25:45.586716Z","iopub.status.idle":"2022-07-14T04:25:45.605409Z","shell.execute_reply.started":"2022-07-14T04:25:45.586667Z","shell.execute_reply":"2022-07-14T04:25:45.604121Z"},"trusted":true},"execution_count":null,"outputs":[]}]}