| 1 |
# Import the necessary libraries |
| 2 |
from sklearn import datasets |
| 3 |
from sklearn.model_selection import train_test_split |
| 4 |
from sklearn.linear_model import LinearRegression |
| 5 |
from sklearn.metrics import mean_squared_error |
| 6 |
|
| 7 |
# Load a sample dataset (e.g., the Boston Housing dataset) |
| 8 |
data = datasets.load_boston() |
| 9 |
X, y = data.data, data.target |
| 10 |
|
| 11 |
# Split the dataset into training and testing sets |
| 12 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
| 13 |
|
| 14 |
# Create a linear regression model |
| 15 |
model = LinearRegression() |
| 16 |
|
| 17 |
# Train the model on the training data |
| 18 |
model.fit(X_train, y_train) |
| 19 |
|
| 20 |
# Make predictions on the test data |
| 21 |
y_pred = model.predict(X_test) |
| 22 |
|
| 23 |
# Calculate the mean squared error to evaluate the model |
| 24 |
mse = mean_squared_error(y_test, y_pred) |
| 25 |
print(f"Mean Squared Error: {mse}") |