forked from Ansar390/BBB-PEP-Prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHyperparamter Tuning (IND).py
180 lines (141 loc) · 5.8 KB
/
Hyperparamter Tuning (IND).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
# -*- coding: utf-8 -*-
"""bbb_INDparam.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Lisf8zNfIhX5V-0s1ZmlyeUQV1aTMdUP
"""
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, make_scorer
from sklearn.model_selection import cross_val_score, StratifiedKFold, KFold, LeaveOneOut, train_test_split
from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.ensemble import BaggingClassifier, ExtraTreesClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import RidgeClassifier, Perceptron
import pandas as pd
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, AdaBoostClassifier
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
# from catboost import CatBoostClassifier
import numpy as np
pos=pd.read_csv('/content/posbbb.csv')
neg=pd.read_csv('/content/negbbb.csv')
df = pd.concat([pos,neg], axis=0)
df=df.reset_index(drop=True)
from sklearn.preprocessing import StandardScaler,MinMaxScaler
#dataset = pd.read_csv('df.csv', sep=',')
dataset = df
X = dataset.drop(['class'],axis=1)
y = dataset['class']
X = X.to_numpy()
y = y.to_numpy()
std_scale = MinMaxScaler().fit(X)
X = std_scale.transform(X)
X = np.nan_to_num(X.astype('float32'))
from xgboost import XGBClassifier
from sklearn.model_selection import GridSearchCV,RandomizedSearchCV
from xgboost import XGBClassifier
from sklearn.ensemble import ExtraTreesClassifier
from lightgbm import LGBMClassifier
# Define the parameter grid for each classifier
xgb_param_grid = {
'n_estimators': [100, 200, 300,400,500],
'max_depth': [3, 5, 7,9],
'learning_rate': [0.1, 0.01, 0.001]
}
extra_trees_param_grid = {
'n_estimators': [100, 200, 300,500,400],
'max_depth': [None, 5, 10],
'min_samples_split': [2, 5, 10]
}
lgbm_param_grid = {
'n_estimators': [100, 200, 300,400,500],
'max_depth': [3, 5, 7,9],
'learning_rate': [0.1, 0.01, 0.001]
}
# Create the classifiers
xgb_classifier = XGBClassifier()
extra_trees_classifier = ExtraTreesClassifier()
lgbm_classifier = LGBMClassifier()
# Perform GridSearchCV for each classifier
xgb_grid_search = RandomizedSearchCV(xgb_classifier, xgb_param_grid, cv=5)
extra_trees_grid_search = RandomizedSearchCV(extra_trees_classifier, extra_trees_param_grid, cv=5)
lgbm_grid_search = RandomizedSearchCV(lgbm_classifier, lgbm_param_grid, cv=5)
# Fit the data to perform hyperparameter tuning
xgb_grid_search.fit(X, y)
extra_trees_grid_search.fit(X, y)
lgbm_grid_search.fit(X, y)
# Get the best parameters and best score for each classifier
xgb_best_params = xgb_grid_search.best_params_
xgb_best_score = xgb_grid_search.best_score_
extra_trees_best_params = extra_trees_grid_search.best_params_
extra_trees_best_score = extra_trees_grid_search.best_score_
lgbm_best_params = lgbm_grid_search.best_params_
lgbm_best_score = lgbm_grid_search.best_score_
# Print the results
print("XGBClassifier:")
print("Best Parameters:", xgb_best_params)
print("Best Score:", xgb_best_score)
print()
print("ExtraTreesClassifier:")
print("Best Parameters:", extra_trees_best_params)
print("Best Score:", extra_trees_best_score)
print()
print("LGBMClassifier:")
print("Best Parameters:", lgbm_best_params)
print("Best Score:", lgbm_best_score)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.234)
from xgboost import XGBClassifier
from sklearn.ensemble import ExtraTreesClassifier
from lightgbm import LGBMClassifier
# Create the classifiers with the best parameters
xgb_classifier = XGBClassifier(n_estimators=400, max_depth=9, learning_rate=0.1)
extra_trees_classifier = ExtraTreesClassifier(n_estimators=100, max_depth=None, min_samples_split=2)
lgbm_classifier = LGBMClassifier(n_estimators=200, max_depth=5, learning_rate=0.1)
# Fit the models with the best parameters
xgb_classifier.fit(X_train, y_train)
extra_trees_classifier.fit(X_train, y_train)
lgbm_classifier.fit(X_train, y_train)
# Make predictions on the test set
xgb_predictions = xgb_classifier.predict(X_test)
extra_trees_predictions = extra_trees_classifier.predict(X_test)
lgbm_predictions = lgbm_classifier.predict(X_test)
# Evaluate the models
xgb_accuracy = accuracy_score(y_test, xgb_predictions)
extra_trees_accuracy = accuracy_score(y_test, extra_trees_predictions)
lgbm_accuracy = accuracy_score(y_test, lgbm_predictions)
# Print the accuracies
print("XGBClassifier Accuracy:", xgb_accuracy)
print("ExtraTreesClassifier Accuracy:", extra_trees_accuracy)
print("LGBMClassifier Accuracy:", lgbm_accuracy)
import pickle
# Save the XGBoost model
with open('xgb_modelup83.pkl', 'wb') as file:
pickle.dump(xgb_classifier, file)
# Save the Extra Trees model
with open('extra_trees_modelup83.pkl', 'wb') as file:
pickle.dump(extra_trees_classifier, file)
# Save the LGBM model
with open('lgbm_modelup83.pkl', 'wb') as file:
pickle.dump(lgbm_classifier, file)
import pandas as pd
# Convert y_train and y_test arrays to dataframes
y_train_df = pd.DataFrame({'target': y_train})
y_test_df = pd.DataFrame({'target': y_test})
# Merge X_train and y_train dataframes
train_data = pd.concat([X_train, y_train_df], axis=1)
# Merge X_test and y_test dataframes
test_data = pd.concat([X_test, y_test_df], axis=1)
# Save train_data dataframe as CSV file
train_data.to_csv('train_data77up.csv', index=False)
# Save test_data dataframe as CSV file
test_data.to_csv('test_data23up.csv', index=False)
from google.colab import files
# Download train_data.csv
files.download('train_data77up.csv')
# Download test_data.csv
files.download('test_data23up.csv')