Odometer Prediction

Uli2000 2 weeks ago 0

I have had a small python script written that predicts my mileage in the future using linear regression. This was important to me because of the warranty dates and the annual mileage for the insurance. As a data basis I imported a csv file from Teslafi with all the data from the past. Perhaps you could offer this as a feature on your site.

import pandas as pd
from datetime import datetime
from sklearn.linear_model import LinearRegression
import numpy as np

# Load CSV file
file_path = "mileages.csv" # Please replace this with the actual file path
df = pd.read_csv(file_path, sep=";", header=None, names=["datetime", "kilometers"])

# Convert the date and time column to DateTime data type
df["datetime"] = pd.to_datetime(df["datetime"])

# Convert the kilometers column values to float data type
df["kilometers"] = df["kilometers"].str.replace(",", ".").astype(float)

# Function to predict kilometers
def predict_kilometers(date):
# Linear Regression
X = df["datetime"].apply(lambda x: x.timestamp()).values.reshape(-1, 1)
y = df["kilometers"].values
model = LinearRegression().fit(X, y)

# Prediction for the specified date
timestamp = datetime.strptime(date, "%d.%m.%Y").timestamp()
predicted_kilometers = model.predict([[timestamp]])
return predicted_kilometers[0]

# User input for the prediction date
input_date = input("Please enter the prediction date in the format 'DD.MM.YYYY': ")

# Prediction of kilometers for the entered date
predicted_kilometers = predict_kilometers(input_date)
print(f"Predicted kilometers on {input_date}: {predicted_kilometers:.2f} km")

best regards