-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
65 lines (56 loc) · 2.5 KB
/
test.py
File metadata and controls
65 lines (56 loc) · 2.5 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
from sklearn.datasets import make_regression, make_classification
# import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error, accuracy_score
from sklearn.model_selection import train_test_split
from MSBoost import MSBoostRegressor, MSBoostClassifier
from time import perf_counter
def regression_test():
# Generate synthetic regression data
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# Initialize and fit the regressor
regressor = MSBoostRegressor(n_estimators=100, learning_rate=0.1, return_vals=True, bayes=True)
regressor.fit(X_train, y_train)
# Predict and evaluate using mean squared error
y_pred = regressor.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("MSBoost Regression Test - Mean Squared Error:", mse)
print(regressor.ensemble)
# iterations = list(range(1, len(regressor.errors) + 1))
# plt.figure(figsize=(10, 6))
# plt.plot(iterations, regressor.errors, marker='.', linestyle='-', color='blue', label='Error')
# plt.title('MSBoostRegressor Error per Iteration')
# plt.xlabel('Iteration')
# plt.ylabel('Error')
# plt.grid(True)
# plt.legend()
# plt.tight_layout()
# plt.show()
def classification_test():
# Generate synthetic binary classification data
X, y = make_classification(n_samples=1000, n_features=10, n_informative=8, n_redundant=0, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# Initialize and fit the classifier
classifier = MSBoostClassifier(n_estimators=10, learning_rate=0.1, return_vals=True, bayes=True)
classifier.fit(X_train, y_train)
# Predict and evaluate using accuracy
y_pred = classifier.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print("MSBoost Classification Test - Accuracy:", acc)
print(classifier.ensemble)
# iterations = list(range(1, len(classifier.errors) + 1))
# plt.figure(figsize=(10, 6))
# plt.plot(iterations, classifier.errors, marker='.', linestyle='-', color='blue', label='Error')
# plt.title('MSBoostClassifier Error per Iteration')
# plt.xlabel('Iteration')
# plt.ylabel('Error')
# plt.grid(True)
# plt.legend()
# plt.tight_layout()
# plt.show()
if __name__ == '__main__':
s = perf_counter()
regression_test()
classification_test()
e = perf_counter()
print("Time Taken:", (e - s) / 60, "min")