-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdl1mam.txt
185 lines (173 loc) · 7.15 KB
/
dl1mam.txt
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
------------------------------------------------------------------
# Reference
# https://inside-machinelearning.com/en/how-to-do-linear-regression-with-keras/
------------------------------------------------------------------
import pandas as pd
import numpy as np
from sklearn import metrics
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
import pandas as pd
------------------------------------------------------------------
data=pd.read_csv("housing.csv")
data
------------------------------------------------------------------
data.dtypes
------------------------------------------------------------------
# Finding out the correlation between the features
corr = data.corr()
corr.shape
------------------------------------------------------------------
# Plotting the heatmap of correlation between features
plt.figure(figsize=(20,20))
sns.heatmap(corr, cbar=True, square= True, fmt='.1f', annot=True, annot_kws={'size':15}, cmap='gray')
------------------------------------------------------------------
X =data.drop(['MEDV'], axis = 1)# data['area']#
y = data['MEDV']
------------------------------------------------------------------
X
------------------------------------------------------------------
X.columns
------------------------------------------------------------------
y
------------------------------------------------------------------
y.describe()
------------------------------------------------------------------
# Splitting to training and testing data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.3, random_state = 4)
------------------------------------------------------------------
from sklearn.preprocessing import MinMaxScaler
# Instantiate the scaler and fit to training dataset, X_train
scaler = MinMaxScaler()
scaler.fit(X_train)
# Replace unscaled values with scaled values
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
------------------------------------------------------------------
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
------------------------------------------------------------------
model = Sequential()
model.add(Dense(64, input_dim =13, activation = 'relu'))
model.add(Dropout(0.15))
model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(40, activation = 'relu'))
model.add(Dropout(0.15))
model.add(Dense(54, activation = 'relu'))
model.add(Dropout(0.18))
model.add(Dense(1))
------------------------------------------------------------------
model.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['mse', 'mae'])
------------------------------------------------------------------
history = model.fit(X_train, y_train, validation_split=0.2, epochs=200)
------------------------------------------------------------------
import matplotlib.pyplot as plt
#plot the loss and validation loss of the dataset
plt.plot(history.history['mae'], label='mae')
plt.plot(history.history['val_mae'], label='val_mae')
plt.legend()
------------------------------------------------------------------
scores = model.evaluate(X_test, y_test, verbose = 0)
print('Mean Squared Error : ', scores[1])
print('Mean Absolute Error : ', scores[2])
------------------------------------------------------------------
Y_pred = model.predict(X_test)
Y_pred
------------------------------------------------------------------
from sklearn.metrics import r2_score
print('r2 score: ', r2_score(y_test,Y_pred))
------------------------------------------------------------------
Y_pred = model.predict(X_test)
a = plt.axes(aspect='equal')
plt.xlabel('True values')
plt.ylabel('Predicted values')
plt.xlim([0, 50])
plt.ylim([0, 50])
plt.plot([0, 50], [0,50])
plt.scatter(y_test,Y_pred)
plt.plot()
------------------------------------------------------------------
plt.plot(y_test)
------------------------------------------------------------------
plt.plot(Y_pred)
Y_pred
------------------------------------------------------------------
print(Y_pred[:5])
print(y_test[:5])
y_test.head()
------------------------------------------------------------------
# Using ML MODEL lm for Linear Regression
------------------------------------------------------------------
# Import library for Linear Regression
from sklearn.linear_model import LinearRegression
------------------------------------------------------------------
# Create a Linear regressor
lm = LinearRegression()
# Train the model using the training sets
lm.fit(X_train, y_train)
------------------------------------------------------------------
# Value of y intercept
lm.intercept_
------------------------------------------------------------------
#Converting the coefficient values to a dataframe
coeffcients = pd.DataFrame([X.columns,lm.coef_]).T
coeffcients = coeffcients.rename(columns={0: 'Attribute', 1: 'Coefficients'})
coeffcients
------------------------------------------------------------------
# Model prediction on train data
y_pred = lm.predict(X_train)
------------------------------------------------------------------
# Model Evaluation
print('R^2:',metrics.r2_score(y_train, y_pred))
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_train, y_pred))*(len(y_train)-1)/(len(y_train)-X_train.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_train, y_pred))
print('MSE:',metrics.mean_squared_error(y_train, y_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_train, y_pred)))
------------------------------------------------------------------
# Visualizing the differences between actual prices and predicted values
plt.scatter(y_train, y_pred)
plt.xlabel("Prices")
plt.ylabel("Predicted prices")
plt.title("Prices vs Predicted prices")
plt.show()
------------------------------------------------------------------
# Checking residuals
plt.scatter(y_pred,y_train-y_pred)
plt.title("Predicted vs residuals")
plt.xlabel("Predicted")
plt.ylabel("Residuals")
plt.show()
------------------------------------------------------------------
# Checking Normality of errors
sns.distplot(y_train-y_pred)
plt.title("Histogram of Residuals")
plt.xlabel("Residuals")
plt.ylabel("Frequency")
plt.show()
------------------------------------------------------------------
# Predicting Test data with the model
y_test_pred = lm.predict(X_test)
------------------------------------------------------------------
# Model Evaluation
acc_linreg = metrics.r2_score(y_test, y_test_pred)
print('R^2:', acc_linreg)
print('Adjusted R^2:',1 - (1-metrics.r2_score(y_test, y_test_pred))*(len(y_test)-1)/(len(y_test)-X_test.shape[1]-1))
print('MAE:',metrics.mean_absolute_error(y_test, y_test_pred))
print('MSE:',metrics.mean_squared_error(y_test, y_test_pred))
print('RMSE:',np.sqrt(metrics.mean_squared_error(y_test, y_test_pred)))
------------------------------------------------------------------
"""
x1=[1,2,3,4,5,6]
y1=[1,2,4,5,7,6]
plt.xlabel('True values')
plt.ylabel('Predicted values')
plt.xlim([0,10])
plt.ylim([0,10])
plt.plot([0, 10], [0,10])
plt.scatter(x1,y1)
plt.plot()
"""