import matplotlib pyplot as plt from sklearn model_selection import tr

 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
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import DecisionTreeRegressor
from sklearn.neural_network import MLPClassifier
import mglearn
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
dataset_filename = "ionosphere.data"
# dataset = np.loadtxt(dataset_filename, delimiter=',')
dataset = pd.read_csv(dataset_filename).to_numpy()
print(dataset.shape)
print(dataset[0][:-1])
X = []
y = []
for row in dataset:
X.append(row[:-1])
y.append(row[-1])
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=6)
tree = DecisionTreeClassifier(max_depth=3, random_state=6).fit(X_train, y_train)
train_score = tree.score(X_train, y_train)
test_score = tree.score(X_test, y_test)
print("точность на обучающей выборке: {:.3f}".format(train_score))
print("точность на тестовой выборке: {:.3f}".format(test_score))
print("Важности признаков:\n{}".format(tree.feature_importances_))
def plot_feature_importances(model):
n_features = len(X[0])
plt.barh(range(n_features), model.feature_importances_, align='center')
plt.yticks(np.arange(n_features), range(n_features))
plt.xlabel("Важность признака")
plt.ylabel("Признак")
plot_feature_importances(tree)
# predict_proba
# decision_function
print("Форма вероятностей: {}".format(tree.predict_proba(X_test).shape))
print("Спрогнозированные вероятности:\n{}".format(tree.predict_proba(X_test[:6])))
# MLP
print("Not normalized dataset")
for axx, n_hidden_nodes in zip(axes, [10, 100]):
for ax, alpha in zip(axx, [0.0001, 0.01, 0.1, 1]):
mlp = MLPClassifier(solver='lbfgs', random_state=6, hidden_layer_sizes=[n_hidden_nodes, n_hidden_nodes], alpha=alpha)
mlp.fit(X_train, y_train)
train_score = mlp.score(X_train, y_train)
test_score = mlp.score(X_test, y_test)
print("\nn_hidden=[{},{}]'nalpha={:.4f}'".format(n_hidden_nodes, n_hidden_nodes, alpha))
print("Точность на обучающей выборке: {:.3f}".format(train_score))
print("Точность на тестовой выборке: {:.3f}".format(test_score))
# print("Форма вероятностей: {}".format(mlp.predict_proba(X_test).shape))
# print("Спрогнозированные вероятности:\n{}".format(mlp.predict_proba(X_test[:6])))
min_max_scaler = MinMaxScaler()
X_train_minmax = min_max_scaler.fit_transform(X_train)
X_test_minmax = min_max_scaler.fit_transform(X_test)
print("Normalized dataset")
for axx, n_hidden_nodes in zip(axes, [10, 100]):
for ax, alpha in zip(axx, [0.0001, 0.01, 0.1, 1]):
mlp = MLPClassifier(solver='lbfgs', random_state=6, hidden_layer_sizes=[n_hidden_nodes, n_hidden_nodes], alpha=alpha)
mlp.fit(X_train_minmax, y_train)
train_score = mlp.score(X_train_minmax, y_train)
test_score = mlp.score(X_test_minmax, y_test)
print("\nn_hidden=[{},{}]'nalpha={:.4f}'".format(n_hidden_nodes, n_hidden_nodes, alpha))
print("Точность на обучающей выборке: {:.3f}".format(train_score))
print("Точность на тестовой выборке: {:.3f}".format(test_score))
# print("Форма вероятностей: {}".format(mlp.predict_proba(X_test_minmax).shape))
# print("Спрогнозированные вероятности:\n{}".format(mlp.predict_proba(X_test_minmax[:6])))