Untitled

mail@pastecode.io avatar
unknown
plain_text
8 days ago
1.3 kB
1
Indexable
Never
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# Generate a sequence of random numbers (pseudo-random)
np.random.seed(42)  # Seed for reproducibility
random_numbers = np.random.randint(0, 100, 100)

# Prepare data for model training (use previous number(s) to predict next number)
X = []
y = []

# Use the previous number to predict the next number
for i in range(1, len(random_numbers)):
    X.append([random_numbers[i - 1]])  # Feature: the previous number
    y.append(random_numbers[i])        # Target: the next number

X = np.array(X)
y = np.array(y)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
predictions = model.predict(X_test)

# Display results
print("Predicted values:", predictions)
print("True values:", y_test)

# Visualize the results
plt.scatter(X_test, y_test, color='blue', label='True values')
plt.scatter(X_test, predictions, color='red', label='Predictions')
plt.xlabel('Previous Number')
plt.ylabel('Next Number')
plt.title('Random Number Prediction')
plt.legend()
plt.show()
Leave a Comment