combine these two programs into one that uses the saved mouse positions and tries to…
- User
combine these two programs into one that uses the saved mouse positions and tries to predict the movement like in the first example and draw the curve and previous mouse positions on the window. predictor: import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression
Read the data from the CSV file
data = pd.read_csv("data.csv", header=None) x = data[0].values.reshape(-1, 1) y = data[1].values
Define the degree of the polynomial regression
degree = 2 data_amm = 10 # Number of points to use for prediction from the end of the dataset
Use only the last "n" points for prediction
x_end = x[-data_amm:] y_end = y[-data_amm:]
Create polynomial features
poly_features = PolynomialFeatures(degree=degree) x_poly = poly_features.fit_transform(x_end)
Fit the polynomial regression model
model = LinearRegression() model.fit(x_poly, y_end)
Extend the range of future_x for predictions
future_x = np.arange(x_end[-1], x_end[-1] + 100, 1).reshape(-1, 1) future_x_poly = poly_features.transform(future_x) future_y = model.predict(future_x_poly)
Plot the recorded coordinates and the predicted curve
plt.scatter(x, y, color="blue", label="Recorded Coordinates") plt.plot(future_x, future_y, color="red", label="Predicted Curve") plt.xlabel("X") plt.ylabel("Y") plt.legend() plt.grid(True) plt.show()
app: import tkinter as tk
Global variables
dots = [] max_dots = 100 # Maximum number of dots to keep
Function to save mouse position
def save_position(event): x = event.x y = event.y dots.append((x, y))
# Check if number of dots exceeds the limit if len(dots) > max_dots: dots.pop(0) # Remove oldest dot from memory draw_dots()Function to draw dots
def draw_dots(): canvas.delete("dot") for dot in dots: x, y = dot canvas.create_oval(x-2, y-2, x+2, y+2, fill="black", tags="dot")
Create tkinter app window
window = tk.Tk() window.title("Mouse Position Tracker")
Create canvas to track mouse movements
canvas = tk.Canvas(window, width=500, height=500) canvas.pack()
Bind mouse movements to save_position function
canvas.bind("", save_position)
Run tkinter main loop
window.mainloop()
- Assistant
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression import tkinter as tk
Global variables
dots = [] max_dots = 100 # Maximum number of dots to keep
Function to save mouse position
def save_position(event): x = event.x y = event.y dots.append((x, y))
# Check if number of dots exceeds the limit if len(dots) > max_dots: dots.pop(0) # Remove oldest dot from memory draw_dots() predict_and_plot()Function to draw dots
def draw_dots(): canvas.delete("dot") for dot in dots: x, y = dot canvas.create_oval(x-2, y-2, x+2, y+2, fill="black", tags="dot")
Function to predict and plot curve
def predict_and_plot(): if len(dots) > degree + 1: # Extract coordinates from dots x = np.array([dot[0] for dot in dots]).reshape(-1, 1) y = np.array([dot[1] for dot in dots])
# Define the degree of the polynomial regression degree = 2 # Use only the last "n" points for prediction x_end = x[-data_amm:] y_end = y[-data_amm:] # Create polynomial features poly_features = PolynomialFeatures(degree=degree) x_poly = poly_features.fit_transform(x_end) # Fit the polynomial regression model model = LinearRegression() model.fit(x_poly, y_end) # Extend the range of future_x for predictions future_x = np.arange(x_end[-1], x_end[-1] + 100, 1).reshape(-1, 1) future_x_poly = poly_features.transform(future_x) future_y = model.predict(future_x_poly) # Plot the recorded coordinates and the predicted curve plt.scatter(x, y, color="blue", label="Recorded Coordinates") plt.plot(future_x, future_y, color="red", label="Predicted Curve") plt.xlabel("X") plt.ylabel("Y") plt.legend() plt.grid(True) plt.show()Create tkinter app window
window = tk.Tk() window.title("Mouse Position Tracker")
Create canvas to track mouse movements
canvas = tk.Canvas(window, width=500, height=500) canvas.pack()
Bind mouse movements to save_position function
canvas.bind("", save_position)
Run tkinter main loop
window.mainloop()
- User
you forgot indentation
- Assistant
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression import tkinter as tk
Global variables
dots = [] max_dots = 100 # Maximum number of dots to keep
Function to save mouse position
def save_position(event): x = event.x y = event.y dots.append((x, y))
# Check if number of dots exceeds the limit if len(dots) > max_dots: dots.pop(0) # Remove oldest dot from memory draw_dots() predict_and_plot()Function to draw dots
def draw_dots(): canvas.delete("dot") for dot in dots: x, y = dot canvas.create_oval(x-2, y-2, x+2, y+2, fill="black", tags="dot")
Function to predict and plot curve
def predict_and_plot(): if len(dots) > degree + 1: # Extract coordinates from dots x = np.array([dot[0] for dot in dots]).reshape(-1, 1) y = np.array([dot[1] for dot in dots])
# Define the degree of the polynomial regression degree = 2 # Use only the last "n" points for prediction x_end = x[-data_amm:] y_end = y[-data_amm:] # Create polynomial features poly_features = PolynomialFeatures(degree=degree) x_poly = poly_features.fit_transform(x_end) # Fit the polynomial regression model model = LinearRegression() model.fit(x_poly, y_end) # Extend the range of future_x for predictions future_x = np.arange(x_end[-1], x_end[-1] + 100, 1).reshape(-1, 1) future_x_poly = poly_features.transform(future_x) future_y = model.predict(future_x_poly) # Plot the recorded coordinates and the predicted curve plt.scatter(x, y, color="blue", label="Recorded Coordinates") plt.plot(future_x, future_y, color="red", label="Predicted Curve") plt.xlabel("X") plt.ylabel("Y") plt.legend() plt.grid(True) plt.show()Create tkinter app window
window = tk.Tk() window.title("Mouse Position Tracker")
Create canvas to track mouse movements
canvas = tk.Canvas(window, width=500, height=500) canvas.pack()
Bind mouse movements to save_position function
canvas.bind("", save_position)
Run tkinter main loop
window.mainloop()