In [37]:
import pandas as pa
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
Dataset is from Philadelphia, PA and includes average house sales price in a number of neighborhoods. The attributes of each neighborhood we have include the crime rate ('CrimeRate'), miles from Center City ('MilesPhila'), town name ('Name'), and county name ('County').
In [20]:
regressionDir = '/home/weenkus/workspace/Machine Learning - University of Washington/Regression'
sales = pa.read_csv(regressionDir + '/datasets/Philadelphia_Crime_Rate_noNA.csv')
sales
Out[20]:
In [21]:
# Show plots in jupyter
%matplotlib inline
The house price in a town is correlated with the crime rate of that town. Low crime towns tend to be associated with higher house prices and vice versa.
In [34]:
plt.scatter(sales.CrimeRate, sales.HousePrice, alpha=0.5)
plt.ylabel('House price')
plt.xlabel('Crime rate')
Out[34]:
In [77]:
# Check the type and shape
X = sales[['CrimeRate']]
print (type(X))
print (X.shape)
y = sales['HousePrice']
print (type(y))
print (y.shape)
In [101]:
crime_model = linear_model.LinearRegression()
crime_model.fit(X, y)
Out[101]:
In [88]:
plt.plot(sales.CrimeRate, sales.HousePrice, '.',
X, crime_model.predict(X), '-',
linewidth=3)
plt.ylabel('House price')
plt.xlabel('Crime rate')
Out[88]:
Center City is the one observation with an extremely high crime rate, yet house prices are not very low. This point does not follow the trend of the rest of the data very well. A question is how much including Center City is influencing our fit on the other datapoints. Let's remove this datapoint and see what happens.
In [90]:
sales_noCC = sales[sales['MilesPhila'] != 0.0]
In [92]:
plt.scatter(sales_noCC.CrimeRate, sales_noCC.HousePrice, alpha=0.5)
plt.ylabel('House price')
plt.xlabel('Crime rate')
Out[92]:
In [112]:
crime_model_noCC = linear_model.LinearRegression()
crime_model_noCC.fit(sales_noCC[['CrimeRate']], sales_noCC['HousePrice'])
Out[112]:
In [113]:
plt.plot(sales_noCC.CrimeRate, sales_noCC.HousePrice, '.',
sales_noCC[['CrimeRate']], crime_model_noCC.predict(sales_noCC[['CrimeRate']]), '-',
linewidth=3)
plt.ylabel('House price')
plt.xlabel('Crime rate')
Out[113]:
Visually, the fit seems different, but let's quantify this by examining the estimated coefficients of our original fit and that of the modified dataset with Center City removed.
In [118]:
print ('slope: ', crime_model.coef_)
print ('intercept: ', crime_model.intercept_)
In [119]:
print ('slope: ', crime_model_noCC.coef_)
print ('intercept: ', crime_model_noCC.intercept_)
Above: We see that for the "no Center City" version, per unit increase in crime, the predicted decrease in house prices is 2,287. In contrast, for the original dataset, the drop is only 576 per unit increase in crime. This is significantly different!
Center City is said to be a "high leverage" point because it is at an extreme x value where there are not other observations. As a result, recalling the closed-form solution for simple regression, this point has the potential to dramatically change the least squares line since the center of x mass is heavily influenced by this one point and the least squares line will try to fit close to that outlying (in x) point. If a high leverage point follows the trend of the other data, this might not have much effect. On the other hand, if this point somehow differs, it can be strongly influential in the resulting fit.
An influential observation is one where the removal of the point significantly changes the fit. As discussed above, high leverage points are good candidates for being influential observations, but need not be. Other observations that are not leverage points can also be influential observations (e.g., strongly outlying in y even if x is a typical value).
Based on the discussion above, a question is whether the outlying high-value towns are strongly influencing the fit. Let's remove them and see what happens.
In [121]:
sales_nohighend = sales_noCC[sales_noCC['HousePrice'] < 350000]
crime_model_nohighhend = linear_model.LinearRegression()
crime_model_nohighhend.fit(sales_nohighend[['CrimeRate']], sales_nohighend['HousePrice'])
Out[121]:
In [127]:
plt.plot(sales_nohighend.CrimeRate, sales_nohighend.HousePrice, '.',
sales_nohighend[['CrimeRate']], crime_model_nohighhend.predict(sales_nohighend[['CrimeRate']]), '-',
linewidth=3)
plt.ylabel('House price')
plt.xlabel('Crime rate')
Out[127]:
In [123]:
print ('slope: ', crime_model_noCC.coef_)
print ('intercept: ', crime_model_noCC.intercept_)
In [124]:
print ('slope: ', crime_model_nohighhend.coef_)
print ('intercept: ', crime_model_nohighhend.intercept_)
Above: We see that removing the outlying high-value neighborhoods has some effect on the fit, but not nearly as much as our high-leverage Center City datapoint.
In [ ]: