-
Notifications
You must be signed in to change notification settings - Fork 4
/
sum_insurance.py
159 lines (78 loc) · 2.89 KB
/
sum_insurance.py
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
#!/usr/bin/env python
# coding: utf-8
# In[46]:
import pandas as pd
import numpy as np
# In[80]:
data = pd.read_csv("insurance(R).csv")
data_new = data.copy(deep = True)
# In[81]:
data.head()
# In[82]:
import re
obj_columns = data.select_dtypes("object")
for col in obj_columns:
data[col] = data[col].apply(lambda x: re.sub(r'[^a-zA-Z0-9]', '', x.lower())).astype("str")
# In[83]:
data.head()
# In[84]:
season_catogory = list(data.season.values)
scheme_catogory = list(data.scheme.values)
state_catogory = list(data.state_name.values)
district_catogory = list(data.district_name.values)
# In[85]:
columns = ['season','scheme','state_name','district_name']
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
for col in columns:
data[col] = encoder.fit_transform(data[col])
# In[86]:
season_label = list(data.season.values)
scheme_label = list(data.scheme.values)
state_label = list(data.state_name.values)
district_label = list(data.district_name.values)
# In[87]:
season_category_label_dict = dict(zip(season_catogory, season_label))
# In[88]:
scheme_category_label_dict = dict(zip(scheme_catogory, scheme_label))
# In[89]:
state_category_label_dict = dict(zip(state_catogory, state_label))
# In[90]:
district_category_label_dict = dict(zip(district_catogory, district_label))
# In[ ]:
# In[91]:
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder, StandardScaler, FunctionTransformer
from sklearn.model_selection import train_test_split
# In[92]:
X = data.drop("sum_insured", axis=1)
y = data["sum_insured"]
# In[93]:
# X_train, X_test, y_train, y_test = train_test_split(X,y, random_state=1000, test_size=0.2)
# In[94]:
# from sklearn.ensemble import ExtraTreesRegressor
# from sklearn.metrics import r2_score
# # Create ExtraTreesRegressor with custom parameters
# model = ExtraTreesRegressor(
# n_estimators=200,
# criterion='squared_error',
# max_depth=None,
# min_samples_split=2,
# min_samples_leaf=1,
# max_features=5,
# random_state=1000
# )
# model.fit(X, y)
# In[95]:
# import pickle as pk
# filename= 'crop_insurance_sum_Raghu.pkl'
# pk.dump(model,open(filename,'wb'))
# In[96]:
def encoding(input_data):
input_data[0] = season_category_label_dict[input_data[0].lower().replace(" ","").replace(" ","").replace(" ","").replace(" ","")]
input_data[1] = scheme_category_label_dict[input_data[1].lower().replace(" ","").replace(" ","").replace(" ","").replace(" ","")]
input_data[2] = state_category_label_dict[input_data[2].lower().replace(" ","").replace(" ","").replace(" ","").replace(" ","")]
input_data[3] = district_category_label_dict[input_data[3].lower().replace(" ","").replace(" ","").replace(" ","").replace(" ","")]
return input_data