<jupyter_start><jupyter_text>Project - California Housing Price Prediction<jupyter_code>#Load the data
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
data=pd.read_csv("C:\DataScienceClass\MachineLearning\housing.csv")
data.head()
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 20640 entries, 0 to 20639
Data columns (total 10 columns):
longitude             20640 non-null float64
latitude              20640 non-null float64
housing_median_age    20640 non-null int64
total_rooms           20640 non-null int64
total_bedrooms        20433 non-null float64
population            20640 non-null int64
households            20640 non-null int64
median_income         20640 non-null float64
ocean_proximity       20640 non-null object
median_house_value    20640 non-null int64
dtypes: float64(4), int64(5), object(1)
memory usage: 1.6+ MB<jupyter_text>Column Description 1. longitude                  Longitude value for the block in California, USA2. latitude:                  Latitude value for the block in California, USA3. housing_median_age:        Median age of the house in the block4. total_rooms                Count of the total number of rooms (excluding bedrooms) in all houses in the block5. total_bedrooms:            Count of the total number of bedrooms in all houses in the block6. population:                Count of the total number of population in the block7. households:                Count of the total number of households in the block8. median_income:             Median of the total household income of all the houses in the block9. ocean_proximity:           Type of the landscape of the block [ Unique Values : 'NEAR BAY', '<1H OCEAN', 'INLAND', 'NEAR OCEAN', 'ISLAND'  ]1o.median_house_value:        Median of the household prices of all the houses in the block<jupyter_code>data.describe()
data.shape
#Handle missing values 
data.isnull().sum()
data.fillna(data.mean(),inplace=True)

data.isnull().sum()
# Encode categorical data
data["ocean_proximity"].value_counts()
data["ocean_proximity"].value_counts().plot(kind="bar")
#box plot of house value based on ocean proximity categories
plt.figure(figsize=(16,6))
sns.boxplot(data.ocean_proximity,data.median_house_value)
fig, axes = plt.subplots(ncols=8, figsize=(16, 5))

sns.boxplot(data=data[['longitude']],  palette='Set2', ax=axes[0]);
sns.boxplot(data=data[['latitude']]  ,  palette='Set2', ax=axes[1]);
sns.boxplot(data=data[['housing_median_age']], palette='Set2', ax=axes[2]);
sns.boxplot(data=data[['total_rooms']],  palette='Set2', ax=axes[3]);

sns.boxplot(data=data[['total_bedrooms']],   palette='Set2', ax=axes[4]);
sns.boxplot(data=data[['population']]  ,  palette='Set2', ax=axes[5]);
sns.boxplot(data=data[['households']],   palette='Set2', ax=axes[6]);
sns.boxplot(data=data[['median_income']],   palette='Set2', ax=axes[7]);
corr=data.corr()
corr
#here medium_income and median_house_value are highly correlated
corr.median_house_value.sort_values(ascending=False)
plt.figure(figsize=(10,8))
sns.heatmap(corr,annot=True)
data = pd.concat([data,pd.get_dummies(data.ocean_proximity)],axis=1)
data.drop(['ocean_proximity'],axis=1,inplace=True)
data.head()
#standardized the data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(data)
X=data.drop(['median_house_value'],axis=1).values
y=data.iloc[:,8].values.reshape(-1,1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1, test_size=0.3)
#linear regressor
from sklearn.linear_model import LinearRegression
model1 = LinearRegression()
model1.fit(X_train,y_train)
model1.intercept_
model1.coef_
feature_cols = data.drop(['median_house_value'], axis=1).columns
feature_cols
y_pred = model1.predict(X_test)
from sklearn import metrics
print('Mean Abs Error   MAE    : ', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Sq  Error MSE      : ', metrics.mean_squared_error(y_test, y_pred))

print('Root Mean Sq Error RMSE : ', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))

print('r2 value                : ', metrics.r2_score(y_test, y_pred))
#Decision tree regressor
from sklearn.tree import DecisionTreeRegressor
model2 =  DecisionTreeRegressor(random_state=0)
model2.fit(X_train,y_train)
y_pred2 = model2.predict(X_test)
print('Mean Abs Error   MAE    : ', metrics.mean_absolute_error(y_test, y_pred2))
print('Mean Sq  Error MSE      : ', metrics.mean_squared_error(y_test, y_pred2))

print('Root Mean Sq Error RMSE : ', np.sqrt(metrics.mean_squared_error(y_test, y_pred2)))

print('r2 value                : ', metrics.r2_score(y_test, y_pred2))
#Random Forest Regression
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(random_state=42)
clf.fit(X_train, y_train)
clf.feature_importances_
list(zip(feature_cols, clf.feature_importances_))
y_pred3 = clf.predict(X_test)
print('Mean Abs Error   MAE    : ', metrics.mean_absolute_error(y_test, y_pred3))
print('Mean Sq  Error MSE      : ', metrics.mean_squared_error(y_test, y_pred3))

print('Root Mean Sq Error RMSE : ', np.sqrt(metrics.mean_squared_error(y_test, y_pred3)))

print('r2 value                : ', metrics.r2_score(y_test, y_pred3))
#Bonus ELinear Regression with one independent variable
X1=data.iloc[:,7].values.reshape(-1,1)
X_train, X_test, y_train, y_test = train_test_split(X1, y, random_state=1, test_size=0.3)
model4 = LinearRegression()
model4.fit(X_train,y_train)
y_pred4 =model4.predict(X_test)
print('Mean Abs Error   MAE    : ', metrics.mean_absolute_error(y_test, y_pred4))
print('Mean Sq  Error MSE      : ', metrics.mean_squared_error(y_test, y_pred4))

print('Root Mean Sq Error RMSE : ', np.sqrt(metrics.mean_squared_error(y_test, y_pred4)))

print('r2 value                : ', metrics.r2_score(y_test, y_pred4))
test = pd.DataFrame({'Predicted':list(y_pred4),'Actual':list(y_test)})
fig= plt.figure(figsize=(16,8))
test = test.reset_index()
test = test.drop(['index'],axis=1)
plt.plot(test[:50])
plt.legend(['Actual','Predicted'])
#1st model  is the best model with r2 =0.63<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12192755498059508, 'fraction_numerical': 0.031371280724450196, 'mean_word_length': 5.129831516352825, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '165904', 'license': 'MIT', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2132, 'n_words': 488, 'path': 'Project - California Housing Price Prediction.ipynb', 'repo_name': 'MukundAabha/California-Housing-Price-Prediction', 'size': '6184'}