Passed
Push — master ( a4a5a4...600197 )
by Simon
02:08
created

tests.test_packages   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 163
Duplicated Lines 6.13 %

Importance

Changes 0
Metric Value
wmc 5
eloc 94
dl 10
loc 163
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A test_keras() 0 45 1
A test_xgboost() 0 15 1
A test_sklearn() 10 25 1
A test_catboost() 0 23 1
A test_lightgbm() 0 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
from sklearn.datasets import load_breast_cancer
6
7
from sklearn.model_selection import cross_val_score
8
from hyperactive import Hyperactive
9
10
data = load_breast_cancer()
11
X, y = data.data, data.target
12
13
14
def test_sklearn():
15
    from sklearn.tree import DecisionTreeClassifier
16
17 View Code Duplication
    def model(para, X_train, y_train):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
18
        model = DecisionTreeClassifier(
19
            criterion=para["criterion"],
20
            max_depth=para["max_depth"],
21
            min_samples_split=para["min_samples_split"],
22
            min_samples_leaf=para["min_samples_leaf"],
23
        )
24
        scores = cross_val_score(model, X_train, y_train, cv=3)
25
26
        return scores.mean(), model
27
28
    search_config = {
29
        model: {
30
            "criterion": ["gini", "entropy"],
31
            "max_depth": range(1, 21),
32
            "min_samples_split": range(2, 21),
33
            "min_samples_leaf": range(1, 21),
34
        }
35
    }
36
37
    opt = Hyperactive(search_config)
38
    opt.fit(X, y)
39
    # opt.predict(X)
40
    # opt.score(X, y)
41
42
43
def test_xgboost():
44
    from xgboost import XGBClassifier
45
46
    def model(para, X_train, y_train):
47
        model = XGBClassifier(
48
            n_estimators=para["n_estimators"], max_depth=para["max_depth"]
49
        )
50
        scores = cross_val_score(model, X_train, y_train, cv=3)
51
52
        return scores.mean(), model
53
54
    search_config = {model: {"n_estimators": range(2, 20), "max_depth": range(1, 11)}}
55
56
    opt = Hyperactive(search_config)
57
    opt.fit(X, y)
58
    # opt.predict(X)
59
    # opt.score(X, y)
60
61
62
def test_lightgbm():
63
    from lightgbm import LGBMClassifier
64
65
    def model(para, X_train, y_train):
66
        model = LGBMClassifier(
67
            num_leaves=para["num_leaves"], learning_rate=para["learning_rate"]
68
        )
69
        scores = cross_val_score(model, X_train, y_train, cv=3)
70
71
        return scores.mean(), model
72
73
    search_config = {
74
        model: {
75
            "num_leaves": range(2, 20),
76
            "learning_rate": [0.001, 0.005, 00.01, 0.05, 0.1, 0.5, 1],
77
        }
78
    }
79
80
    opt = Hyperactive(search_config)
81
    opt.fit(X, y)
82
    # opt.predict(X)
83
    # opt.score(X, y)
84
85
86
def test_catboost():
87
    from catboost import CatBoostClassifier
88
89
    def model(para, X_train, y_train):
90
        model = CatBoostClassifier(
91
            iterations=para["iterations"],
92
            depth=para["depth"],
93
            learning_rate=para["learning_rate"],
94
        )
95
        scores = cross_val_score(model, X_train, y_train, cv=3)
96
97
        return scores.mean(), model
98
99
    search_config = {
100
        model: {
101
            "iterations": [1],
102
            "depth": range(2, 10),
103
            "learning_rate": [0.001, 0.005, 00.01, 0.05, 0.1, 0.5, 1],
104
        }
105
    }
106
107
    opt = Hyperactive(search_config)
108
    opt.fit(X, y)
109
    # opt.predict(X)
110
    # opt.score(X, y)
111
112
113
def test_keras():
114
    from keras.models import Sequential
115
    from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten
116
    from keras.datasets import cifar10
117
    from keras.utils import to_categorical
118
119
    (X_train, y_train), (X_test, y_test) = cifar10.load_data()
120
121
    X_train = X_train[0:1000]
122
    y_train = y_train[0:1000]
123
124
    X_test = X_train[0:1000]
125
    y_test = y_train[0:1000]
126
127
    y_train = to_categorical(y_train, 10)
128
    y_test = to_categorical(y_test, 10)
129
130
    def cnn(para, X_train, y_train):
131
        model = Sequential()
132
133
        model.add(
134
            Conv2D(
135
                filters=para["filters.0"],
136
                kernel_size=para["kernel_size.0"],
137
                activation="relu",
138
            )
139
        )
140
        model.add(MaxPooling2D(pool_size=(2, 2)))
141
142
        model.add(Flatten())
143
        model.add(Dense(10, activation="softmax"))
144
145
        model.compile(
146
            optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]
147
        )
148
        model.fit(X_train, y_train, epochs=1)
149
150
        score = model.evaluate(x=X_test, y=y_test)
151
152
        return score
153
154
    search_config = {cnn: {"filters.0": [32, 64], "kernel_size.0": [3, 4]}}
155
156
    opt = Hyperactive(search_config)
157
    opt.fit(X_train, y_train)
158
    # opt.predict(X)
159
    # opt.score(X, y)
160
161
162
"""
163
def test_pytorch():
164
    import torch
165
    import torchvision
166
    import torchvision.transforms as transforms
167
    import torch.nn as nn
168
    import torch.nn.functional as F
169
    import torch.optim as optim
170
171
    transform = transforms.Compose(
172
        [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
173
    )
174
175
    trainset = torchvision.datasets.CIFAR10(
176
        root="./data", train=True, download=True, transform=transform
177
    )
178
179
    def cnn(para, X_train, y_train):
180
        class Net(nn.Module):
181
            def __init__(self):
182
                super(Net, self).__init__()
183
                self.conv1 = nn.Conv2d(3, 6, 5)
184
                self.pool = nn.MaxPool2d(2, 2)
185
                self.conv2 = nn.Conv2d(6, 16, 5)
186
                self.fc1 = nn.Linear(16 * 5 * 5, 120)
187
                self.fc2 = nn.Linear(120, 84)
188
                self.fc3 = nn.Linear(84, 10)
189
190
            def forward(self, x):
191
                x = self.pool(F.relu(self.conv1(x)))
192
                x = self.pool(F.relu(self.conv2(x)))
193
                x = x.view(-1, 16 * 5 * 5)
194
                x = F.relu(self.fc1(x))
195
                x = F.relu(self.fc2(x))
196
                x = self.fc3(x)
197
                return x
198
199
        trainloader = torch.utils.data.DataLoader(
200
            trainset, batch_size=4, shuffle=True, num_workers=2
201
        )
202
203
        net = Net()
204
205
        criterion = nn.CrossEntropyLoss()
206
        optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
207
208
        for epoch in range(1):  # loop over the dataset multiple times
209
210
            running_loss = 0.0
211
            for i, data in enumerate(trainloader, 0):
212
                # get the inputs; data is a list of [inputs, labels]
213
                inputs, labels = data
214
215
                # zero the parameter gradients
216
                optimizer.zero_grad()
217
218
                # forward + backward + optimize
219
                outputs = net(inputs)
220
                loss = criterion(outputs, labels)
221
                loss.backward()
222
                optimizer.step()
223
224
                # print statistics
225
                running_loss += loss.item()
226
                if i % 2000 == 1999:  # print every 2000 mini-batches
227
                    print(
228
                        "[%d, %5d] loss: %.3f" % (epoch + 1, i + 1, running_loss / 2000)
229
                    )
230
                    running_loss = 0.0
231
232
        return running_loss
233
234
    search_config = {cnn: {"filters.0": [32, 64], "kernel_size.0": [3, 4]}}
235
236
    opt = Hyperactive(search_config)
237
    opt.fit(None, None)
238
    # opt.predict(X)
239
    # opt.score(X, y)
240
241
242
243
def test_chainer():
244
    def cnn(para, X_train, y_train):
245
        pass
246
247
    search_config = {cnn: {"filters.0": [32, 64], "kernel_size.0": [3, 4]}}
248
    opt = Hyperactive(search_config)
249
    opt.fit(None, None)
250
    # opt.predict(X)
251
    # opt.score(X, y)
252
"""
253