IIS_2023_1/volkov_rafael_lab_3/app.py

81 lines
3.9 KiB
Python
Raw Normal View History

2023-12-05 12:27:52 +04:00
from flask import Flask, render_template, request
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.inspection import permutation_importance
app = Flask(__name__)
# Загрузите данные из файла
bgg_data = pd.read_csv("bgg_dataset.csv", delimiter=";")
# Преобразуйте столбцы Rating Average и Complexity Average в числа с плавающей точкой
bgg_data["Rating Average"] = bgg_data["Rating Average"].apply(lambda x: float(x.replace(',', '.')))
bgg_data["Complexity Average"] = bgg_data["Complexity Average"].apply(lambda x: float(x.replace(',', '.')))
# Создайте целевую переменную (успешность игры)
bgg_data["Success"] = bgg_data["Rating Average"].apply(lambda x: 1 if x > 7.5 else 0)
# Определите признаки и целевую переменную
features_bgg = ["Year Published", "Users Rated", "BGG Rank", "Owned Users", "Complexity Average"]
X_bgg = bgg_data[features_bgg]
y_bgg = bgg_data["Success"]
# Разделите данные на обучающий и тестовый наборы
X_train_bgg, X_test_bgg, y_train_bgg, y_test_bgg = train_test_split(X_bgg, y_bgg, test_size=0.01, random_state=42)
# Создайте и обучите модель дерева решений
model_bgg = DecisionTreeClassifier()
model_bgg.fit(X_train_bgg, y_train_bgg)
# Оцените модель на тестовом наборе данных
y_pred_bgg = model_bgg.predict(X_test_bgg)
accuracy_bgg = accuracy_score(y_test_bgg, y_pred_bgg)
# Оценка важности признаков с использованием permutation_importance
result = permutation_importance(model_bgg, X_test_bgg, y_test_bgg, n_repeats=30, random_state=42)
# Важности признаков
feature_importances_bgg = result.importances_mean
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
# Получите данные из запроса
year_published = int(request.form["year_published"])
users_rated = int(request.form["users_rated"])
bgg_rank = int(request.form["bgg_rank"])
owned_users = int(request.form["owned_users"])
complexity_average = float(request.form["complexity_average"].replace(',', '.'))
# Создайте DataFrame с введенными данными
input_data_bgg = pd.DataFrame({
"Year Published": [year_published],
"Users Rated": [users_rated],
"BGG Rank": [bgg_rank],
"Owned Users": [owned_users],
"Complexity Average": [complexity_average]
})
# Прогнозируйте успешность игры
prediction_bgg = model_bgg.predict(input_data_bgg)[0]
# Определите результат
result_bgg = "Высокая оценка" if prediction_bgg == 1 else "Низкая оценка"
# Передайте переменные в шаблон
return render_template("index.html", accuracy_bgg=accuracy_bgg, success_count_bgg=sum(y_test_bgg),
failure_count_bgg=len(y_test_bgg) - sum(y_test_bgg),
feature_importances_bgg=feature_importances_bgg, prediction_result_bgg=result_bgg,
X_train_bgg_columns=X_train_bgg.columns)
# Передайте переменные в шаблон
return render_template("index.html", accuracy_bgg=accuracy_bgg, success_count_bgg=sum(y_test_bgg),
failure_count_bgg=len(y_test_bgg) - sum(y_test_bgg),
feature_importances_bgg=feature_importances_bgg,
X_train_bgg_columns=X_train_bgg.columns)
if __name__ == "__main__":
app.run(host="localhost", port=5000)