{"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":"markdown","source":"#### I tried to make something complex by using this method, but i guess simplicity is always the 'gor for' to start even tho a got a decent score\n#### Follow me so i can explain what i  did xD","metadata":{}},{"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)\nimport matplotlib.pyplot as plt\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":{"execution":{"iopub.status.busy":"2022-07-14T14:05:03.807857Z","iopub.execute_input":"2022-07-14T14:05:03.808371Z","iopub.status.idle":"2022-07-14T14:05:03.817740Z","shell.execute_reply.started":"2022-07-14T14:05:03.808331Z","shell.execute_reply":"2022-07-14T14:05:03.816577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')\ntest = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:03.819695Z","iopub.execute_input":"2022-07-14T14:05:03.820375Z","iopub.status.idle":"2022-07-14T14:05:03.924214Z","shell.execute_reply.started":"2022-07-14T14:05:03.820341Z","shell.execute_reply":"2022-07-14T14:05:03.923266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:03.928574Z","iopub.execute_input":"2022-07-14T14:05:03.930881Z","iopub.status.idle":"2022-07-14T14:05:03.974820Z","shell.execute_reply.started":"2022-07-14T14:05:03.930841Z","shell.execute_reply":"2022-07-14T14:05:03.972399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking missing values","metadata":{}},{"cell_type":"code","source":"for col in train:\n    if train[col].isnull().sum() != 0:\n        print(f'{col} : {train[col].isnull().sum()} null values | Dtype : {train[col].dtype}')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:03.975867Z","iopub.execute_input":"2022-07-14T14:05:03.976361Z","iopub.status.idle":"2022-07-14T14:05:04.019002Z","shell.execute_reply.started":"2022-07-14T14:05:03.976330Z","shell.execute_reply":"2022-07-14T14:05:04.018102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = train.isnull().sum().sort_values(ascending=False)\ntotal_select = total.head(20)\ntotal_select.plot(kind=\"bar\", figsize = (8,6), fontsize = 10)\n\nplt.xlabel(\"Columns\", fontsize = 20)\nplt.ylabel(\"Count\", fontsize = 20)\nplt.title(\"Total Missing Values\", fontsize = 20)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:04.024341Z","iopub.execute_input":"2022-07-14T14:05:04.025290Z","iopub.status.idle":"2022-07-14T14:05:04.379513Z","shell.execute_reply.started":"2022-07-14T14:05:04.025255Z","shell.execute_reply":"2022-07-14T14:05:04.378538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:04.380882Z","iopub.execute_input":"2022-07-14T14:05:04.381327Z","iopub.status.idle":"2022-07-14T14:05:04.542275Z","shell.execute_reply.started":"2022-07-14T14:05:04.381283Z","shell.execute_reply":"2022-07-14T14:05:04.541252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Gathering columns that contain missing values so we can use them later","metadata":{}},{"cell_type":"code","source":"df_nulls = train[['LotFrontage', \n'Alley',\n'MasVnrType',\n'MasVnrArea',\n'BsmtQual',\n'BsmtCond',\n'BsmtExposure',\n'BsmtFinType1',\n'BsmtFinType2',\n'Electrical',\n'FireplaceQu',\n'GarageType',\n'GarageYrBlt',\n'GarageFinish',\n'GarageQual',\n'GarageCond',\n'PoolQC',\n'Fence',\n'MiscFeature']]\n\ndf_nulls","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:04.546274Z","iopub.execute_input":"2022-07-14T14:05:04.546575Z","iopub.status.idle":"2022-07-14T14:05:04.592143Z","shell.execute_reply.started":"2022-07-14T14:05:04.546549Z","shell.execute_reply":"2022-07-14T14:05:04.591323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['SalePrice'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:04.593415Z","iopub.execute_input":"2022-07-14T14:05:04.594042Z","iopub.status.idle":"2022-07-14T14:05:04.605370Z","shell.execute_reply.started":"2022-07-14T14:05:04.594007Z","shell.execute_reply":"2022-07-14T14:05:04.604373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['SaleCondition'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:04.606847Z","iopub.execute_input":"2022-07-14T14:05:04.607250Z","iopub.status.idle":"2022-07-14T14:05:04.615317Z","shell.execute_reply.started":"2022-07-14T14:05:04.607219Z","shell.execute_reply":"2022-07-14T14:05:04.614534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sorting Correlated features to the SalePrice","metadata":{}},{"cell_type":"code","source":"hous_num = train.select_dtypes(include = ['float64', 'int64'])\nhous_num_corr = hous_num.corr()['SalePrice'][:-1]\ntop_features = hous_num_corr[abs(hous_num_corr) > 0.5].sort_values(ascending=False)\nprint(\"There is {} strongly correlated values with SalePrice:\\n{}\".format(len(top_features), top_features))","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:04.616572Z","iopub.execute_input":"2022-07-14T14:05:04.617046Z","iopub.status.idle":"2022-07-14T14:05:04.640107Z","shell.execute_reply.started":"2022-07-14T14:05:04.617018Z","shell.execute_reply":"2022-07-14T14:05:04.638723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualizing to better understand correlations","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nfor i in range(0, len(hous_num.columns), 5):\n    sns.pairplot(data=hous_num,\n                x_vars=hous_num.columns[i:i+5],\n                y_vars=['SalePrice'])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:04.641541Z","iopub.execute_input":"2022-07-14T14:05:04.642429Z","iopub.status.idle":"2022-07-14T14:05:11.198416Z","shell.execute_reply.started":"2022-07-14T14:05:04.642397Z","shell.execute_reply":"2022-07-14T14:05:11.197330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_features = train.select_dtypes(include = ['object'])\ncategorical_features","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:11.199943Z","iopub.execute_input":"2022-07-14T14:05:11.200279Z","iopub.status.idle":"2022-07-14T14:05:11.230977Z","shell.execute_reply.started":"2022-07-14T14:05:11.200250Z","shell.execute_reply":"2022-07-14T14:05:11.229858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cat in categorical_features:\n    print(f'{cat} : {train[cat].nunique()} unique values')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:11.232731Z","iopub.execute_input":"2022-07-14T14:05:11.233447Z","iopub.status.idle":"2022-07-14T14:05:11.248875Z","shell.execute_reply.started":"2022-07-14T14:05:11.233404Z","shell.execute_reply":"2022-07-14T14:05:11.247664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Endoding categorical features and leaving the NaN","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\ndef encoding(column):\n    df_1 = train.copy()\n    original = df_1\n    mask = df_1[column].isnull()\n    df_1 = df_1.astype(str).apply(LabelEncoder().fit_transform)\n    train[column] = df_1.where(~mask, original)[column]\nfor cat in categorical_features:\n    encoding(cat)\ntrain.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:11.254049Z","iopub.execute_input":"2022-07-14T14:05:11.254715Z","iopub.status.idle":"2022-07-14T14:05:19.020723Z","shell.execute_reply.started":"2022-07-14T14:05:11.254668Z","shell.execute_reply":"2022-07-14T14:05:19.019606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Gathering columns with 0 missing values","metadata":{}},{"cell_type":"code","source":"non_null_cols = []\nfor col in train.drop(['Id','SalePrice'], axis=1):\n    if train[col].isnull().sum() == 0:\n        non_null_cols.append(col)\nnon_null_cols","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:19.022342Z","iopub.execute_input":"2022-07-14T14:05:19.022781Z","iopub.status.idle":"2022-07-14T14:05:19.056355Z","shell.execute_reply.started":"2022-07-14T14:05:19.022739Z","shell.execute_reply":"2022-07-14T14:05:19.055498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Here comes the moneey","metadata":{}},{"cell_type":"markdown","source":"### so i thought about predicting the NaN values depending on the other columns","metadata":{}},{"cell_type":"markdown","source":"This first method returns the column with no missing values as a target that i'm gonna use to train the model","metadata":{}},{"cell_type":"code","source":"def all_except_for_nan(column):\n    df_1 = train.copy()\n\n    original = df_1\n    mask = df_1[column].isnull()\n\n    df_1 = df_1.astype(str).apply(LabelEncoder().fit_transform)\n    return df_1.where(~mask, original)[column].dropna().values","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:19.057404Z","iopub.execute_input":"2022-07-14T14:05:19.058114Z","iopub.status.idle":"2022-07-14T14:05:19.063911Z","shell.execute_reply.started":"2022-07-14T14:05:19.058082Z","shell.execute_reply":"2022-07-14T14:05:19.062542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"And this method train a model for each column to predict its missing values  \n","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.linear_model import LogisticRegression\ndef predict_NaN_value(column):\n    X_train_on_null = train[train[column].isna() == False][non_null_cols].values.astype(int)\n    y_train_on_null = all_except_for_nan(column).astype(int)\n    # Checking if column is categorical or numeric\n    if column in categorical_features.columns:\n        # training a classification model\n        lr = LogisticRegression()\n        lr.fit(X_train_on_null, y_train_on_null)\n        # looping on each cell and predicting\n        for i in range(0, len(train[column])):\n            if pd.isna(train[column][i]):\n                train[column][i] = lr.predict([train[non_null_cols].iloc[i].astype(int)])\n            else:\n                pass\n    else:\n        # Same but for numeric columns\n        regressor = LinearRegression()\n        regressor.fit(X_train_on_null, y_train_on_null)\n        for i in range(0, len(train[column])):\n            if pd.isna(train[column][i]):\n                train[column][i] = regressor.predict([train[non_null_cols].iloc[i]])\n            else:\n                pass","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:19.065692Z","iopub.execute_input":"2022-07-14T14:05:19.066705Z","iopub.status.idle":"2022-07-14T14:05:19.210734Z","shell.execute_reply.started":"2022-07-14T14:05:19.066660Z","shell.execute_reply":"2022-07-14T14:05:19.209612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in df_nulls.columns:\n    predict_NaN_value(col)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:19.212145Z","iopub.execute_input":"2022-07-14T14:05:19.212538Z","iopub.status.idle":"2022-07-14T14:05:37.270174Z","shell.execute_reply.started":"2022-07-14T14:05:19.212503Z","shell.execute_reply":"2022-07-14T14:05:37.268965Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:37.271757Z","iopub.execute_input":"2022-07-14T14:05:37.272540Z","iopub.status.idle":"2022-07-14T14:05:37.296121Z","shell.execute_reply.started":"2022-07-14T14:05:37.272493Z","shell.execute_reply":"2022-07-14T14:05:37.295028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:37.297265Z","iopub.execute_input":"2022-07-14T14:05:37.297577Z","iopub.status.idle":"2022-07-14T14:05:37.325905Z","shell.execute_reply.started":"2022-07-14T14:05:37.297548Z","shell.execute_reply":"2022-07-14T14:05:37.324785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Some of the predictions were into arrays so i had to convert them","metadata":{}},{"cell_type":"code","source":"objects = train.select_dtypes(include = ['object'])\nfor ob in objects.columns:\n    for i in range(0,len(train[ob])):\n        train[ob][i] = int(train[ob][i])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:37.327282Z","iopub.execute_input":"2022-07-14T14:05:37.327608Z","iopub.status.idle":"2022-07-14T14:05:43.988891Z","shell.execute_reply.started":"2022-07-14T14:05:37.327579Z","shell.execute_reply":"2022-07-14T14:05:43.987943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:43.991090Z","iopub.execute_input":"2022-07-14T14:05:43.991560Z","iopub.status.idle":"2022-07-14T14:05:44.015551Z","shell.execute_reply.started":"2022-07-14T14:05:43.991517Z","shell.execute_reply":"2022-07-14T14:05:44.014283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Doing all the previous work for the test set","metadata":{}},{"cell_type":"code","source":"df_nulls = []\nfor col in test.columns:\n    if test[col].isnull().sum() != 0:\n        df_nulls.append(col)\n\ncategorical_features = test.select_dtypes(include = ['object'])\ncategorical_features\n\ndef encoding(column):\n    df_1 = test.copy()\n\n    original = df_1\n    mask = df_1[column].isnull()\n\n    df_1 = df_1.astype(str).apply(LabelEncoder().fit_transform)\n    test[column] = df_1.where(~mask, original)[column]\nfor cat in categorical_features:\n    encoding(cat)\n\ntest['Utilities'].fillna(value=test['Utilities'].mean())\ncategorical_features = categorical_features.drop('Utilities', axis=1)\n\nnon_null_cols = []\nfor col in test.drop(['Id'], axis=1):\n    if test[col].isnull().sum() == 0:\n        non_null_cols.append(col)\nnon_null_cols\n\ndef all_except_for_nan(column):\n    df_1 = test.copy()\n\n    original = df_1\n    mask = df_1[column].isnull()\n\n    df_1 = df_1.astype(str).apply(LabelEncoder().fit_transform)\n    return df_1.where(~mask, original)[column].dropna().values\n\ndef predict_NaN_value(column):\n    X_test_on_null = test[test[column].isna() == False][non_null_cols].values.astype(int)\n    y_test_on_null = all_except_for_nan(column).astype(int)\n    if column in categorical_features.columns:\n        lreg = LogisticRegression()\n        lreg.fit(X_test_on_null, y_test_on_null)\n        for i in range(0, len(test[column])):\n            if pd.isna(test[column][i]):\n                test[column][i] = lreg.predict([test[non_null_cols].iloc[i].astype(int)])\n            else:\n                pass\n    else:\n        reg = LinearRegression()\n        reg.fit(X_test_on_null, y_test_on_null)\n        for i in range(0, len(test[column])):\n            if pd.isna(test[column][i]):\n                test[column][i] = reg.predict([test[non_null_cols].iloc[i]])\n            else:\n                pass\n\nfor col in df_nulls:\n    predict_NaN_value(col)\n\nobjects = test.select_dtypes(include = ['object'])\nfor ob in objects.columns:\n    for i in range(0,len(test[ob])):\n        test[ob][i] = int(test[ob][i])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:05:44.016996Z","iopub.execute_input":"2022-07-14T14:05:44.017317Z","iopub.status.idle":"2022-07-14T14:06:23.623632Z","shell.execute_reply.started":"2022-07-14T14:05:44.017290Z","shell.execute_reply":"2022-07-14T14:06:23.622180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:06:23.625325Z","iopub.execute_input":"2022-07-14T14:06:23.626698Z","iopub.status.idle":"2022-07-14T14:06:23.660635Z","shell.execute_reply.started":"2022-07-14T14:06:23.626640Z","shell.execute_reply":"2022-07-14T14:06:23.659509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"regressor = LinearRegression()\nX_train = train.drop(['Id', 'SalePrice'], axis=1).values\ny_train = train['SalePrice'].values\nregressor.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:06:23.662095Z","iopub.execute_input":"2022-07-14T14:06:23.662853Z","iopub.status.idle":"2022-07-14T14:06:23.713128Z","shell.execute_reply.started":"2022-07-14T14:06:23.662806Z","shell.execute_reply":"2022-07-14T14:06:23.711714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test.drop('Id', axis=1).values\npred = pd.DataFrame()\npred['Id'] = test['Id']\npred['SalePrice'] = regressor.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:06:23.715184Z","iopub.execute_input":"2022-07-14T14:06:23.716013Z","iopub.status.idle":"2022-07-14T14:06:23.831100Z","shell.execute_reply.started":"2022-07-14T14:06:23.715963Z","shell.execute_reply":"2022-07-14T14:06:23.829893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pred.to_csv('subbmission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T14:06:23.832832Z","iopub.execute_input":"2022-07-14T14:06:23.833640Z","iopub.status.idle":"2022-07-14T14:06:23.847421Z","shell.execute_reply.started":"2022-07-14T14:06:23.833597Z","shell.execute_reply":"2022-07-14T14:06:23.843236Z"},"trusted":true},"execution_count":null,"outputs":[]}]}