Act as a spelling corrector, content editor, and text improver. ONLY return the rewritten text and nothing else.

Strictly follow these rules:
- Correct spelling, grammar, and punctuation errors in the given text
- Enhance clarity and conciseness without altering the original meaning
- Use the same language as the original text
- Divide lengthy sentences into shorter, more readable ones
- Eliminate unnecessary repetition while preserving important points
- Prioritize active voice over passive voice for a more engaging tone
- Opt for simpler, more accessible vocabulary when possible
- ALWAYS ensure the original meaning and intention of the given text
- ALWAYS maintain the original language of the text
- ALWAYS maintain the existing tone of voice and style, e.g. formal, casual, polite, etc.
- NEVER surround the improved text with quotes or any additional formatting
- If the text is already well-written and requires no improvement, do not change the given text

Text to improve:



from darts import TimeSeries
from darts.models import KalmanForecaster
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

# Define the train-test split ratio and batch size
train_ratio = 0.95
batch_size = 1

# Sample data for demonstration purposes
filtered_refgps = filtered_refgps  # don't change this, already have one

# Determine the number of training samples
num_samples = len(filtered_refgps)
train_size = int(num_samples * train_ratio)

# Create a date range for the entire dataset
date_range = pd.date_range(start='2023-01-01', periods=num_samples, freq='D')

# Create a DataFrame with the date range and the filtered_refgps data
df = pd.DataFrame({'value': filtered_refgps}, index=date_range)

# Split the data into train and test sets
train_df = df.iloc[:train_size]
test_df = df.iloc[train_size:]

# Convert to TimeSeries with explicit frequency handling
train_series = TimeSeries.from_dataframe(train_df, freq='D', fill_missing_dates=True)

# Initialize KalmanForecaster
model = KalmanForecaster(dim_x=batch_size)

# Initial training
model.fit(train_series)

preds = []

def update_model_with_new_data(model, train_series, new_data):
    pred = model.predict(len(new_data))
    preds.extend(pred)

    new_data_series = TimeSeries.from_dataframe(new_data, freq='D', fill_missing_dates=True)
    updated_series = train_series.append(new_data_series)
    return updated_series

# Process test data in batches
for i in range(0, len(test_df), batch_size):
    batch = test_df.iloc[i:i+batch_size]
    print(f"Processing batch {i//batch_size + 1}/{len(test_df)//batch_size + 1}")
    
    train_series = update_model_with_new_data(model, train_series, batch)
    model.fit(train_series)

flattened_preds = np.array([pred.values()[0][0] for pred in preds])

# Trim predictions to match test data length
flattened_preds = flattened_preds[:len(test_df)]

mse = mean_squared_error(test_df['value'], flattened_preds)
rmse = np.sqrt(mse)
mae = mean_absolute_error(test_df['value'], flattened_preds)
r2 = r2_score(test_df['value'], flattened_preds)

print(f'MSE: {mse}, RMSE: {rmse}, MAE: {mae}, R2: {r2}')

plt.plot(test_df['value'].values, label='Actual')
plt.plot(flattened_preds, label='Predicted')
plt.legend()
plt.show()