In [3]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

In [4]:
evFiyatlari = pd.read_csv('http://bit.ly/2lbjNzm')

In [5]:
evFiyatlari


Out[5]:
HousePrice HsPrc ($10,000) CrimeRate MilesPhila PopChg Name County
0 140463 14.0463 29.7 10.0 -1.0 Abington Montgome
1 113033 11.3033 24.1 18.0 4.0 Ambler Montgome
2 124186 12.4186 19.5 25.0 8.0 Aston Delaware
3 110490 11.0490 49.4 25.0 2.7 Bensalem Bucks
4 79124 7.9124 54.1 19.0 3.9 Bristol B. Bucks
5 92634 9.2634 48.6 20.0 0.6 Bristol T. Bucks
6 89246 8.9246 30.8 15.0 -2.6 Brookhaven Delaware
7 195145 19.5145 10.8 20.0 -3.5 Bryn Athyn Montgome
8 297342 29.7342 20.2 14.0 0.6 Bryn Mawr Montgome
9 264298 26.4298 20.4 26.0 6.0 Buckingham Bucks
10 134342 13.4342 17.3 31.0 4.2 Chalfont Bucks
11 147600 14.7600 50.3 9.0 -1.0 Cheltenham Montgome
12 77370 7.7370 34.2 10.0 -1.2 Clifton Delaware
13 170822 17.0822 33.7 32.0 2.4 Collegeville Montgome
14 40642 4.0642 45.7 15.0 0.0 Darby Bor. Delaware
15 71359 7.1359 22.3 8.0 1.6 Darby Town Delaware
16 104923 10.4923 48.1 21.0 6.9 Downingtown Chester
17 190317 19.0317 19.4 26.0 1.9 Doylestown Bucks
18 215512 21.5512 71.9 26.0 5.8 E. Bradford Chester
19 178105 17.8105 45.1 25.0 2.3 E. Goshen Chester
20 131025 13.1025 31.3 19.0 -1.8 E. Norriton Montgome
21 149844 14.9844 24.9 22.0 6.4 E. Pikeland Chester
22 170556 17.0556 27.2 30.0 4.6 E. Whiteland Chester
23 280969 28.0969 17.7 14.0 2.9 Easttown Chester
24 114233 11.4233 29.0 30.0 1.3 Falls Town Bucks
25 74502 7.4502 21.4 15.0 -3.2 Follcroft Delaware
26 475112 47.5112 28.6 12.0 NaN Gladwyne Montgome
27 97167 9.7167 29.3 10.0 0.2 Glenolden Delaware
28 114572 11.4572 17.5 20.0 5.2 Hatboro Montgome
29 436348 43.6348 16.5 10.0 -0.7 Haverford Delaware
... ... ... ... ... ... ... ...
69 100231 10.0231 24.1 15.0 1.9 Ridley Town Delaware
70 95831 9.5831 21.2 32.0 3.2 Royersford Montgome
71 229711 22.9711 9.8 22.0 5.3 Schuylkill Chester
72 74308 7.4308 29.9 7.0 1.8 Sharon Hill Delaware
73 259506 25.9506 7.2 40.0 17.4 Solebury Bucks
74 159573 15.9573 19.4 15.0 -2.1 Springfield Montgome
75 147176 14.7176 41.1 12.0 -1.7 Springfield Delaware
76 205732 20.5732 11.2 12.0 -0.2 Swarthmore Delaware
77 215783 21.5783 21.2 20.0 1.1 Tredyffin Chester
78 116710 11.6710 42.8 20.0 12.9 U. Chichester Delaware
79 359112 35.9112 9.4 36.0 4.0 U. Makefield Bucks
80 189959 18.9959 61.7 22.0 -2.1 U. Merion Montgome
81 133198 13.3198 19.4 22.0 -2.0 U. Moreland Montgome
82 242821 24.2821 6.6 21.0 1.6 U. Providence Delaware
83 142811 14.2811 15.9 20.0 -1.6 U. Southampton Bucks
84 200498 20.0498 18.8 36.0 11.0 U. Uwchlan Chester
85 199065 19.9065 13.2 20.0 7.8 Upper Darby Montgome
86 93648 9.3648 34.5 8.0 -0.7 Upper Darby Delaware
87 163001 16.3001 22.1 50.0 8.0 Uwchlan T. Chester
88 436348 43.6348 22.1 15.0 1.3 Villanova Montgome
89 124478 12.4478 71.9 22.0 4.6 W. Chester Chester
90 168276 16.8276 31.9 26.0 5.9 W. Goshen Chester
91 114157 11.4157 44.6 38.0 14.6 W. Whiteland Chester
92 130088 13.0088 28.6 19.0 -0.2 Warminster Bucks
93 152624 15.2624 24.0 19.0 23.1 Warrington Bucks
94 174232 17.4232 13.8 25.0 4.7 Westtown Chester
95 196515 19.6515 29.9 16.0 1.8 Whitemarsh Montgome
96 232714 23.2714 9.9 21.0 0.2 Willistown Chester
97 245920 24.5920 22.6 10.0 0.3 Wynnewood Montgome
98 130953 13.0953 13.0 24.0 5.2 Yardley Bucks

99 rows × 7 columns


In [6]:
#plt.style.available

In [7]:
plt.style.use('seaborn-talk')
plt.scatter(evFiyatlari['CrimeRate'], evFiyatlari['HousePrice'])
plt.xlabel('Suç Oranı')
plt.ylabel('Ev Fiyatı')
plt.show()



In [8]:
regr = linear_model.LinearRegression()

In [10]:
print(evFiyatlari.shape)


(99, 7)

In [11]:
evFiyatlari['CrimeRate'].shape


Out[11]:
(99,)

In [12]:
regr.fit(evFiyatlari['CrimeRate'].values.reshape(99, 1), evFiyatlari['HousePrice'].values.reshape(99, 1))


/Users/aaron/Environments/machine_learning/lib/python3.5/site-packages/scipy/linalg/basic.py:884: RuntimeWarning: internal gelsd driver lwork query error, required iwork dimension not returned. This is likely the result of LAPACK bug 0038, fixed in LAPACK 3.2.2 (released July 21, 2010). Falling back to 'gelss' driver.
  warnings.warn(mesg, RuntimeWarning)
Out[12]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

In [15]:
regr.predict(400)


Out[15]:
array([[-54133.842966]])

In [16]:
plt.style.use('seaborn-talk')
plt.scatter(evFiyatlari['CrimeRate'], evFiyatlari['HousePrice'])
plt.plot(evFiyatlari['CrimeRate'], regr.predict(evFiyatlari['CrimeRate'].values.reshape(99,1)), color='red', linewidth=1, linestyle="-")
plt.xlabel('Suç Oranı')
plt.ylabel('Ev Fiyatı')
plt.show()



In [17]:
regr.coef_


Out[17]:
array([[-576.90812768]])

In [18]:
evFiyatlari_t = evFiyatlari[evFiyatlari['CrimeRate'] < 100]

In [39]:
plt.style.use('seaborn-talk')
plt.scatter(evFiyatlari_t['CrimeRate'], evFiyatlari_t['HousePrice'])
plt.xlabel('Suç Oranı')
plt.ylabel('Ev Fiyatı')
plt.show()



In [19]:
# new_row_count = evFiyatlari_t.shape[0]
# new_row_count = len(evFiyatlari_t)
new_row_count = evFiyatlari_t['CrimeRate'].size
plt.style.use('seaborn-talk')
plt.scatter(evFiyatlari_t['CrimeRate'], evFiyatlari_t['HousePrice'])
plt.plot(evFiyatlari_t['CrimeRate'], regr.predict(evFiyatlari_t['CrimeRate'].values.reshape(new_row_count,1)), color='red', linewidth=1, linestyle="-")
plt.xlabel('Suç Oranı')
plt.ylabel('Ev Fiyatı')
plt.show()



In [20]:
regr.predict(10)


Out[20]:
array([[ 170860.32683013]])

In [21]:
evFiyatlari_t


Out[21]:
HousePrice HsPrc ($10,000) CrimeRate MilesPhila PopChg Name County
0 140463 14.0463 29.7 10.0 -1.0 Abington Montgome
1 113033 11.3033 24.1 18.0 4.0 Ambler Montgome
2 124186 12.4186 19.5 25.0 8.0 Aston Delaware
3 110490 11.0490 49.4 25.0 2.7 Bensalem Bucks
4 79124 7.9124 54.1 19.0 3.9 Bristol B. Bucks
5 92634 9.2634 48.6 20.0 0.6 Bristol T. Bucks
6 89246 8.9246 30.8 15.0 -2.6 Brookhaven Delaware
7 195145 19.5145 10.8 20.0 -3.5 Bryn Athyn Montgome
8 297342 29.7342 20.2 14.0 0.6 Bryn Mawr Montgome
9 264298 26.4298 20.4 26.0 6.0 Buckingham Bucks
10 134342 13.4342 17.3 31.0 4.2 Chalfont Bucks
11 147600 14.7600 50.3 9.0 -1.0 Cheltenham Montgome
12 77370 7.7370 34.2 10.0 -1.2 Clifton Delaware
13 170822 17.0822 33.7 32.0 2.4 Collegeville Montgome
14 40642 4.0642 45.7 15.0 0.0 Darby Bor. Delaware
15 71359 7.1359 22.3 8.0 1.6 Darby Town Delaware
16 104923 10.4923 48.1 21.0 6.9 Downingtown Chester
17 190317 19.0317 19.4 26.0 1.9 Doylestown Bucks
18 215512 21.5512 71.9 26.0 5.8 E. Bradford Chester
19 178105 17.8105 45.1 25.0 2.3 E. Goshen Chester
20 131025 13.1025 31.3 19.0 -1.8 E. Norriton Montgome
21 149844 14.9844 24.9 22.0 6.4 E. Pikeland Chester
22 170556 17.0556 27.2 30.0 4.6 E. Whiteland Chester
23 280969 28.0969 17.7 14.0 2.9 Easttown Chester
24 114233 11.4233 29.0 30.0 1.3 Falls Town Bucks
25 74502 7.4502 21.4 15.0 -3.2 Follcroft Delaware
26 475112 47.5112 28.6 12.0 NaN Gladwyne Montgome
27 97167 9.7167 29.3 10.0 0.2 Glenolden Delaware
28 114572 11.4572 17.5 20.0 5.2 Hatboro Montgome
29 436348 43.6348 16.5 10.0 -0.7 Haverford Delaware
... ... ... ... ... ... ... ...
69 100231 10.0231 24.1 15.0 1.9 Ridley Town Delaware
70 95831 9.5831 21.2 32.0 3.2 Royersford Montgome
71 229711 22.9711 9.8 22.0 5.3 Schuylkill Chester
72 74308 7.4308 29.9 7.0 1.8 Sharon Hill Delaware
73 259506 25.9506 7.2 40.0 17.4 Solebury Bucks
74 159573 15.9573 19.4 15.0 -2.1 Springfield Montgome
75 147176 14.7176 41.1 12.0 -1.7 Springfield Delaware
76 205732 20.5732 11.2 12.0 -0.2 Swarthmore Delaware
77 215783 21.5783 21.2 20.0 1.1 Tredyffin Chester
78 116710 11.6710 42.8 20.0 12.9 U. Chichester Delaware
79 359112 35.9112 9.4 36.0 4.0 U. Makefield Bucks
80 189959 18.9959 61.7 22.0 -2.1 U. Merion Montgome
81 133198 13.3198 19.4 22.0 -2.0 U. Moreland Montgome
82 242821 24.2821 6.6 21.0 1.6 U. Providence Delaware
83 142811 14.2811 15.9 20.0 -1.6 U. Southampton Bucks
84 200498 20.0498 18.8 36.0 11.0 U. Uwchlan Chester
85 199065 19.9065 13.2 20.0 7.8 Upper Darby Montgome
86 93648 9.3648 34.5 8.0 -0.7 Upper Darby Delaware
87 163001 16.3001 22.1 50.0 8.0 Uwchlan T. Chester
88 436348 43.6348 22.1 15.0 1.3 Villanova Montgome
89 124478 12.4478 71.9 22.0 4.6 W. Chester Chester
90 168276 16.8276 31.9 26.0 5.9 W. Goshen Chester
91 114157 11.4157 44.6 38.0 14.6 W. Whiteland Chester
92 130088 13.0088 28.6 19.0 -0.2 Warminster Bucks
93 152624 15.2624 24.0 19.0 23.1 Warrington Bucks
94 174232 17.4232 13.8 25.0 4.7 Westtown Chester
95 196515 19.6515 29.9 16.0 1.8 Whitemarsh Montgome
96 232714 23.2714 9.9 21.0 0.2 Willistown Chester
97 245920 24.5920 22.6 10.0 0.3 Wynnewood Montgome
98 130953 13.0953 13.0 24.0 5.2 Yardley Bucks

98 rows × 7 columns


In [ ]: