You are on page 1of 1

import numpy as np

from sklearn.model_selection import train_test_split


from sklearn.linear_model import LinearRegression
from sklearn import metrics

# Create dummy data


np.random.seed(42)
X = np.random.rand(100, 1) * 10 # Independent variable
y = 3 * X.squeeze() + np.random.randn(100) * 2 # Dependent variable with some
random noise

# Prepare data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=0)

# Create and train the model


model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluation
mse = metrics.mean_squared_error(y_test, y_pred)
r2 = metrics.r2_score(y_test, y_pred)

# Check Variable Dependence


correlation_matrix = np.corrcoef(X.squeeze(), y, rowvar=False)

print(f"Mean Squared Error: {mse:.2f}")


print(f"R-squared: {r2:.2f}")
print(f"Correlation Coefficient:\n{correlation_matrix[0, 1]:.2f}"

You might also like