import numpy as np import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression # Generate synthetic data np.random.seed(42) X = 2 * np.random.rand(100, 1) y = 4 + 3 * X + np.random.randn(100, 1) # 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 a linear regression model lin_reg = LinearRegression() # Fit the model to the training data lin_reg.fit(X_train, y_train) # Get the slope (coef_) and the intercept (intercept_) slope = lin_reg.coef_ intercept = lin_reg.intercept_ # Print the slope and intercept print("Slope (Coefficient): ", slope) print("Intercept: ", intercept) # Predict y values for the test set y_pred = lin_reg.predict(X_test) # Visualize the training data and the regression line plt.scatter(X_train, y_train, color='blue', label='Training Data') plt.scatter(X_test, y_test, color='green', label='Testing Data') plt.plot(X_test, y_pred, color='red', linewidth=2, label='Regression Line') plt.xlabel('X') plt.ylabel('y') plt.title('Linear Regression Demonstration') plt.legend() plt.show()