47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
|
import pandas
|
||
|
import numpy as np
|
||
|
from sklearn.tree import DecisionTreeClassifier
|
||
|
from sklearn.model_selection import train_test_split
|
||
|
from sklearn.metrics import accuracy_score
|
||
|
|
||
|
#Данные
|
||
|
data = pandas.read_csv('clean_data.csv')
|
||
|
|
||
|
#Приведение строчных значений к числовым
|
||
|
#work
|
||
|
factorized_data_work, unique_values_work = pandas.factorize(data['work'])
|
||
|
data['work'] = factorized_data_work
|
||
|
#gymtime
|
||
|
factorized_data_gymtime, unique_values_gymtime = pandas.factorize(data['gymtime'])
|
||
|
data['gymtime'] = factorized_data_gymtime
|
||
|
|
||
|
#Создание столбца exercise_reg
|
||
|
data['exercise_reg'] = np.where(data['phy_ex'] >= 7, 1, 0)
|
||
|
|
||
|
#Отбор нужных столбцов
|
||
|
x = data[['age', 'weight', 'work', 'phy_health', 'gymtime']]
|
||
|
|
||
|
#Определение целевой переменной
|
||
|
y = data['exercise_reg']
|
||
|
|
||
|
#Получение обучающей и тестовой выборки
|
||
|
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
|
||
|
|
||
|
#Создание и обучение модели
|
||
|
model = DecisionTreeClassifier()
|
||
|
model.fit(x_train, y_train)
|
||
|
|
||
|
#Прогнозирование на тестовом наборе
|
||
|
y_pred = model.predict(x_test)
|
||
|
|
||
|
#Оценка производительности модели
|
||
|
accuracy = accuracy_score(y_test, y_pred)
|
||
|
|
||
|
#Важности признаков
|
||
|
importances_clf = model.feature_importances_
|
||
|
|
||
|
#Вывод результатов
|
||
|
print(x.head(15))
|
||
|
print(importances_clf)
|
||
|
print("Качество модели DecisionTreeClassifier: ", model.score(x_test, y_test))
|
||
|
print("Качество классификации accuracy:", accuracy)
|