<jupyter_start><jupyter_text>Load Data<jupyter_code>train_data = pd.read_csv('./data/train.csv')
test_data = pd.read_csv('./data/test.csv')<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis<jupyter_code>train_data.info()

import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(10,8))
sns.heatmap(train_data.corr(), center = 0)
plt.title("Correlations Between Columns")
plt.show()<jupyter_output><empty_output><jupyter_text>Split input and target variables<jupyter_code>y = train_data.SalePrice
X = train_data.drop(columns=["SalePrice"], axis=1)
y.shape, X.shape, test_data.shape<jupyter_output><empty_output><jupyter_text>Choose only the significant features, discard those with correlation score < 0.5 with the target variable<jupyter_code>corr_matrix = train_data.corr()
corr_matrix['SalePrice'][(corr_matrix["SalePrice"] > 0.40) | (corr_matrix["SalePrice"] < -0.40)]
important_num_cols = list(corr_matrix['SalePrice'][(corr_matrix["SalePrice"] > 0.5) | (corr_matrix["SalePrice"] < -0.5)].index)

important_num_cols.remove('SalePrice')
len(important_num_cols)
important_num_cols
X_num_only = X[important_num_cols]
X_num_only.shape<jupyter_output><empty_output><jupyter_text>Remove the feautures which are highly correlated with each other<jupyter_code>plt.figure(figsize=(10,8))
sns.heatmap(X_num_only.corr(), center = 0)
plt.title("Correlations Between Columns")
plt.show()
corr_X = X_num_only.corr()
len(corr_X)

for i in range(0, len(corr_X) - 1):
    for j in range(i + 1, len(corr_X)):
        if(corr_X.iloc[i, j] < -0.6 or corr_X.iloc[i, j] > 0.6):
            print(corr_X.iloc[i, j], i, j, corr_X.index[i], corr_X.index[j])
            
# Based on the above information, we further discard the features 1stFlrSF, FullBath, TotRmsAbvGrd, GarageArea
#num_cols = [i for i in X_modified.columns if i not in ['1stFlrSF', 'FullBath', 'TotRmsAbvGrd', 'GarageArea']]
num_cols = [i for i in X_num_only.columns if i not in ['1stFlrSF', 'FullBath', 'TotRmsAbvGrd', 'GarageArea']]

# Categorical columns - choose the important ones

cat_cols = ["MSZoning", "Utilities","BldgType","Heating","KitchenQual","SaleCondition","LandSlope"]
X_final = X[num_cols]
X_final.shape<jupyter_output><empty_output><jupyter_text>Modify 'YearRemodAdd' feature - make it more informative<jupyter_code>X_final['YearRemodAdd'] = X_final['YearRemodAdd'] - X_final['YearBuilt']
X_final.head()<jupyter_output><empty_output><jupyter_text>Handling missing data<jupyter_code>X_final.isna().sum()
#X_final['MasVnrArea'] = X_final['MasVnrArea'].fillna(X_final['MasVnrArea'].median())
X[cat_cols].isna().sum()<jupyter_output><empty_output><jupyter_text>Encoding Categorical data<jupyter_code>X_categorical_df = pd.get_dummies(X[cat_cols], columns=cat_cols)
X_categorical_df
# Create final dataframe
X_final = X_final.join(X_categorical_df)
X_final<jupyter_output><empty_output><jupyter_text>Normalizing the data<jupyter_code>from sklearn import preprocessing
standardize = preprocessing.StandardScaler().fit(X_final[num_cols])
#See mean per column
standardize.mean_
#transform
X_final[num_cols] = standardize.transform(X_final[num_cols])
X_final
X_final.head()<jupyter_output><empty_output><jupyter_text>Split training data into training and validation<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X_final, y, test_size=0.2, random_state=1)
X_train.shape, X_val.shape, y_train.shape, y_val.shape<jupyter_output><empty_output><jupyter_text>Regression Using Machine Learning<jupyter_code>from sklearn.metrics import r2_score 
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error

from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR
from xgboost import XGBRegressor
from sklearn.preprocessing import PolynomialFeatures
perf = []
method = []
# Linear Regression
lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
predictions = lin_reg.predict(X_val)

r_squared = r2_score(predictions, y_val)

print("R2 Score:", r_squared)
method.append('Linear Regression')
perf.append(r_squared)
# Ridge regression
ridge = Ridge()
ridge.fit(X_train, y_train)
predictions = ridge.predict(X_val)

r_squared = r2_score(predictions, y_val)

print("R2 Score:", r_squared)
method.append('Ridge Regression')
perf.append(r_squared)
# Ridge regression
lasso = Lasso()
lasso.fit(X_train, y_train)
predictions = lasso.predict(X_val)

r_squared = r2_score(predictions, y_val)

print("R2 Score:", r_squared)
method.append('Lasso Regression')
perf.append(r_squared)
# support vector regression
svr = SVR(C=1000000)
svr.fit(X_train, y_train)
predictions = svr.predict(X_val)

r_squared = r2_score(predictions, y_val)

print("R2 Score:", r_squared)
#method.append('SVM')
#perf.append(r_squared)
#Random forest regressor
random_forest = RandomForestRegressor(n_estimators=100)
random_forest.fit(X_train, y_train)
predictions = random_forest.predict(X_val)

r_squared = r2_score(predictions, y_val)

print("R2 Score:", r_squared)
method.append('SVM')
perf.append(r_squared)
# xgboost
from xgboost import XGBRegressor
xgb = XGBRegressor(n_estimators=1000, learning_rate=0.01)
xgb.fit(X_train, y_train)
predictions = xgb.predict(X_val)

r_squared = r2_score(predictions, y_val)

print("R2 Score:", r_squared)
method.append('XGBoost Regressor')
perf.append(r_squared)<jupyter_output>R2 Score: 0.8750165471150491<jupyter_text>Testing<jupyter_code># Test Data Preprocessing

X_test = test_data[num_cols + cat_cols]
X_test['YearRemodAdd'] = X_test['YearRemodAdd'] - X_test['YearBuilt']
X_test.shape
# Encode categorical similar to train
X_test = pd.get_dummies(X_test)
X_test
# Add missed columns missed due to get dummies on X_test
X_test = X_test.reindex(columns = X_final.columns, fill_value=0)
X_test
#transform
X_test[num_cols] = standardize.transform(X_test[num_cols])
X_test<jupyter_output><empty_output><jupyter_text>Handling missing data in test data<jupyter_code>X_test.isna().sum()
# we will use median for missing values
X_test['TotalBsmtSF'] = X_test['TotalBsmtSF'].fillna(train_data['TotalBsmtSF'].median())
# mode for cars
X_test['GarageCars'] = X_test['GarageCars'].fillna(train_data['GarageCars'].mode()[0])
preds = random_forest.predict(X_test)
preds
submit = pd.DataFrame({'Id': test_data['Id'], 'SalePrice': preds})
submit
submit.to_csv('submit.csv',index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1246390028879769, 'fraction_numerical': 0.012919896640826873, 'mean_word_length': 8.293785310734464, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '634778', 'license': 'MIT', 'n_tokens_mistral': 2598, 'n_tokens_neox': 2460, 'n_words': 445, 'path': 'Housing Price Prediction - Regression in Machine Learning.ipynb', 'repo_name': 'omkaar718/machine_learning_Regression', 'size': '6579'}