fork download
  1. import numpy as np
  2. from sklearn.linear_model import LinearRegression
  3.  
  4. # Sample data (replace with your actual data)
  5. data = np.array([2.09, 3.60, 1.16, 1.54,1.00, 37.01, 5.09, 10.62, 2.41, 1.46, 1.07, 3.08])
  6.  
  7. # Number of future predictions you want to make
  8. num_predictions = 5
  9.  
  10. # Function to predict the next number and add it to the array
  11. def predict_and_add(data, model, num_predictions):
  12. for _ in range(num_predictions):
  13. X = np.arange(len(data)).reshape(-1, 1)
  14. y = data
  15.  
  16. # Train the model
  17. model.fit(X, y)
  18.  
  19. # Predict the next number
  20. next_index = len(data)
  21. next_value = model.predict([[next_index]])
  22.  
  23. # Add the predicted number to the array
  24. data = np.append(data, next_value)
  25.  
  26. # Print the predicted number with two decimal places
  27. print(f"The predicted next number is: {next_value[0]:.2f}")
  28.  
  29. return data
  30.  
  31. # Create the linear regression model
  32. model = LinearRegression()
  33.  
  34. # Predict and add the future numbers
  35. updated_data = predict_and_add(data, model, num_predictions)
  36.  
  37. print("Updated data array:", updated_data)
Success #stdin #stdout 3.28s 103420KB
stdin
Standard input is empty
stdout
The predicted next number is: 5.65
The predicted next number is: 5.62
The predicted next number is: 5.59
The predicted next number is: 5.56
The predicted next number is: 5.53
Updated data array: [ 2.09        3.6         1.16        1.54        1.         37.01
  5.09       10.62        2.41        1.46        1.07        3.08
  5.65121212  5.62152681  5.59184149  5.56215618  5.53247086]