LabWork6 completed.

This commit is contained in:
ElEgEv 2023-11-23 16:12:33 +04:00
parent a73b247471
commit 9d7bd5297c
5 changed files with 46 additions and 13 deletions

View File

@ -1,6 +1,6 @@
import pandas as pd import pandas as pd
def covertorDataFrame(): def CovertorDataFrame():
df = pd.read_csv("res/Stores.csv") df = pd.read_csv("res/Stores.csv")
@ -14,7 +14,7 @@ def covertorDataFrame():
lambda x: 'Small_Area' if x <= 1100 else ('Average_Area' if 1100 < x <= 1700 else 'Big_Area')) lambda x: 'Small_Area' if x <= 1100 else ('Average_Area' if 1100 < x <= 1700 else 'Big_Area'))
mainDF['TextStoreSales'] = mainDF['Store_Sales'].apply( mainDF['TextStoreSales'] = mainDF['Store_Sales'].apply(
lambda x: 'Small_Sales' if x <= 50000 else ('Average_Sales' if 50000 < x <= 80000 else 'Big_Sales')) lambda x: 'Small_Sales' if x <= 50000 else ('Average_Sales' if 50000 < x <= 70000 else 'Big_Sales'))
mainDF['TextDailyCustomerCount'] = mainDF['Daily_Customer_Count'].apply( mainDF['TextDailyCustomerCount'] = mainDF['Daily_Customer_Count'].apply(
lambda x: 'Small_Customer' if x <= 400 else ('Average_Customer' if 400 < x <= 900 else 'Big_Customer')) lambda x: 'Small_Customer' if x <= 400 else ('Average_Customer' if 400 < x <= 900 else 'Big_Customer'))

View File

@ -2,7 +2,7 @@ import math
import pandas as pd import pandas as pd
from functools import reduce from functools import reduce
from LabWork01.LabWork6.ConvertorDataFrame import covertorDataFrame from LabWork01.LabWork6.ConvertorDataFrame import CovertorDataFrame
# Дата сет # Дата сет
# data = [ # data = [
@ -18,7 +18,7 @@ from LabWork01.LabWork6.ConvertorDataFrame import covertorDataFrame
# df0 = pd.DataFrame(data) # df0 = pd.DataFrame(data)
# df0.columns = ["цвет", "форма", "результат"] # df0.columns = ["цвет", "форма", "результат"]
df0 = covertorDataFrame()[0] df0 = CovertorDataFrame()[0]
# Лямбда-выражение для распределения значений, аргумент - pandas.Series, # Лямбда-выражение для распределения значений, аргумент - pandas.Series,
# возвращаемое значение - массив с количеством каждого из значений # возвращаемое значение - массив с количеством каждого из значений

View File

@ -1,19 +1,20 @@
import math import math
from functools import reduce from functools import reduce
from LabWork01.LabWork6.ConvertorDataFrame import covertorDataFrame from LabWork01.LabWork6.ConvertorDataFrame import CovertorDataFrame
# дата-сет # дата-сет
df0 = covertorDataFrame()[0] dfMain = CovertorDataFrame()[0]
dfTest = CovertorDataFrame()[1]
cstr = lambda s: [k + ":" + str(v) for k, v in sorted(s.value_counts().items())] cstr = lambda s: [k + ":" + str(v) for k, v in sorted(s.value_counts().items())]
# Структура данных Decision Tree # Структура данных Decision Tree
tree = { tree = {
# name: Название этого нода (узла) # name: Название этого нода (узла)
"name": "decision tree " + df0.columns[-1] + " " + str(cstr(df0.iloc[:, -1])), "name": "decision tree " + dfMain.columns[-1] + " " + str(cstr(dfMain.iloc[:, -1])),
# df: Данные, связанные с этим нодом (узлом) # df: Данные, связанные с этим нодом (узлом)
"df": df0, "df": dfMain,
# edges: Список ребер (ветвей), выходящих из этого узла, или пустой массив, если ниже нет листового узла. # edges: Список ребер (ветвей), выходящих из этого узла, или пустой массив, если ниже нет листового узла.
"edges": [], "edges": [],
} }
@ -61,13 +62,37 @@ while (len(open) != 0):
pass pass
# Выводим дата сет # Выводим дата сет
print(df0, "\n-------------") print(dfMain, "\n-------------")
# оценка тестовых данных
def predict_bp(nodes, target) -> int:
overlap = None
for node in nodes:
check: bool = node["value"] == target[node["attr"]]
if check:
overlap = node
break
if overlap is None:
overlap = nodes[-1]
if len(overlap["edges"]) == 0:
return int(overlap["df"]["StoreSales"].mean())
else:
return predict_bp(overlap["edges"], target)
def predict_str(count: int):
predictions = []
for i in range(count):
row = dfTest.iloc[i]
prediction = f"{ {'Age': row['Age'], 'BMI': row['BMI']} }" + \
f"<br/>predict {predict_bp(tree['edges'], {'Age': row['Age'], 'BMI': row['BMI']})} / fact {row['BloodPressure']}"
predictions.append(prediction)
return '<br/>'.join(predictions)
def tstr(tree, indent=""): def tstr(tree, indent=""):
s = indent + tree["name"] + str(cstr(tree["df"].iloc[:, -1]) if len(tree["edges"]) == 0 else "") + "\n" s = indent + tree["name"] + str(cstr(tree["df"].iloc[:, -1]) if len(tree["edges"]) == 0 else "") + "\n"
# Зацикливаем все ветви этого узла. # Зацикливаем все ветви этого узла.
for e in tree["edges"]: for e in tree["edges"]:
s += tstr(e, indent + " ") s += tstr(e, "\t" + indent + " ")
pass pass
return s return s

View File

@ -14,7 +14,7 @@ from LabWork01.LabWork3.CustomGraphics import createCusGraphics
from LabWork01.LabWork3.DeletePng import deleteAllPng from LabWork01.LabWork3.DeletePng import deleteAllPng
from LabWork01.LabWork4.SiteSearch import SiteSearch from LabWork01.LabWork4.SiteSearch import SiteSearch
from LabWork01.LabWork5.create_plot import create_plot_jpg from LabWork01.LabWork5.create_plot import create_plot_jpg
from LabWork01.LabWork6.ConvertorDataFrame import covertorDataFrame from LabWork01.LabWork6.ConvertorDataFrame import CovertorDataFrame
from LabWork01.LabWork6.Tree import getStringTree from LabWork01.LabWork6.Tree import getStringTree
app = Flask(__name__) app = Flask(__name__)
@ -216,7 +216,7 @@ def get_plot_image():
@app.route('/createTree', methods=['GET']) @app.route('/createTree', methods=['GET'])
def get_data_Tree(): def get_data_Tree():
DataTree = getStringTree() DataTree = getStringTree().split('\n')
return render_template('tree_page.html', DataTree=DataTree) return render_template('tree_page.html', DataTree=DataTree)

View File

@ -23,7 +23,15 @@
</div> </div>
</form> </form>
<div class=""> <div class="">
<p>{{ DataTree }}</p> {% for item in DataTree %}
{% if item.count("\t") == 1 %}
<p style="margin-left: 50px;"> {{ item }}</p>
{% elif item.count("\t") == 2 %}
<p style="margin-left: 100px;"> {{ item }}</p>
{% else %}
<p>{{ item }}</p>
{% endif %}
{% endfor %}
</div> </div>
</div> </div>
</body> </body>