78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
from flask import Flask, render_template, request
|
|
import pandas as pd
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.preprocessing import StandardScaler
|
|
from sklearn.neural_network import MLPRegressor
|
|
from sklearn.pipeline import Pipeline
|
|
import joblib
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Загрузка данных
|
|
data_bgg = pd.read_csv("bgg_dataset.csv", delimiter=";")
|
|
|
|
# Выбор нужных столбцов
|
|
selected_columns_bgg = ['Year Published', 'Users Rated', 'Rating Average', 'BGG Rank', 'Owned Users', 'Complexity Average']
|
|
features = data_bgg[selected_columns_bgg]
|
|
|
|
# Замена запятых на точки в столбцах 'Rating Average' и 'Complexity Average'
|
|
features['Rating Average'] = features['Rating Average'].str.replace(',', '.').astype(float)
|
|
features['Complexity Average'] = features['Complexity Average'].str.replace(',', '.').astype(float)
|
|
|
|
# Замена пропущенных значений средними значениями по столбцам
|
|
features = features.fillna(features.mean())
|
|
|
|
# Разделение данных
|
|
X_bgg = features.drop('Rating Average', axis=1)
|
|
y_bgg = features['Rating Average']
|
|
X_train_bgg, X_test_bgg, y_train_bgg, y_test_bgg = train_test_split(X_bgg, y_bgg, test_size=0.2, random_state=42)
|
|
|
|
# Масштабирование данных
|
|
scaler = StandardScaler()
|
|
X_train_bgg_scaled = scaler.fit_transform(X_train_bgg)
|
|
X_test_bgg_scaled = scaler.transform(X_test_bgg)
|
|
|
|
# Обучение модели нейронной сети
|
|
mlp_regressor_model = Pipeline([
|
|
('scaler', StandardScaler()),
|
|
('mlp_regressor', MLPRegressor(hidden_layer_sizes=(100, 50), max_iter=2000, random_state=42))
|
|
])
|
|
mlp_regressor_model.fit(X_train_bgg_scaled, y_train_bgg)
|
|
|
|
# Сохранение модели
|
|
joblib.dump(mlp_regressor_model, 'mlp_regressor_model.joblib')
|
|
|
|
# Загрузка модели
|
|
mlp_regressor_model = joblib.load('mlp_regressor_model.joblib')
|
|
|
|
# Обновление маршрута для предсказания
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/predict', methods=['POST'])
|
|
def predict():
|
|
if request.method == 'POST':
|
|
# Получение данных из формы
|
|
input_data_bgg = {
|
|
'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'])
|
|
}
|
|
|
|
# Преобразование данных в DataFrame
|
|
input_df_bgg = pd.DataFrame([input_data_bgg])
|
|
|
|
# Масштабирование входных данных
|
|
input_data_scaled = scaler.transform(input_df_bgg)
|
|
|
|
# Предсказание
|
|
prediction_bgg = mlp_regressor_model.predict(input_data_scaled)[0]
|
|
|
|
return render_template('index.html', prediction_bgg=prediction_bgg)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|