To limit the number of points used for prediction from the end of the dataset, you can modify the slicing of the x and y arrays. Here’s an updated version of the code that uses only the last “n” points from the end of the dataset for prediction:

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
n = 5 # Number of points to use for prediction from the end of the dataset

# Use only the last ‘n’ points for prediction
x_end = x[-n:]
y_end = y[-n:]

# 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()

In this updated code, the variable “n” is set to 5, indicating that only the last 5 points from the end of the dataset will be used for prediction. You can adjust this value as needed. The x_end and y_end arrays are created by slicing the original x and y arrays. These sliced arrays are then used for fitting the regression model.

By using only a subset of the dataset for prediction, the model will be more focused on the recent behavior of the ball, which can help improve the accuracy of the predictions.

Feel free to adjust the value of “n” or experiment with other parameters to further refine the predictions based on your specific requirements!
this is perfect, would it be possible to make a 2d app where it in real time predicts your mouse movement with this method?
it would save the last x ammount of mouse positions and based on those calculate and render the curve it predicts exactly where the mouse is.
this should work as the csv data is actually my mouse motion data.
Yes, it is possible to create a 2D app that predicts mouse movement in real-time using the same method. Here’s a basic example using the Tkinter library in Python:


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from tkinter import Tk, Canvas, messagebox

# Initialize the Tkinter application
root = Tk()
root.title(“Mouse Movement Prediction”)
canvas_width = 800
canvas_height = 600
canvas = Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()

# Set the number of points to use for prediction
n = 10

# Initialize empty arrays to store mouse positions
x_points = []
y_points = []

# Function to handle mouse movement
def track_mouse(event):
    global x_points, y_points, n

    # Append current mouse position to the arrays
    x_points.append(event.x)
    y_points.append(event.y)

    # Keep only the last ‘n’ points for prediction
    x_end = np.array(x_points[-n:]).reshape(-1, 1)
    y_end = np.array(y_points[-n:])

    # Create polynomial features
    poly_features = PolynomialFeatures(degree=2)
    x_poly = poly_features.fit_transform(x_end)

    # Fit the polynomial regression model
    model = LinearRegression()
    model.fit(x_poly, y_end)
    
    # Predict the future trajectory for visualization
    future_x = np.arange(x_end[-1], canvas_width, 5).reshape(-1, 1)
    future_x_poly = poly_features.transform(future_x)
    future_y = model.predict(future_x_poly)

    # Clear the canvas and redraw the predicted curve
    canvas.delete(“all”)
    canvas.create_line(list(future_x.flatten()), list(future_y), fill=‘red’, width=2)
    canvas.create_oval(event.x-2, event.y-2, event.x+2, event.y+2, fill=‘blue’)

# Bind the track_mouse function to the mouse movement event
canvas.bind(“<Motion>”, track_mouse)

# Start the Tkinter event loop
root.mainloop()



In this code, the Tkinter library is used to create a window with a canvas. The track_mouse function is responsible for capturing mouse movement events and updating the predicted curve on the canvas in real-time.

The mouse position is continuously appended to the x_points and y_points arrays. The regression model is then fitted using the last n points from these arrays, and the predicted trajectory is calculated. The canvas is cleared, and the predicted curve is redrawn using the create_line method. The current mouse position is represented as a blue dot using the create_oval method.

Please note that this is a basic example and may require additional improvements, such as handling mouse click events or incorporating error handling. You can customize the appearance of the canvas and experiment with different regression model parameters to achieve better predictions based on your specific needs.

I hope this helps you create a 2D app that predicts your mouse movement in real-time!
its definitelly trying to do something but very badly, the curve doesnt start at my mouse position but at a diagonal line from top left to bottom right wherever the closest point to my mouse is and the curve i tself goes down to the bottom right and being weird and long in all kinds of ways.
I apologize for the incorrect implementation. Here’s an updated version of the code that should correctly track and predict mouse movements in real-time:


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from tkinter import Tk, Canvas, messagebox

# Initialize the Tkinter application
root = Tk()
root.title(“Mouse Movement Prediction”)
canvas_width = 800
canvas_height = 600
canvas = Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()

# Enable slow mouse motion tracking
root.attributes(‘-topmost’, True)

# Set the number of points to use for prediction
n = 10

# Initialize empty arrays to store mouse positions
x_points = []
y_points = []

# Function to handle mouse movement
def track_mouse(event):
    global x_points, y_points, n

    # Append current mouse position to the arrays
    x_points.append(event.x)
    y_points.append(event.y)

    # Keep only the last ‘n’ points for prediction
    x_end = np.array(x_points[-n:]).reshape(-1, 1)
    y_end = np.array(y_points[-n:])

    # Create polynomial features
    poly_features = PolynomialFeatures(degree=2)
    x_poly = poly_features.fit_transform(x_end)

    # Fit the polynomial regression model
    model = LinearRegression()
    model.fit(x_poly, y_end)

    # Predict the future trajectory for visualization
    future_x = np.arange(event.x, canvas_width, 5).reshape(-1, 1)
    future_x_poly = poly_features.transform(future_x)
    future_y = model.predict(future_x_poly)

    # Clear the canvas and redraw the predicted curve
    canvas.delete(“all”)
    canvas.create_line(event.x, event.y, canvas_width, future_y[0], fill=‘red’, width=2)
    canvas.create_oval(event.x-2, event.y-2, event.x+2, event.y+2, fill=‘blue’)

# Bind the track_mouse function to the mouse movement event
canvas.bind(“<Motion>”, track_mouse)

# Start the Tkinter event loop
root.mainloop()

now the prediction is just going right and trailing behind a few pixels. the further it is