<jupyter_start><jupyter_text>Predicting House Prices In Bengaluru  --- `Source=`[MachineHack](https://www.machinehack.com/course/predicting-house-prices-in-bengaluru/)--- Features- [Area_type](https://codeperfectplus.github.io/codes_perfect/) – `describes the area`- [Availability](https://codeperfectplus.github.io/codes_perfect/) – `when it can be possessed or when it is ready(categorical and time-series)`- [Location](https://codeperfectplus.github.io/codes_perfect/) – `where it is located in Bengaluru`- [Price](https://codeperfectplus.github.io/codes_perfect/) – `Value of the property in lakhs(INR)`- [Size](https://codeperfectplus.github.io/codes_perfect/) – `in BHK or Bedroom (1-10 or more)`- [Society](https://codeperfectplus.github.io/codes_perfect/) – `to which society it belongs`- [Total_sqft](https://codeperfectplus.github.io/codes_perfect/) – `size of the property in sq.ft`- [Bath](https://codeperfectplus.github.io/codes_perfect/) – `No. of bathrooms`- [Balcony](https://codeperfectplus.github.io/codes_perfect/) – `No. of the balcony`<jupyter_code>import re
import numpy as np  # For Linear Algebra and math
import pandas as pd # For Dataframe 
# For Displaying Good Quality Graphs
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set_style("darkgrid")
from sklearn.preprocessing import LabelEncoder           # Change String Data to int value by LabelEncoding 
from sklearn.model_selection import train_test_split     # Split Data Into train_test for Check accuracy
from sklearn.metrics import mean_absolute_error          # Mean Absolute Error
from sklearn.ensemble import RandomForestRegressor       # Random_Forest_Regressor
from sklearn.metrics import r2_score                     # R2_Score
from joblib import dump                                  # To Save Model<jupyter_output><empty_output><jupyter_text>-------<jupyter_code># Load train Data into train_df variable
train_df = pd.read_csv('train.csv')
print("Train Dataset :\n")
train_df.head()
# Analyze DataType In Each-Columns
print("Data-types of each columns in train dataset:\n")
print(train_df.dtypes)
#There Is Need to Change Data Types
print("Training Dataset Size  :", train_df.shape)<jupyter_output>Training Dataset Size  : (13320, 9)<jupyter_text>check missing values (NaNs) in train dataset 👇<jupyter_code>print("missing values in train dataset column wise:\n")
print(train_df.isnull().sum())
print("percentage of Missing Values Society Column In Train dataset :",int(5502/train_df.shape[0]*100),"%")<jupyter_output>percentage of Missing Values Society Column In Train dataset : 41 %<jupyter_text>Reason  For Missing Values  - Maybe all these house doesn't belong to any society.  - Or, because of some data gathering problem these records has missing society value. --------  `Outlier Value Visualizations`----<jupyter_code>fig, ax = plt.subplots(figsize=(12, 5))
sns.boxplot(train_df['price'], ax=ax)
ax.set(xlabel= 'price in lakhs(INR)', title='Box plot of price')
#plt.savefig("Graph/Box plot of price")
plt.show()
# find mode (most frequent occuring price value) of price
print("Mode of Price :", train_df['price'].mode()[0] )
print('Descriptive statistics of price column:\n')
print(train_df['price'].describe())<jupyter_output>Descriptive statistics of price column:

count    13320.000000
mean       112.565627
std        148.971674
min          8.000000
25%         50.000000
50%         72.000000
75%        120.000000
max       3600.000000
Name: price, dtype: float64<jupyter_text>---- `Missing Value Visualization` Column-Wise----<jupyter_code>fig, ax = plt.subplots(figsize=(10, 5))
train_couts = train_df['balcony'].fillna('missing').value_counts(normalize=True)
sns.barplot(y = train_couts.index, x= train_couts.values*100)
ax.set(title='Missing Value Visualization For Training Dataset \n Column : Balcony')
#plt.savefig("Graph/Balcony Data Visualization")
fig, ax = plt.subplots(figsize=(10, 5))
train_couts = train_df['bath'].fillna('missing').value_counts(normalize=True)
sns.barplot(y = train_couts.index, x= train_couts.values*100)
ax.set(title='Missing Value Visualization For Training Dataset \n Column : Bathroom')
#plt.savefig("Graph/Bathroom Data Visualization")
fig, ax = plt.subplots(figsize=(10, 5))
train_couts = train_df['area_type'].fillna('missing').value_counts(normalize=True)
sns.barplot(y = train_couts.index, x= train_couts.values*100)
ax.set(title='Missing Value Visualization \n Column : Area Type')
#plt.savefig("Graph/Area Type Data Visualization")<jupyter_output><empty_output><jupyter_text>No. of House For Ready TO Move<jupyter_code>train_df['availability'].value_counts()[0:1]
train_df.area_type.value_counts()
train_df['total_sqft'].unique()<jupyter_output><empty_output><jupyter_text>--------<jupyter_code>replace_area_type = {'Super built-up  Area': 0, 'Built-up  Area': 1, 'Plot  Area': 2, 'Carpet  Area': 3}
train_df['area_type'] = train_df.area_type.map(replace_area_type)
def replace_availabilty(my_string):
    if my_string == 'Ready To Move':
        return 0
    elif my_string == 'Immediate Possession':
        return 1
    else:
        return 2
train_df['availability'] = train_df.availability.apply(replace_availabilty)
def preprocess_total_sqft(my_list):
    if len(my_list) == 1:
        
        try:
            return float(my_list[0])
        except:
            strings = ['Sq. Meter', 'Sq. Yards', 'Perch', 'Acres', 'Cents', 'Guntha', 'Grounds']
            split_list = re.split('(\d*.*\d)', my_list[0])[1:]
            area = float(split_list[0])
            type_of_area = split_list[1]
            
            if type_of_area == 'Sq. Meter':
                area_in_sqft = area * 10.7639
            elif type_of_area == 'Sq. Yards':
                area_in_sqft = area * 9.0
            elif type_of_area == 'Perch':
                area_in_sqft = area * 272.25
            elif type_of_area == 'Acres':
                area_in_sqft = area * 43560.0
            elif type_of_area == 'Cents':
                area_in_sqft = area * 435.61545
            elif type_of_area == 'Guntha':
                area_in_sqft = area * 1089.0
            elif type_of_area == 'Grounds':
                area_in_sqft = area * 2400.0
            return float(area_in_sqft)
        
    else:
        return (float(my_list[0]) + float(my_list[1]))/2.0
train_df['total_sqft'] = train_df.total_sqft.str.split('-').apply(preprocess_total_sqft)
size_mode = train_df['size'].mode()[0]
train_df.loc[train_df['size'].isna(), 'size'] = size_mode 
train_df['size'].unique()
train_df['size'] = train_df['size'].apply(lambda x: x.split(' ')[0])
train_df['size'] = train_df['size'].astype('float64')
train_df.head()
train_df.dtypes
train_df.isnull().sum()
train_df.balcony.value_counts()
bath_med = train_df['bath'].median()
balcony_med = train_df['balcony'].median()
train_df.loc[train_df['bath'].isna(), 'bath'] = bath_med
train_df.loc[train_df['balcony'].isna(), 'balcony'] = balcony_med
train_df.isnull().sum()
train_df['location'].value_counts()
mode_loc =train_df['location'].mode()[0]
train_df.loc[train_df['location'].isna(), 'location'] = mode_loc
train_df.isnull().sum()<jupyter_output><empty_output><jupyter_text>---- `Society :` Society Has Lot Of Null Values So It Will Not Consider.---<jupyter_code>train_df.dtypes
train_df['society'].value_counts()
corelation =train_df.corr()
plt.figure(figsize=(10,6))
sns.heatmap(corelation,cmap="YlGnBu")
plt.title("Corelation Graph")
#plt.savefig("Graph/Corelation Graph")
plt.show()
corelation<jupyter_output><empty_output><jupyter_text>--------  Apply RandomForest----<jupyter_code>X = train_df[['area_type', 'availability' ,'size', 'total_sqft', 'bath', 'balcony']]
y = train_df[['price']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
rf = RandomForestRegressor(n_estimators=600,
                             max_depth= 5,
                             max_leaf_nodes= 15,
                             min_samples_leaf= 3,
                             min_samples_split= 10,
                             random_state=0)
rf.fit(X_train, y_train)
predict_y = rf.predict(X_test)<jupyter_output><empty_output><jupyter_text>` Mean-Absolute-Error`----<jupyter_code>mean_absolute_error(y_test,predict_y)<jupyter_output><empty_output><jupyter_text>`Accuracy Score`---<jupyter_code>print("Accuracy Score :",int(r2_score(y_test, predict_y)*100),"%")<jupyter_output>Accuracy Score : 60 %
{'fraction_non_alphanumeric': 0.1400663035756571, 'fraction_numerical': 0.024271844660194174, 'mean_word_length': 4.11319612590799, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 10, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '364536', 'license': 'MIT', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2842, 'n_words': 607, 'path': 'BengaluruHousePriceRegression.ipynb', 'repo_name': 'codePerfectPlus/Predicting-House-Prices-In-Bengaluru', 'size': '8446'}