Добавил:
mtuci_guy16384
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:ЛБ#2 / main
.pyimport numpy as np
import pandas
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import confusion_matrix
from sklearn.tree import DecisionTreeRegressor
from sklearn import tree
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import accuracy_score
def visualize(y_test, y_pred, model_name):
pernb_1 = 100 * (sum(abs(y_test - y_pred) <= 1)) / len(y_test)
accuracy = round(pernb_1, 2)
print(f'{model_name} % within 1 unit (accuracy): {accuracy}%')
C = confusion_matrix(y_test, y_pred)
C_perc, res, rank, s = np.linalg.lstsq(
np.diag(np.sum(C, axis=1)), C, rcond=None)
plt.ylabel('Outputs')
plt.xlabel('Outputs')
plt.title(f'How many responses were correctly predicted within 1 (accuracy): {accuracy}%')
plt.imshow(C_perc)
plt.colorbar()
plt.show()
def native_bayes(X_train, y_train, X_test, y_test):
gnb = GaussianNB()
NBmodel = gnb.fit(X_train, y_train)
y_pred = NBmodel.predict(X_test)
visualize(y_test, y_pred, "Naive Bayes")
def regression_tree(X_train, y_train, X_test, y_test):
rt_model = DecisionTreeRegressor()
rt_model.fit(X_train, y_train)
rt_y_pred = rt_model.predict(X_test)
rt_y_pred = np.round(rt_y_pred).astype(int)
visualize(y_test, rt_y_pred, "Decision Tree Regressor")
tree.plot_tree(rt_model, max_depth=3, feature_names=list(X_train.columns))
plt.title('Decision Tree Regressor')
plt.show()
for name, imp in zip(X_train.columns, rt_model.feature_importances_):
print(f' feature {name}: importance = {round(float(imp), 3)}')
def nn(X_train, y_train, X_test, y_test):
nn = MLPRegressor(hidden_layer_sizes=(10,), random_state=1, max_iter=2000)
nn.fit(X_train, y_train)
nn_y_pred = nn.predict(X_test)
error = y_test - nn_y_pred
mae = round(float(np.mean(np.abs(error))), 2)
acc = round(100 * float(np.mean(np.abs(error) <= 1)), 2)
print(f"MAE: {mae} кВт*ч/м2; Accuracy (within 1): {acc}%")
def main():
df = pandas.read_csv('ЛР#2/data.csv', sep=';')
for col in ['X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8',
'Y1', 'Y2', 'Y1_R', 'Y2_R']:
df[col].astype(float)
Y1 = df['Y1_R']
Y2 = df['Y2_R']
u = len(df['Y1_R'].unique())
print(f'There are {u} unique output categories')
X = df.drop('Y1', axis=1).drop('Y2', axis=1).drop('Y1_R', axis=1).drop('Y2_R', axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, Y1, random_state=0)
print('--- Все 8 признаков ---')
native_bayes(X_train, y_train, X_test, y_test)
regression_tree(X_train, y_train, X_test, y_test)
nn(X_train, y_train, X_test, y_test)
print('--- Только значимые признаки: X1, X4, X7, X8 ---')
X_sig = X[['X1', 'X4', 'X7', 'X8']]
X_train, X_test, y_train, y_test = train_test_split(X_sig, Y1, random_state=0)
regression_tree(X_train, y_train, X_test, y_test)
nn(X_train, y_train, X_test, y_test)
if __name__ == '__main__':
main()
