text_plain_output_1.png image_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

import statsmodels.api as sm

import warnings

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import warnings
warnings.filterwarnings('ignore')

raw_data = pd.read_csv('../input/housedata/data.csv')

raw_data.describe(include='all').T

x1 = raw_data.drop(['price', 'date', 'street', 'city', 'statezip', 'country'], axis=1)
y = raw_data['price']
x = sm.add_constant(x1)
results = sm.OLS(y, x).fit()
results.summary()

raw_data.isnull().sum()

zero_price = raw_data[raw_data['price'] == 0]
zero_price.describe().T

#In order to decide what to do with the null values in the price, 
#it is helpful to know the corelations between features.

corr_matrix = raw_data.corr()

fig, ax = plt.subplots(figsize = (15,6))
sns.heatmap(corr_matrix, annot = True)

low_price_data = raw_data[(raw_data['sqft_living'] < zero_price['sqft_living'].median()) & (raw_data['bathrooms'] < zero_price['bathrooms'].median()) & (raw_data['sqft_above'] < zero_price['sqft_above'].median())]
low_price = low_price_data.price.median()
high_price_data = raw_data[(raw_data['sqft_living'] > zero_price['sqft_living'].median()) & (raw_data['bathrooms'] > zero_price['bathrooms'].median()) & (raw_data['sqft_above'] > zero_price['sqft_above'].median())]
high_price = high_price_data.price.median()
data_prc = raw_data.copy()
data_prc['price'] = np.where((data_prc['price'] == 0) & (data_prc['sqft_living'] > zero_price['sqft_living'].median()), high_price, data_prc.price)
data_prc['price'] = np.where((data_prc['price'] == 0) & (data_prc['sqft_living'] <= zero_price['sqft_living'].median()), low_price, data_prc.price)
data_prc.price[data_prc.price == 0].count()

fig, ax = plt.subplots(1, 2, figsize=(20, 6))
sns.distplot(ax=ax[0], x=data_prc.bedrooms, color='darkmagenta')
ax[0].set_title('Bedrooms', size=18)
sns.distplot(ax=ax[1], x=data_prc.bathrooms, color='darkmagenta')
ax[1].set_title('Bathrooms', size=18)