I am very new to programming would really appreciated some help. I have developed a ML model using RandomForest. My code works fine when I run my model standalone, but when I try to send data to my code using Tkinter wrapper function I get the value error.
My model code is as follows
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from joblib import dump, load
from sklearn.ensemble import RandomForestRegressor
housing = pd.read_csv("housing_data.csv")
train_set, test_set = train_test_split(housing, test_size=0.2, random_state=42)
splitter = StratifiedShuffleSplit(n_splits = 1, test_size = 0.2, random_state=42)
for train_index, test_index in splitter.split(housing, housing['CHAS']):
strat_train_set = housing.loc[train_index]
strat_test_set = housing.loc[test_index]
median = housing["RM"].median()
housing["RM"].fillna(median)
housing = strat_train_set.drop("MEDV", axis = 1)
housing_labels = strat_train_set["MEDV"].copy()
my_pipeline = Pipeline([
('imputer', SimpleImputer(strategy = "median")), #imputer takes the median value and fills all the blank fields
('std_scaler', StandardScaler()),
])
housing_pip = my_pipeline.fit_transform(housing)
print(housing_pip.shape)
model_random_forest = RandomForestRegressor()
model_random_forest.fit(housing_pip, housing_labels)
dump(model_random_forest, 'Dragon.joblib')
I am using a fucntion to enter the values and get the predictions. Function code is as follows
a=[]
b=[]
c=[]
d=[]
e=[]
f=[]
g=[]
h=[]
i=[]
j=[]
k=[]
l=[]
m=[]
def predictor(array = np.array([[a],[b],[c],[d],[e],[f],[g],[h],[i],[j],[k],[l],[m]])):
print(model_random_forest.predict(array))
predictor([[1,2,3,4,5,6,7,8,9,10,11,12,13]])
Code works absolutely fine but when I use Tkinter GUI wrapper function to send the values to this function it gives ValueError: Number of features of the model must match the input. Model n_features is 13 and input n_features is 1
My Tkinter wrapper function is as follows
def make_predict():
test.predictor([[crim.get()],[zn.get()],[indus.get()],[chas.get()],[nox.get()],[rm.get()],[age.get()],[dis.get()],[rad.get()],[tax.get()],[ptratio.get()],[b.get()],[lstat.get()]])
crim.get, zn.get etc. are entry boxes in tkinter. Guys I would really appreciate your help. I am new to programming and have already spen lots of time solving this error.
Just in case I am also pasting my complete Tkinter code below
from tkinter import*
import numpy as np
import test
window = Tk()
def make_predict():
test.predictor([[crim.get()],[zn.get()],[indus.get()],[chas.get()],[nox.get()],[rm.get()],[age.get()],
[dis.get()],[rad.get()],[tax.get()],[ptratio.get()],[b.get()],[lstat.get()]])
#t1.delete(0,END)
#t1.insert(END,(model_random_forest.predict(features)))
window.title("Property Price Predictor")
window.geometry('500x800')
mainLabel = Label(window, text="Property Price Predictor", font=("Arial Bold", 20))
mainLabel.grid(row=2, columnspan=8)
crim=StringVar()
e1 = Entry(window, width = 25, textvariable=crim) #crim
e1.grid(row = 3, column = 2)
l1 = Label(window, text = 'PER CAPITA CRIME RATE BY TOWN')
l1.grid(row = 4, column = 2)
zn = StringVar()
e2 = Entry(window, width = 25, textvariable=zn) #ZN
e2.grid(row = 6, column = 2)
l2 = Label(window, text = 'PROPORTION OF RESIDENTIAL LAND ZONED FOR LOTS OVER 25,000 SQ.FT.')
l2.grid(row = 7, column = 2)
indus=StringVar()
e3 = Entry(window, width = 25, textvariable=indus) #INDUS
e3.grid(row = 9, column = 2)
l3 = Label(window, text = 'PROPORTION OF NON-RETAIL BUSINESS ACRES PER TOWN')
l3.grid(row = 10, column = 2)
chas=StringVar()
e4 = Entry(window, width = 25, textvariable=chas) #CHAS
e4.grid(row = 12, column = 2)
l4 = Label(window, text = 'CHARLES RIVER DUMMY VARIABLE (= 1 IF TRACT BOUNDS RIVER; 0 OTHERWISE)')
l4.grid(row = 13, column = 2)
nox=StringVar()
e5 = Entry(window, width = 25, textvariable=nox) #NOX
e5.grid(row = 15, column = 2)
l5 = Label(window, text = 'NITRIC OXIDES CONCENTRATION (PARTS PER 10 MILLION)')
l5.grid(row = 16, column = 2)
rm=StringVar()
e6 = Entry(window, width = 25, textvariable=rm) #RM
e6.grid(row = 18, column = 2)
l6 = Label(window, text = 'AVERAGE NUMBER OF ROOMS PER DWELLING')
l6.grid(row = 19, column = 2)
age=StringVar()
e7 = Entry(window, width = 25, textvariable=age) # AGE
e7.grid(row = 21, column = 2)
l7 = Label(window, text = 'PROPORTION OF OWNER-OCCUPIED UNITS BUILT PRIOR TO 1940')
l7.grid(row = 22, column = 2)
dis=StringVar()
e8 = Entry(window, width = 25, textvariable=dis) #DIS
e8.grid(row = 24, column = 2)
l8 = Label(window, text = 'WEIGHTED DISTANCES TO FIVE BOSTON EMPLOYMENT CENTRES')
l8.grid(row = 25, column = 2)
rad=StringVar()
e9 = Entry(window, width = 25, textvariable=rad) #RAD
e9.grid(row = 27, column = 2)
l9 = Label(window, text = 'INDEX OF ACCESSIBILITY TO RADIAL HIGHWAYS')
l9.grid(row = 28, column = 2)
tax=StringVar()
e10 = Entry(window, width = 25, textvariable=tax ) #TAX
e10.grid(row = 30, column = 2)
l10 = Label(window, text = 'FULL-VALUE PROPERTY-TAX RATE PER $10,000')
l10.grid(row = 31, column = 2)
ptratio=StringVar()
e11 = Entry(window, width = 25, textvariable=ptratio) #PTRATIO
e11.grid(row = 33, column = 2)
l11 = Label(window, text = 'PUPIL-TEACHER RATIO BY TOWN')
l11.grid(row = 34, column = 2)
b=StringVar()
e12 = Entry(window, width = 25, textvariable=b) #B
e12.grid(row = 36, column = 2)
l12 = Label(window, text = '1000(BK - 0.63)^2 WHERE BK IS THE PROPORTION OF BLACKS BY TOWN')
l12.grid(row = 37, column = 2)
lstat=StringVar()
e13 = Entry(window, width = 25, textvariable=lstat) #LSTAT
e13.grid(row = 39, column = 2)
l13 = Label(window, text = '% LOWER STATUS OF THE POPULATION')
l13.grid(row = 40, column = 2)
t1 = Text(window, height = 1, width = 20)
t1.grid(row = 45, column = 2)
l15 = Label(window, text = 'PREDICTION')
l15.grid(row = 46, column = 2)
db1 = Button(window, text = 'PREDICT', width=20, command = make_predict)
db1.grid(row=48, column=2)
window.mainloop()
Your housing dataset most probably has missing values
you need to drop the columns with missing values instead of imputing them.
Here is the code:
First check for the missing values in your dataset
housing_train = X_train
missing_values_columns = [col for col in housing_train.columns
if housing_train[col].isnull().any()]
housing_transformed = X_test.drop(missing_values_columns, axis=1
model_random_forest = RandomForestRegressor()
model_random_forest.fit(housing_transformed, housing_labels)
Related
making an F1 application where users can type in a driver from the current grid [supported via autofill/suggestions], and then API fetches relevant stats
The issue I'm having is that I want the listbox to appear over another frame (called left_frame). However, what this does is that the listbox goes under left_frame , and not over it. Any thoughts of how to make it appear over the frame?
some approaches that I tried:
used .lift(), but .lift() only seems to work when it's relative to objects in the same frame
since I currently use .grid and placed the listbox in the root frame, I initially tried to put it in top_frame, but it then expands the top_frame. Since I have positioned all frames using .place(), I don't think I can use propagate(False) (I tried it , didn't work)
Here's a picture of the issues
Here's my code so far:
#import necessary modules
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
from tkinter import messagebox
import requests
import json
#set up main window components
root = Tk()
root.title("F1 Desktop Application")
root.geometry("500x600")
root.configure(bg="white")
#since same directory, can just use filename
root.iconbitmap("formula1_logo.ico")
#images
#creating canvas for image to go into
#canvas_f1_logo = Canvas(top_frame, width = 5, height = 5)
#canvas.grid(row = 0, column = 1)
#acquire image
#formula1_logo = ImageTk.PhotoImage(Image.open("formula1_logo.png"))
#resize image
#resized_formula1_logo = formula1_logo.resize((3,3), Image.ANTIALIAS)
#new_formula1_logo = ImageTK.PhotoImage(resized_formula1_logo)
#canvas.create_image(5,5, anchor = NW, image = new_formula1_logo)
#functions
#generate 2022 drivers-list [can scale to drivers-list by changing the]
drivers_list_request = requests.get("http://ergast.com/api/f1/2022/drivers.json")
#initialize empty-list
drivers_list = []
drivers_list_object = json.loads(drivers_list_request.content)
for elements in drivers_list_object["MRData"]["DriverTable"]["Drivers"]:
drivers_list.append(elements["givenName"] + " " + elements["familyName"])
#set up frames [main frames, can put frames within frames if needed]
#frame for search bar + magnifying glass
top_frame = LabelFrame(root, padx = 80, pady = 15)
top_frame.place(relx = 0.5, rely = 0.2, anchor = CENTER)
header_label = Label(top_frame, text = "F1 2022 Drivers App", pady = 20, font = ("Arial bold",14))
header_label.grid(row = 0, column = 0, pady = 2 )
search_button = Button(top_frame, text = "search", padx = 2, pady = 2)
search_button.grid(row = 1, column = 1)
# Update the Entry widget with the selected item in list
def check(e):
v= entry.get()
if v=='':
hide_button(menu)
else:
data=[]
for item in drivers_list:
if v.lower() in item.lower():
data.append(item)
update(data)
show_button(menu)
def update(data):
# Clear the Combobox
menu.delete(0, END)
# Add values to the combobox
for value in data:
menu.insert(END,value)
def fillout(event):
try:
entry.delete(0,END)
entry.insert(0,menu.get(menu.curselection()))
#handle a complete deletion of entry-box via cursor double tap
except:
pass
def hide_button(widget):
widget.grid_remove()
def show_button(widget):
widget.grid()
# Create an Entry widget
entry= Entry(top_frame)
entry.grid(row = 1, column = 0)
entry.bind('<KeyRelease>',check)
# Create a Listbox widget to display the list of items
menu= Listbox(root)
menu.grid(row = 2, column = 0, padx = 165, pady = 165)
menu.bind("<<ListboxSelect>>",fillout)
menu.lift()
# Add values to our combobox
hide_button(menu)
left_frame = LabelFrame(root, padx = 30, pady = 30)
left_frame.place(relx = 0.24, rely = 0.5, anchor = CENTER)
bottom_left_frame = LabelFrame(root, padx = 30, pady = 30)
bottom_left_frame.place(relx = 0.24, rely = 0.82, anchor = CENTER)
bottom_right_frame = LabelFrame(root, padx = 30, pady = 30)
bottom_right_frame.place(relx = 0.6, rely = 0.82)
basic_info = Label(left_frame, text = "Basic Info ", font = ("Arial bold",14))
basic_info.grid(row = 0, column = 0, pady = 3)
full_name = Label(left_frame, text = "Full Name : ")
full_name.grid(row = 1, column = 0, pady = 2)
driver_code = Label(left_frame, text = "Driver Code : ")
driver_code.grid(row = 2, column = 0, pady = 2)
nationality = Label(left_frame, text = "Nationality : ")
nationality.grid(row = 3, column = 0, pady = 2)
F1_career = Label(bottom_left_frame, text = "F1 Career ", font = ("Arial bold",14))
F1_career.grid(row = 0, column = 0, pady = 3)
wins = Label(bottom_left_frame, text = "Wins :")
wins.grid(row = 1, column = 0, pady = 2)
poles = Label(bottom_left_frame, text = "Poles :")
poles.grid(row = 2, column = 0, pady = 2)
drivers_championships = Label(bottom_left_frame, text = "Championships :")
drivers_championships.grid(row = 3, column = 0 , pady = 2)
F1_22_stats = Label(bottom_right_frame, text = "F1 22 Stats", font = ("Arial bold",14))
F1_22_stats.grid(row = 0, column = 0, pady = 3)
root.mainloop()
would appreciate the help!
i want to build a selection from a dataframe based on double search-arguments. In detail:
I have build a search within a given Dataframe based on one value. This value can be given multiple times. The difference can be found in the row of the value. Here is a example:
TECHNIKART FSZ BEZEICHNUNG FIRMA PRODUKT_SPANNUNG KLIMA EW_KW MENGE_KW LEISTUNG_KW Ü
I get this Dataframe from following search in a bigger Dataframe:
key = '71E'
try:
geraet_dat2 = geraet_dat[geraet_dat.FSZ == key]
lang = len(geraet_dat2.index)
print(geraet_dat2)
My idea is now to have a if-else-query based on the length of the Dataframe. If the Dataframe has the lenght 1 i will print the values of the needed cells. If the Dataframe is longer than 1 the following popup appears:
def abfrage_selektion():
popup_selektion = Tk()
frame_selek = Frame(popup_selektion)
frame_selek.pack()
popup_selektion.configure(bg="Snow2")
popup_selektion.geometry('300x250')
popup_selektion.title('Bitte Gerät auswählen!')
LABEL_SELEKTION = LabelFrame(popup_selektion, text="Bitte Gerät auswählen: ")
LABEL_SELEKTION.place(x = 2, y = 2, width = 296, height = 246)
INFO = ttk.Label(LABEL_SELEKTION, justify=CENTER, text="Diese FSZ ist mehrfach verfügbar! \nBitte das entsprechende Gerät wählen!")
INFO.place(x = 50, y = 5)
SEPTR_1 = ttk.Separator(LABEL_SELEKTION, orient=HORIZONTAL)
SEPTR_1.place(x = 5, y = 40, width = 280)
GERAET = ttk.LabelFrame(LABEL_SELEKTION, text="Geräte: ")
GERAET.place(x = 15, y = 50, width = 260, height = 120)
LISTE = Listbox(GERAET, yscrollcommand=True)
LISTE.insert(1, '1')
LISTE.insert(2, '2')
LISTE.insert(3, '3')
LISTE.insert(4, '4')
LISTE.place(x = 5, y = 2, width = 240, height = 90)
SEPTR_2 = ttk.Separator(LABEL_SELEKTION, orient=HORIZONTAL)
SEPTR_2.place(x = 5, y = 180, width = 280)
BUTTON_OK = ttk.Button(LABEL_SELEKTION, text="OK")
BUTTON_OK.place(x = 5, y = 190, width = 280, height = 30)
that far everything is nice an clear for me.
But now the point i dont know how to solve:
In the popup i want a selection from the given Dataframe with more than one Dataset to select the wanted Data. The criteria for the selection should be 'BEZEICHNUNG' and 'PRODUKT_SPANNUNG'.
Do you have any ideas how i can do this? I searched in the web but didnt find a good solution for that.
Thank you for you help!
I've made a simple gui age converter app but i want to change its bg color to black.
The problem is in the ttk frame.i don't know how to configure its bg color.
I have tried different methods but that didn't work.
i would be grateful if you guys could help.
here is the code
from tkinter import *
from tkinter import ttk
from PIL import Image
def calculate(*args):
try:
age_sec = int(age.get())
age_sec = age_sec * 12 * 365 * 24 * 60 * 60
age_seconds.set(age_sec)
except:
age_seconds.set('Either the field is empty or \n value is not numeric.')
root = Tk()
root.title("Converter")
root.configure(background="black")
mainframe = ttk.Frame(root, padding = "6 6 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
#mainframe['borderwidth'] = 2
#mainframe['relief'] = 'groove'
age = StringVar()
age_seconds = StringVar()
ttk.Label(mainframe, foreground = "#4D4E4F", text = "Enter your Age: ").grid(column = 1, row = 1, sticky = E)
age_entry = ttk.Entry(mainframe, width = 30, textvariable = age)
age_entry.grid(column = 2, row = 1, sticky = (W, E))
ttk.Label(mainframe, foreground = "#4D4E4F", text = "your age in seconds is ").grid(column = 1, row = 2, sticky = (E))
ttk.Label(mainframe, textvariable = age_seconds, background = "lightyellow", foreground = "#727475", width = 30).grid(column = 2, row = 2, sticky = W)
#Mouse Events...\\
butt_image = PhotoImage(file = 'images.gif')
ttk.Button(mainframe, compound = TOP, text = "Hit Now", image =butt_image, cursor = "hand2", width= 30, command = calculate).grid(column = 2, row = 4, sticky = W)
l2 = ttk.Label(mainframe,foreground = "#4D4E4F", text = "Mouse Events: ").grid(column = 1, row = 3, sticky = E)
l = ttk.Label(mainframe,background = "lightyellow", foreground = "#727475", text = 'Measurement is starting...', width = 30)
l.grid(column = 2, row = 3, sticky = W)
l.bind('<Enter>', lambda e: l.configure(text = 'Moved Mouse Inside'))
l.bind('<Leave>', lambda e: l.configure(text = 'Mouse Moved Out'))
l.bind('<1>', lambda e: l.configure(text = 'left Mouse clicked'))
l.bind('<Double-1>', lambda e: l.configure(text = 'Double clicked'))
l.bind('<B1-Motion>', lambda e: l.configure(text = 'Left button drag to %d, %d' %(e.x, e.y)))
image = PhotoImage(file = 'waves.gif')
ttk.Label(mainframe, compound = CENTER, text = "Image text", font = ('roman', 9, 'normal'), foreground ='green', image = image).grid(column = 3, row = 1, sticky = (N, E))
#if '__name__' == '__main__':
for child in mainframe.winfo_children(): child.grid_configure(padx = 15, pady = 15)
age_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()
A ttk.Frame does not have a background / bg option. To change it you need to use style.
See ttk.Frame Info
If you don't really need to use a ttk.Frame you can just switch to a tkinter.Frame
I'm trying to create a list of values and later be able to read, sort and search them later. Although when I write to my csv file, it skips the first line and prints on the next.
I've looked all over for the solution internally and externally but nothing helps.
Here's my code:
from tkinter import *
import csv
window=Tk()
def show_entry_fields():
with open('shoplist.csv', 'a') as csvfile:
list = csv.writer(csvfile, delimiter=',', lineterminator='\n')
list.writerow([ "%s" % (e3.get()) "%s" % (e4.get()), "%s" % (e5.get())])
Button(window, text='Add', command=show_entry_fields).grid(row=3, column=3)
v = StringVar()
e3 = Entry(window)
e4 = Entry(window)
e5 = Entry(window)
e3.grid(row = 3, column = 1)
e4.grid(row = 4, column = 1)
e5.grid(row = 5, column = 1)
l3 = Label(window, text = "Name:")
l3.grid(row = 3, column = 0)
l4 = Label(window, text = "Location:")
l4.grid(row = 4, column = 0)
l5 = Label(window, text = "Rate /5:")
l5.grid(row = 5, column = 0)
Trying to show a green or red background in the text field of the answer to the simple addition quizzer.
Currently in PyCHarm complains that:
Entry.grid_configure(background = "red")
TypeError: grid_configure() missing 1 required positional argument: 'self'
0
I can't seem to figure this out. Any help is appreciated.
Here's the code so far:
from tkinter import *
import random
class MainGUI:
def __init__(self):
window = Tk() # Create the window
window.title("Addition Quizzer") # Set the title
#window.width(len(window.title()))
self.number1 = random.randint(0, 9)
self.number2 = random.randint(0, 9)
Label(window, text = "+").grid(row = 2, column = 1, sticky = E)
Label(window, text = "Answer").grid(row = 3, column = 1, sticky = W)
self.firstNumber = StringVar()
Label(window, text = self.number1, justify = RIGHT).grid(row = 1, column = 2)
self.secondNumber = StringVar()
Label(window, text = self.number2, justify = RIGHT).grid(row = 2, column = 2)
self.entry = StringVar()
Entry(window, textvariable = self.entry, justify = CENTER, width = 4, background = "grey").grid(row = 3, column = 2)
Button(window, text = "Answer:", command = self.computeAnswer).grid(row = 4, column = 1, sticky = E)
self.result = StringVar()
Label(window, textvariable = self.result).grid(row = 4, column = 2)
window.mainloop() # Create the event loop
def computeAnswer(self):
self.result.set(format(self.number1 + self.number2))
if self.entry == self.result:
self.displayCorrect()
else:
self.displayIncorrect()
def displayCorrect(self):
# self.correctAnswer = "Correct"
# Label(self.window, text = self.correctAnswer, background = "green", justify = RIGHT).grid(row = 5, column = 2)
Entry.grid_configure(background = "green")
def displayIncorrect(self):
# self.incorrectAnswer = "Incorrect"
# Label(self.window, text = self.incorrectAnswer, background = "red", justify = RIGHT).grid(row = 5, column = 2)
Entry.grid_configure(background = "red")
MainGUI()
If you had read and followed this in the Help Center material, you would have reduced your code to the following, which still gets the same error message.
from tkinter import *
Entry.grid_configure()
The message refers to the fact that Python instance methods require an instance. This is usually done by calling the method on an instance instead of the class. Otherwise, an instance must be given as the first argument. Consider
mylist = []
mylist.append(1)
list.append(mylist, 2)
print(mylist)
# [1, 2]
You need to save a reference to your Entry box. Change
Entry(window, ..., background = "grey").grid(...)
to
self.entry = Entry(window, ..., background = "grey").grid(...)
I do not know if calling .grid_configure(background=color will do what you want.
This will, I am sure.
self.entry['background'] = 'red'