473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

tkinter, Entry does not get the variable values for the text box , or replace the new

What I am trying to achieve is to firstly read the intvariable values defined globally and insert into a textbox when the program is ran, secondly I need to enable the user to change the values if they wish to.
the code is:
from numpy import *
import numpy as np
import matplotlib.pyplot as plt
import os
from matplotlib import style
from utils import DATA_DIR
from tkinter import *

style.use("ggplot")
# y = mx + b
# m is slope, b is y-intercept

##Creating the interface
root=Tk()
root.wm_title("Linear Regression")
root.geometry("450x450+500+300")





###LoadData

data = np.loadtxt(os.path.join(DATA_DIR, "data.csv"),delimiter=",")

x = data[:, 0]
y = data[:, 1]


# I have set these values global so they can be called into the text box


LRrtt = float(0.00001)
initb = int(35) # initial y-intercept guess
initm = int(0) # initial slope guess
iternum =int(50)

## interface labels etc
lbl1 = Label(root, text="Initial intercept").grid(row=0,sticky='E')
lbl2 = Label(root, text="Initial slope").grid(row=1,sticky='E')
lbl3 = Label(root, text="learning rate").grid(row=2,sticky='E')
lbl4 = Label(root, text="num_iteration").grid(row=3,sticky='E')
txtb = Entry(root,textvariable=initb).grid(row=0, column=1)
txtm= Entry(root,textvariable = initm).grid(row=1, column=1)
txtlr= Entry(root,textvariable= LRrtt).grid(row=2, column=1)
txtiter= Entry(root,textvariable=iternum).grid(row=3, column=1)





### Defining error function f(x)
def compute_error_for_line_given_points(b, m, points):
fx = 0
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
fx += (y - (m * x + b)) ** 2 #TSSE
return fx / float(len(points))


### Initialising the search for the best line fit(starting with any two values for m&b)
###allows the gradient descent algorithm to tries to reduce the error function f(x)
###to find the best fit line

def step_gradient(b_current, m_current, points, learningRate):
b_intercept = 0
m_gradient = 0
N = float(len(points))
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
b_intercept += -(2/N) * (y - ((m_current * x) + b_current))
m_gradient += -(2/N) * x * (y - ((m_current * x) + b_current))
new_b = b_current - (learningRate * b_intercept)
new_m = m_current - (learningRate * m_gradient)

return [new_b, new_m]



def gradient_descent_runner(points, starting_b, starting_m, learning_rate, num_iterations):
b = starting_b
m = starting_m
plt.ion()
for i in range(num_iterations):
b, m = step_gradient(b, m, array(points), learning_rate)
# plt.set_title('Linear Regression',color='black')
#plt.set_xlabel('This is the X axis',color='white')
#plt.set_ylabel('This is the Y axis',color='white')
#plt.clf()
plt.plot(x,m*x+b,'-')
plt.scatter(x, y, label='Points', color='k', s=20, marker='*')
plt.pause(0.2)
plt.draw()
plt.xlabel("Age")
plt.ylabel("height")
# plt.show()
print("New values for b and m: ")
return [b, m]




def run():
points = data
learning_rate = LRrtt
initial_b = initb #initial y-intercept guess
initial_m = initm # initial slope guess
num_iterations = iternum
print ("Starting gradient descent at b = {0}, m = {1}, error = {2}".format(initial_b, initial_m, compute_error_for_line_given_points(initial_b, initial_m, points)))
print ("Running...")
[b, m] = gradient_descent_runner(points, initial_b, initial_m, learning_rate, num_iterations)
print("After {0} iterations b = {1}, m = {2}, error = {3}".format(num_iterations, b, m, compute_error_for_line_given_points(b, m, points)))

Button_sub = Button(root,text="Start Analysis", command=run).grid(row=4, column=0, sticky=W, pady=4)
Button_exit = Button(root, text='Quit',command=quit).grid(row=5, column=0, sticky=W, pady=4)
root.mainloop()





if __name__ == '__main__':
run()

###/show plot
Jun 22 '16 #1
0 1163

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Phil Schmidt | last post by:
I'm trying to make a custom entry widget, as in the code that follows. There are two problems I'm trying to fix: 1) I would like the widget to behave as myEntry.Escape() does now, except that it...
5
by: Otto Krüse | last post by:
Hi everyone, I'm building a GUI in which I want, amongst other things, for people to fill in there postal code. The postal codes of my country (Holland) are in this format: 1234 AB So for the...
1
by: Michael Krasnyk | last post by:
Hi, How can I print variable values by their names? For example: import sys for str in dir(sys): print str Will be printed only variable names, but I need values from list of variable...
1
by: phil | last post by:
In a Tkinter entry field (or Pmw entry) how could I eat charactres? Say a certain char is keyed in. Say & I notice in the event handler for <key>. I don't want any more charactres to display or...
8
by: deko | last post by:
Is there a rule of thumb for quoting variable values? For example, do I have to put all string number values in quotes? strAbc = "3" ? What about long, int and byte? I assume these are NOT...
6
by: msigwald | last post by:
The following line of code works, however, since my professor is a real purist of c, I would like to know if this code is valid (is it good code or a piece of crap?): #define DMP_FILE argv ...
2
by: Martyn Quick | last post by:
If I create a Tkinter.Entry widget, I can adjust the background and the text colours, using the background and foreground options. However, if the state is "disabled", then this has no effect and...
2
by: Kevin Walzer | last post by:
I'm trying to construct a simple Tkinter GUI and I'm having trouble with getting the value of an entry widget and passing it onto a callback function. But I'm not grokking variable scope correctly....
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
0
by: Leonhard Vogt | last post by:
Hello I have the following problem in Python 2.5 on Windows XP. On Ubuntu I do not see the problem. I have a Tkinter application as in the following example The entry-widget is somehow...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.