Как подключить базу данных с машинном обучением в python?
Как создать бота и подключить модель pkl если у меня есть база данных [dataframe]
Есть только визуализация
import bot
from tkinter import *
import csv
def enter():
lableAnswer["text"] = bot.analiz(textCommand.get().lower())
l = bot.list()
lable2["text"] = bot.list_to_str(l)
fillability = bot.fill()
if fillability:
lableAnswer['text'] += "\nВы согласны с результатом?"
def enter_two():
fields = bot.list()
with open('путь к фалу', 'a') as f:
writer = csv.writer(f)
writer.writerow(fields)
# with open('titanic_train.csv', 'a', newline='') as csvFile:
# write = csv.writer(csvFile, delimiter=',')
# write.writerow([9999, 0, param[2], 'Name', param[0], param[1], 'test', 'test', 'test', 'test', 'test', 'test'])
root = Tk()
root.title('Bot')
root.geometry('600x500')
textCommand = StringVar()
questionsEntry = Entry(textvariable=textCommand)
questionsEntry.place(x=50, y=80, width=400)
lable1 = Label(text="setink")
lable1.place(x=50, y=50)
lable2 = Label(text="")
lable2.place(x=50, y=200)
lableAnswer = Label(text="")
lableAnswer.place(x=50, y=100)
button = Button(text="UP", command=enter)
button.place(x=450, y=80)
button2 = Button(text="save", command=enter_two)
button2.place(x=450, y=180)
root.mainloop()
Ответы (2 шт):
Автор решения: llin.1in
→ Ссылка
#import pandas as pd # загрузка pandas
#import numpy as np
#data.columns
#data = data1[['название', 'название 1',"название","%","название 2"]]
#for i in range(1, 10):
#data2 = data[[название', 'название 1',"название"+str(i),"%"+str(i),"знания"+str(i)]]
#data2.columns = ['название', 'Описание',"Показатель","%","название 2"]
#data1 = data1.append(data2)
#data1.isna().sum()
#data.dropna(inplace = True)
#data3 = data1['название 2'].str.split(';',expand=True)
#dff = pd.concat([data,data3[0]], axis = 1)
#dff.columns = ['название', 'название 1',"название","%","название 2",название 22]
#for i in range(1, 44):
#data5 = pd.concat([data1,data3[i]], axis = 1)
#data5.columns = [['название', 'название 1',"название","%","название 2",название 22]
#dff = dff.append(data5)
#dff["22"][0]
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
scal = StandardScaler()
df_scal = scal.fit_transform(dtt.drop('building_age',axis=1))
y = dtt['building_age']
x = df_scal.copy()
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=13)
dt['sub_type'] = pd.factorize(dt.sub_type)[0]
Автор решения: llin.1in
→ Ссылка
import pandas as pd
import joblib
#dt = pd.read_csv("путь")
R_tree = joblib.load("путь")
fillability = False
sub_type = -1
listing_type = -1
tom = -1
building_age = -1
total_floor_count = -1
floor_no = -1
room_count = -1
size = -1
address = -1
heating_type = -1
pred = -1
keywords_default = [""]
keywords_sub_type = ["]
keywords_listing_type_sale = [""]
keywords_listing_type_rent = [""]
keywords_tom = [""]
keywords_building_age = [""]
keywords_total_floor_count = [""]
keywords_floor_no = [""]
keywords_room_count = [""]
keywords_size = [""]
keywords_heating_type = ["]
def fill():
return fillability
def list():
fields = [sub_type, listing_type, tom, building_age, total_floor_count, floor_no, room_count, size, heating_type, pred]
return fields
def list_to_str(fields):
str1 = ' '.join(str(e) for e in fields)
return str1
def analiz(tex):
global sub_type
global listing_type
global tom
global building_age
global total_floor_count
global floor_no
global room_count
global size
global heating_type
global fillability
text = tex
textList = text.split()
for i in range(len(keywords_sub_type)):
if text.find(keywords_sub_type[i]) != -1:
sub_type = i
for i in range(len(textList)):
for j in range(len(keywords_tom)):
if textList[i].find(keywords_tom[j]) != -1:
tom = textList[i - 1]
for i in range(len(keywords_listing_type_sale)):
if text.find(keywords_listing_type_sale[i]) != -1:
listing_type = 1
for i in range(len(keywords_listing_type_rent)):
if text.find(keywords_listing_type_rent[i]) != -1:
listing_type = 2
for i in range(len(textList)):
for j in range(len(keywords_building_age)):
if textList[i].find(keywords_building_age[j]) != -1:
building_age_l = float(textList[i - 1])
if building_age_l == 0:
building_age = 0
elif building_age_l == 1:
building_age = 1
for i in range(len(textList)):
for j in range(len(keywords_total_floor_count)):
if textList[i].find(keywords_total_floor_count[j]) != -1:
total_floor_count_l = float(textList[i - 1])
if total_floor_count_l == 1:
total_floor_count = 0
elif total_floor_count_l == 10:
total_floor_count = 1
for i in range(len(textList)):
for j in range(len(keywords_floor_no)):
if textList[i].find(keywords_floor_no[j]) != -1:
floor_no_l = float(textList[i - 1])
if floor_no_l >= 20:
floor_no = 12
elif floor_no_l >= 10:
floor_no = floor_no_l - 9
for i in range(len(textList)):
for j in range(len(keywords_room_count)):
if textList[i].find(keywords_room_count[j]) != -1:
room_count = textList[i - 1]
for i in range(len(textList)):
for j in range(len(keywords_size)):
if textList[i].find(keywords_size[j]) != -1:
size = textList[i - 1]
for i in range(len(keywords_heating_type)):
if text.find(keywords_heating_type[i]) != -1:
heating_type = i
if sub_type == -1:
fillability = False
return "Укажите пожалуйста тип "
else:
fillability = True
if listing_type == -1:
fillability = False
return
else:
fillability = True
if tom == -1:
fillability = False
return
else:
fillability = True
if building_age == -1:
fillability = False
return
else:
fillability = True
if total_floor_count == -1:
fillability = False
return
else:
fillability = True
if floor_no == -1:
fillability = False
return
else:
fillability = True
if room_count == -1:
fillability = False
return
else:
fillability = True
if size == -1:
fillability = False
return
else:
fillability = True
if heating_type == -1:
fillability = False
return
else:
fillability = True
if fillability:
data = {"sub_type": [sub_type], 'start_date': [18], 'end_date':[19],'listing_type': [listing_type], 'tom': [tom],
'building_age': [building_age], 'total_floor_count': [total_floor_count], 'floor_no': [floor_no],
'room_count': [room_count], 'size': [size],'address':[123], 'heating_type': [heating_type]}
df_new = pd.DataFrame(data)
pred = R_tree.predict(df_new)
return " " + str(pred)