-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLinear Regression Boston Dataset
127 lines (53 loc) · 1.38 KB
/
Linear Regression Boston Dataset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
# In[2]:
from sklearn.datasets import load_boston
boston_ds = load_boston()
# In[3]:
print boston_ds['DESCR']
# In[4]:
print boston_ds['feature_names']
# In[5]:
df_boston = pd.DataFrame(boston_ds.data)
# In[6]:
df_boston.columns = boston_ds.feature_names
# In[7]:
df_boston.head()
# In[8]:
print boston_ds.data.shape
# In[9]:
print boston_ds.target.shape
# In[10]:
print boston_ds['target']
# In[11]:
X_features = boston_ds.data
Y_target = boston_ds.target
# In[12]:
from sklearn.linear_model import LinearRegression
linearRegr = LinearRegression()
# In[13]:
linearRegr.fit(X_features,Y_target)
# In[14]:
linearRegr.intercept_
# In[15]:
print 'the estimated intercept %.2f '% linearRegr.intercept_
# In[16]:
linearRegr.coef_
# In[17]:
print 'the coefficient is %d'% len(linearRegr.coef_)
# In[18]:
from sklearn import cross_validation
X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(X_features, Y_target)
# In[19]:
print boston_ds.data.shape
# In[20]:
print X_train.shape, X_test.shape, Y_train.shape, Y_test.shape
# In[21]:
linearRegr.fit(X_train,Y_train)
# In[22]:
print 'Mean Squared Estimator value is %.2f '% np.mean((linearRegr.predict(X_test) - Y_test) ** 2)
# In[23]:
print 'Variance Score is %.2f '% linearRegr.score(X_test,Y_test)
# In[ ]: