-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_model.py
212 lines (169 loc) · 6.25 KB
/
final_model.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Importing libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.feature_selection import RFE
from sklearn import metrics
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.feature_selection import SelectKBest, chi2, f_classif
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.ensemble import GradientBoostingClassifier
# Load data
dataset = pd.read_csv("Dataset/train.csv")
ds = dataset
# converting Nan values
ds['condition']=ds['condition'].fillna(3)
# hot encoding
#for condition
integer_encoded = ds['condition'].values
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
x1 = onehot_encoder.fit_transform(integer_encoded) # 4 columns
#for colortype
integer_encoded = ds['color_type'].values
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
x2 = onehot_encoder.fit_transform(integer_encoded) # 56 columns
#imputation for X1
matrix = [0 for y in range(10)]
count = [0 for y in range(10)]
avg = [0 for y in range(10)]
for i in range(len(ds)):
if ds['X1'][i] != 0:
x = int(ds['X2'][i])
matrix[x] = matrix[x] + ds['X1'][i]
count[x] += 1
for i in range(10):
if count[i] != 0:
avg[i] = round(matrix[i]/count[i])
x3 = [0]*len(ds) # 1 column
for i in range(len(ds)):
if ds['X1'][i] == 0:
x = int(ds['X2'][i])
x3[i] = avg[x]
else:
x3[i] = ds['X1'][i]
x3 = np.resize(x3, (len(ds), 1))
x_final = np.concatenate( # 66 columns
(x1, # 4
x2, # 56
ds[['length(m)']], # 1
ds[['height(cm)']], # 1
x3, # 1
ds[['X2']], # 1
ds[['breed_category']], # 1
ds[['pet_category']]), axis=1) # 1
final_ds = pd.DataFrame(
data=x_final[0:,0:],
index=[i for i in range(x_final.shape[0])],
columns=['f'+str(i) for i in range(x_final.shape[1])])
# data distribution
feature_names = final_ds.columns[0:64]
x_fn = final_ds[feature_names]
# have to find x_fn_breed to predict y_pet
x_fn_breed = pd.concat([x_fn, final_ds['f64']], axis=1)
y_breed = final_ds['f64']
y_pet = final_ds['f65']
# final feature selection
selector = SelectKBest(chi2, k = 64) #chi2, f_classif
feature_x_fn = selector.fit_transform(x_fn, y_breed)
mask = selector.get_support() #list of booleans
feature_x_fn_names = [] # The list of your K best features
for bool, feature in zip(mask, x_fn.columns):
if bool:
feature_x_fn_names.append(feature)
#lola = pd.DataFrame(feature_x_fn, feature_x_fn_names)
# use only one
#feature_x_fn_breed = pd.concat([feature_x_fn, final_ds['f64']], axis=1)
#feature_x_fn_breed = SelectKBest(chi2, k=10).fit_transform(x_fn_breed, y_pet)
selector = SelectKBest(chi2, k = 50) #chi2, f_classif
feature_x_fn_breed = selector.fit_transform(x_fn_breed, y_pet)
mask = selector.get_support() #list of booleans
feature_x_fn_breed_names = [] # The list of your K best features
for bool, feature in zip(mask, x_fn_breed.columns):
if bool:
feature_x_fn_breed_names.append(feature)
# use classification algorithm for model training
model_1 = GradientBoostingClassifier(random_state=0)
model_1.fit(feature_x_fn, y_breed)
model_2 = GradientBoostingClassifier(random_state=0)
model_2.fit(feature_x_fn_breed, y_pet)
# apply on test data
# load data
td = pd.read_csv("Dataset/test.csv")
# converting Nan values
td['condition']=td['condition'].fillna(3)
# hot encoding
#for condition
integer_encoded = td['condition'].values
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
x1 = onehot_encoder.fit_transform(integer_encoded) # 4 columns
#for colortype
integer_encoded = td['color_type'].values
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
x2 = onehot_encoder.fit_transform(integer_encoded) # 56 columns
#imputation for X1
matrix = [0 for y in range(10)]
count = [0 for y in range(10)]
avg = [0 for y in range(10)]
for i in range(len(td)):
if td['X1'][i] != 0:
x = int(td['X2'][i])
matrix[x] = matrix[x] + td['X1'][i]
count[x] += 1
for i in range(10):
if count[i] != 0:
avg[i] = round(matrix[i]/count[i])
x3 = [0]*len(td) # 1 column
for i in range(len(td)):
if td['X1'][i] == 0:
x = int(td['X2'][i])
x3[i] = avg[x]
else:
x3[i] = td['X1'][i]
x3 = np.resize(x3, (len(td), 1))
x_final = np.concatenate( # 66 columns
(x1, # 4
x2, # 56
td[['length(m)']], # 1
td[['height(cm)']], # 1
x3, # 'X1' # 1
td[['X2']]), axis=1) # 1
final_td = pd.DataFrame(
data=x_final[0:,0:],
index=[i for i in range(x_final.shape[0])],
columns=['f'+str(i) for i in range(x_final.shape[1])])
# to predict the breed
test_df = pd.DataFrame()
for i in feature_x_fn_names:
test_df[i] = final_td[i]
predict_1 = model_1.predict(test_df)
predict_1 = np.resize(predict_1, (len(td), 1))
# to predict the pet
# modifying content of final_td: adding predict_1 as 'f64'
x_final = np.concatenate( # 66 columns
(x1, # 4
x2, # 56
td[['length(m)']], # 1
td[['height(cm)']], # 1
x3, # 'X1' # 1
td[['X2']], predict_1), axis=1) # 1
final_td_2 = pd.DataFrame(
data=x_final[0:,0:],
index=[i for i in range(x_final.shape[0])],
columns=['f'+str(i) for i in range(x_final.shape[1])])
test_df = pd.DataFrame()
for i in feature_x_fn_breed_names:
test_df[i] = final_td_2[i]
predict_2 = model_2.predict(test_df)
predict_2 = np.resize(predict_2, (len(td), 1))
answer = pd.read_csv("lola.csv")
answer['breed_category'] = predict_1
answer['pet_category'] = predict_2
answer.to_csv(r'lola.csv', index=False)