|
| 1 | +#1. Load the Dataset: |
| 2 | + |
| 3 | +from sklearn.datasets import load_boston |
| 4 | +boston = load_boston() |
| 5 | + |
| 6 | +#2. Prepare the Data: |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | + |
| 10 | +boston_df = pd.DataFrame(boston.data, columns=boston.feature_names) |
| 11 | +boston_df['PRICE'] = boston.target |
| 12 | + |
| 13 | +X = boston_df.drop('PRICE', axis=1) |
| 14 | +y = boston_df['PRICE'] |
| 15 | + |
| 16 | +#3. Split the Data: |
| 17 | + |
| 18 | +from sklearn.model_selection import train_test_split |
| 19 | + |
| 20 | +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
| 21 | + |
| 22 | +#4. Train the Model: |
| 23 | + |
| 24 | +from sklearn.linear_model import LinearRegression |
| 25 | + |
| 26 | +model = LinearRegression() |
| 27 | +model.fit(X_train, y_train) |
| 28 | + |
| 29 | +#5. Evaluate the Model: |
| 30 | + |
| 31 | +train_score = model.score(X_train, y_train) |
| 32 | +print(f'Training Score: {train_score}') |
| 33 | + |
| 34 | +test_score = model.score(X_test, y_test) |
| 35 | +print(f'Testing Score: {test_score}') |
| 36 | + |
| 37 | +#6. Plot Residuals: |
| 38 | + |
| 39 | +import matplotlib.pyplot as plt |
| 40 | + |
| 41 | +train_residuals = y_train - model.predict(X_train) |
| 42 | +test_residuals = y_test - model.predict(X_test) |
| 43 | + |
| 44 | +plt.figure(figsize=(10, 5)) |
| 45 | +plt.scatter(model.predict(X_train), train_residuals, label='Train Residuals', alpha=0.5) |
| 46 | +plt.scatter(model.predict(X_test), test_residuals, label='Test Residuals', alpha=0.5) |
| 47 | +plt.axhline(y=0, color='r', linestyle='--') |
| 48 | +plt.xlabel('Predicted Values') |
| 49 | +plt.ylabel('Residuals') |
| 50 | +plt.title('Residual Plot') |
| 51 | +plt.legend() |
| 52 | +plt.show() |
0 commit comments