-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML.py
More file actions
180 lines (129 loc) · 4.43 KB
/
Copy pathML.py
File metadata and controls
180 lines (129 loc) · 4.43 KB
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
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.metrics import r2_score
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import numpy as np # linear algebra
import pandas as pd # data processing
data = pd.read_csv(r'C:\Users\Sumit\Desktop\insurance.csv')
print("\n THE DATASET IS AS FOLLOWS \n")
data = data.drop('region', axis=1)
print(data)
# creating a label encoder
le = LabelEncoder()
# label encoding for sex
# 0 for females and 1 for males
data['sex'] = le.fit_transform(data['sex'])
# label encoding for smoker
# 0 for smokers and 1 for non smokers
data['smoker'] = le.fit_transform(data['smoker'])
# splitting the dependent and independent variable
x = data.iloc[:, :5]
y = data.iloc[:, 5]
print("\n THE VALUES OF X VARIABLE ARE \n")
print(x)
print("\n THE VALUES OF Y VARIABLE ARE \n")
print(y)
print("\n THE SHAPE OF X & Y VARIABLE ARE \n")
print(x.shape)
print(y.shape)
# splitting the dataset into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.2, random_state=30)
print("\n THE SHAPE OF X & Y TRAIN TEST VARIABLE ARE \n")
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
# standard scaling
# creating a standard scaler
sc = StandardScaler()
# feeding independents sets into the standard scaler
x_train = sc.fit_transform(x_train)
x_test = sc.fit_transform(x_test)
# Set data
df = pd.DataFrame({
'group': [i for i in range(0, 1338)],
'Age': data['age'],
'Charges': data['charges'],
'Children': data['children'],
'BMI': data['bmi']
})
print("##################### MLR ######################")
# importing the model
# Multiple linear regression
model = LinearRegression()
# Fit linear model by passing training dataset
model.fit(x_train, y_train)
# Predicting the target variable for test datset
predictions = model.predict(x_test)
print('THE PREDICTION VALUE IN MULTIPLE LINEAR REGRESSOR IS:\n')
print(predictions)
# plotting the y prediction
plt.scatter(y_test, predictions)
plt.title('Multiple Linear Regression')
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')
plt.show()
# RANDOM FOREST
print("##################### RFR ######################")
# creating the model
model = RandomForestRegressor(n_estimators=40, max_depth=4, n_jobs=-1)
# feeding the training data to the model
model.fit(x_train, y_train)
# predicting the test set results
y_pred = model.predict(x_test)
print('THE PREDICTION VALUE OF IN RANDOM FOREST REGRESSOR IS:\n')
print(y_pred)
# plotting the y prediction
plt.scatter(y_test, y_pred)
plt.title('Random Forest Regression')
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')
plt.show()
# feature extraction
print("##################### PCA WITH MLR ######################")
pca = PCA(n_components=None)
x_train = pca.fit_transform(x_train)
x_test = pca.transform(x_test)
# importing the model
# Multiple linear regression
model = LinearRegression()
# Fit linear model by passing training dataset
model.fit(x_train, y_train)
# Predicting the target variable for test datset
predictions = model.predict(x_test)
print('THE PREDICTION VALUE OF PCA WITH MULTIPLE LINEAR REGRESSOR IS:\n')
print(predictions)
# plotting the y prediction
plt.scatter(y_test, predictions)
plt.title('PCA with Multiple Linear Regression')
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')
plt.show()
# feature extraction
print("##################### PCA WITH RFR ######################")
pca = PCA(n_components=None)
x_train = pca.fit_transform(x_train)
x_test = pca.transform(x_test)
# RANDOM FOREST
# creating the model
model = RandomForestRegressor(n_estimators=40, max_depth=4, n_jobs=-1)
# feeding the training data to the model
model.fit(x_train, y_train)
# predicting the test set results
y_pred = model.predict(x_test)
print('THE PREDICTION VALUE OF IN PCA WITH RANDOM FOREST REGRESSOR IS:\n')
print(y_pred)
# plotting the y prediction
plt.scatter(y_test, y_pred)
plt.title('PCA with Random Forest Regression')
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')
plt.show()
# Calculating the r2 score
r2 = r2_score(y_test, y_pred)
print("\n r2 score :", r2)