From 2ab806693320386126c09e166131d29fbd70a059 Mon Sep 17 00:00:00 2001 From: ElEgEv <112943269+ElEgEv@users.noreply.github.com> Date: Fri, 3 Nov 2023 21:58:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LabWork01/FuncLoad.py | 2 +- LabWork01/LabWork5/create_plot.py | 198 +++++++++++++++++++++++++++++ LabWork01/LoadDB.py | 32 ++++- LabWork01/templates/main_page.html | 8 ++ 4 files changed, 237 insertions(+), 3 deletions(-) create mode 100644 LabWork01/LabWork5/create_plot.py diff --git a/LabWork01/FuncLoad.py b/LabWork01/FuncLoad.py index 08a34b0..9513ba0 100644 --- a/LabWork01/FuncLoad.py +++ b/LabWork01/FuncLoad.py @@ -1,6 +1,6 @@ import pandas as pd def createDataFrame(): - df = pd.read_csv('../res/Stores.csv') + df = pd.read_csv('res/Stores.csv') return df \ No newline at end of file diff --git a/LabWork01/LabWork5/create_plot.py b/LabWork01/LabWork5/create_plot.py new file mode 100644 index 0000000..bfb6247 --- /dev/null +++ b/LabWork01/LabWork5/create_plot.py @@ -0,0 +1,198 @@ +import os + +import numpy as np +import pandas as pd +import matplotlib +import matplotlib.pyplot as plt +import sns +from sklearn import metrics +from sklearn.model_selection import train_test_split +from sklearn.linear_model import LinearRegression + +INCH = 25.4 + +def create_plot_jpg(df: pd.DataFrame, nameFile): + # для сохранения диаграммы в конкретной папке + script_dir = os.path.dirname(__file__) + results_dir = os.path.join(script_dir, '../static/') + + if not os.path.isdir(results_dir): + os.makedirs(results_dir) + + # набор атрибутов - независимых переменных - площадь + _X = df["Store_Area"].array + + # набор меток - зависимых переменных, значение которых требуется предсказать - выручка + _Y = df["Store_Sales"].array + + # делим датафрейм на набор тренировочных данных и данных для тестов, test_size содержит определние соотношения этих наборов + X_train, X_test, y_train, y_test = train_test_split(_X, _Y, test_size=0.01, random_state=0) + + regressor = LinearRegression() + + X_train = X_train.reshape(-1, 1) + X_test = X_test.reshape(-1, 1) + + regressor.fit(X_train, y_train) + + # массив numpy, который содержит все предсказанные значения для входных значений в серии X_test + y_pred = regressor.predict(X_test) + + df.plot(x='Store_Sales', y='Store_Area', style='o') + + plt.title('Store sales / Store area') + plt.xlabel('Store sales') + plt.ylabel('Store area') + + plt.savefig(results_dir + nameFile + '.jpg') + plt.close() + + listMessages = ['Mean Absolute Error: ' + str(metrics.mean_absolute_error(y_test, y_pred)), + 'Mean Squared Error: ' + str(metrics.mean_squared_error(y_test, y_pred)), + 'Root Mean Squared Error: ' + str(np.sqrt(metrics.mean_squared_error(y_test, y_pred)))] + + return listMessages + +# def graph_regression_plot_sns( +# X, Y, +# regression_model, +# Xmin=None, Xmax=None, +# Ymin=None, Ymax=None, +# display_residuals=False, +# title_figure=None, title_figure_fontsize=None, +# title_axes=None, title_axes_fontsize=None, +# x_label=None, +# y_label=None, +# label_fontsize=None, tick_fontsize=12, +# label_legend_regr_model='', label_legend_fontsize=12, +# s=50, linewidth_regr_model=2, +# graph_size=None, +# file_name=None): +# X = np.array(X) +# Y = np.array(Y) +# Ycalc = Y - regression_model(X) +# +# if not (Xmin) and not (Xmax): +# Xmin = min(X) * 0.99 +# Xmax = max(X) * 1.01 +# if not (Ymin) and not (Ymax): +# Ymin = min(Y) * 0.99 +# Ymax = max(Y) * 1.01 +# +# # график с остатками +# # ------------------ +# if display_residuals: +# if not (graph_size): +# graph_size = (297 / INCH, 420 / INCH / 1.5) +# if not (title_figure_fontsize): +# title_figure_fontsize = 18 +# if not (title_axes_fontsize): +# title_axes_fontsize = 16 +# if not (label_fontsize): +# label_fontsize = 13 +# if not (label_legend_fontsize): +# label_legend_fontsize = 12 +# fig = plt.figure(figsize=graph_size) +# fig.suptitle(title_figure, fontsize=title_figure_fontsize) +# ax1 = plt.subplot(2, 1, 1) +# ax2 = plt.subplot(2, 1, 2) +# +# # фактические данные +# ax1.set_title(title_axes, fontsize=title_axes_fontsize) +# sns.scatterplot( +# x=X, y=Y, +# label='data', +# s=s, +# color='red', +# ax=ax1) +# ax1.set_xlim(Xmin, Xmax) +# ax1.set_ylim(Ymin, Ymax) +# ax1.axvline(x=0, color='k', linewidth=1) +# ax1.axhline(y=0, color='k', linewidth=1) +# # ax1.set_xlabel(x_label, fontsize = label_fontsize) +# ax1.set_ylabel(y_label, fontsize=label_fontsize) +# ax1.tick_params(labelsize=tick_fontsize) +# +# # график регрессионной модели +# nx = 100 +# hx = (Xmax - Xmin) / (nx - 1) +# x1 = np.linspace(Xmin, Xmax, nx) +# y1 = regression_model(x1) +# sns.lineplot( +# x=x1, y=y1, +# color='blue', +# linewidth=linewidth_regr_model, +# legend=True, +# label=label_legend_regr_model, +# ax=ax1) +# ax1.legend(prop={'size': label_legend_fontsize}) +# +# # график остатков +# ax2.set_title('Residuals', fontsize=title_axes_fontsize) +# ax2.set_xlim(Xmin, Xmax) +# # ax2.set_ylim(Ymin, Ymax) +# sns.scatterplot( +# x=X, y=Ycalc, +# # label='фактические данные', +# s=s, +# color='orange', +# ax=ax2) +# +# ax2.axvline(x=0, color='k', linewidth=1) +# ax2.axhline(y=0, color='k', linewidth=1) +# ax2.set_xlabel(x_label, fontsize=label_fontsize) +# ax2.set_ylabel(r'$ΔY = Y - Y_{calc}$', fontsize=label_fontsize) +# ax2.tick_params(labelsize=tick_fontsize) +# +# # график без остатков +# # ------------------- +# else: +# if not (graph_size): +# graph_size = (297 / INCH, 210 / INCH) +# if not (title_figure_fontsize): +# title_figure_fontsize = 18 +# if not (title_axes_fontsize): +# title_axes_fontsize = 16 +# if not (label_fontsize): +# label_fontsize = 14 +# if not (label_legend_fontsize): +# label_legend_fontsize = 12 +# fig, axes = plt.subplots(figsize=graph_size) +# fig.suptitle(title_figure, fontsize=title_figure_fontsize) +# axes.set_title(title_axes, fontsize=title_axes_fontsize) +# +# # фактические данные +# sns.scatterplot( +# x=X, y=Y, +# label='фактические данные', +# s=s, +# color='red', +# ax=axes) +# +# # график регрессионной модели +# nx = 100 +# hx = (Xmax - Xmin) / (nx - 1) +# x1 = np.linspace(Xmin, Xmax, nx) +# y1 = regression_model(x1) +# sns.lineplot( +# x=x1, y=y1, +# color='blue', +# linewidth=linewidth_regr_model, +# legend=True, +# label=label_legend_regr_model, +# ax=axes) +# +# axes.set_xlim(Xmin, Xmax) +# axes.set_ylim(Ymin, Ymax) +# axes.axvline(x=0, color='k', linewidth=1) +# axes.axhline(y=0, color='k', linewidth=1) +# axes.set_xlabel(x_label, fontsize=label_fontsize) +# axes.set_ylabel(y_label, fontsize=label_fontsize) +# axes.tick_params(labelsize=tick_fontsize) +# axes.legend(prop={'size': label_legend_fontsize}) +# +# plt.show() +# if file_name: +# fig.savefig(file_name, orientation="portrait", dpi=300) +# +# return \ No newline at end of file diff --git a/LabWork01/LoadDB.py b/LabWork01/LoadDB.py index 6e0eec2..46923ed 100644 --- a/LabWork01/LoadDB.py +++ b/LabWork01/LoadDB.py @@ -13,6 +13,7 @@ from LabWork01.LabWork3.CreateGraphics import createGraphics from LabWork01.LabWork3.CustomGraphics import createCusGraphics from LabWork01.LabWork3.DeletePng import deleteAllPng from LabWork01.LabWork4.SiteSearch import SiteSearch +from LabWork01.LabWork5.create_plot import create_plot_jpg app = Flask(__name__) @@ -45,7 +46,7 @@ search_engine.add("https://www.kaggle.com/datasets/fedesoriano/stroke-prediction @app.route("/") def home(): - return render_template('main_page.html', context=[], main_img=[], image_names=[], tableAnalys=[], titles=[''], listTypes=listTypes, countNull=countNull, firstRow=1, secondRow=4, firstColumn=1, secondColumn=4) + return render_template('main_page.html', context=[], main_img=[], messages=[], image_names=[], tableAnalys=[], titles=[''], listTypes=listTypes, countNull=countNull, firstRow=1, secondRow=4, firstColumn=1, secondColumn=4) @app.route("/showDiapason", methods=['GET','POST']) def numtext(): @@ -83,7 +84,7 @@ def numtext(): totalList.append(listStoreSales) if int(data['firstRow']) and int(data['secondRow']) and int(data['firstColumn']) and int(data['secondColumn']): - return render_template('main_page.html', context=totalList, main_img=[], image_names=[], listTypes=listTypes, countNull=countNull, + return render_template('main_page.html', context=totalList, main_img=[], messages=[], image_names=[], listTypes=listTypes, countNull=countNull, firstColumn=int(data['firstColumn']), secondColumn=int(data['secondColumn']), firstRow=int(data['firstRow']), secondRow=int(data['secondRow'])) @@ -157,6 +158,7 @@ def analysis(): tableAnalysThree=[], tableAnalysFour=[], main_img=newCustomJpg, + messages=[], titles=[''], listTypes=listTypes, countNull=countNull, firstRow=1, secondRow=4, firstColumn=1, secondColumn=4) @@ -184,6 +186,32 @@ def get_page_showFindURL(): return render_template('showLinks.html', links=links) +# 5-я лабораторная +@app.route('/createPlotImage', methods=['GET', 'POST']) +def get_plot_image(): + + # 99% + # main_df = listShops.loc[listShops['Store_ID'] <= listShops.shape[0]*0.9] + + # 1% + # support_df = listShops.loc[listShops['Store_ID'] > listShops.shape[0]*0.9] + + messages = create_plot_jpg(listShops, "myPlot") + + myPlotJpg = ['myPlot.jpg'] + + return render_template('main_page.html', context=[], image_names_start=[], + image_names_addition=[], + tableAnalysOne=[], + tableAnalysTwo=[], + tableAnalysThree=[], + tableAnalysFour=[], + main_img=myPlotJpg, + messages=messages, + titles=[''], + listTypes=listTypes, countNull=countNull, firstRow=1, + secondRow=4, firstColumn=1, secondColumn=4) + if __name__=="__main__": app.run(debug=True) diff --git a/LabWork01/templates/main_page.html b/LabWork01/templates/main_page.html index 21f3e4c..77a76b5 100644 --- a/LabWork01/templates/main_page.html +++ b/LabWork01/templates/main_page.html @@ -23,6 +23,9 @@
+
+ +
@@ -79,6 +82,11 @@ {% for image_name in main_img %} {{ image_name }} + {% for message in messages %} +
+

{{message}}

+
+ {% endfor %} {% endfor %}