fork download
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.linear_model import LinearRegression
  6. from sklearn.metrics import mean_squared_error, r2_score
  7.  
  8. np.random.seed(0)
  9. n_samples = 100
  10. X = np.random.rand(n_samples, 3)
  11. y = 3 * X[:, 0] + 2 * X[:, 1] + 5 * X[:, 2] + np.random.randn(n_samples)
  12. data = pd.DataFrame({'Feature1': X[:, 0], 'Feature2': X[:, 1], 'Feature3': X[:, 2], 'Price': y})
  13.  
  14. X = data[['Feature1', 'Feature2', 'Feature3']]
  15. y = data['Price']
  16.  
  17. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  18.  
  19. model = LinearRegression()
  20. model.fit(X_train, y_train)
  21.  
  22. y_pred = model.predict(X_test)
  23.  
  24. mse = mean_squared_error(y_test, y_pred)
  25.  
  26. r2 = r2_score(y_test, y_pred)
  27.  
  28. print(f"Mean Squared Error (MSE): {mse}")
  29. print(f"R-squared (R2) Score: {r2}")
  30.  
  31. plt.scatter(y_test, y_pred)
  32. plt.xlabel("Actual Prices")
  33. plt.ylabel("Predicted Prices")
  34. plt.title("Actual Prices vs. Predicted Prices")
  35. plt.show()
  36.  
  37. coefficients = pd.DataFrame({'Feature': X.columns, 'Coefficient': model.coef_})
  38. print(coefficients)
  39.  
Success #stdin #stdout 2.96s 125248KB
stdin
Standard input is empty
stdout
Mean Squared Error (MSE): 1.0964652490522435
R-squared (R2) Score: 0.7406846582262431
    Feature  Coefficient
0  Feature1     2.868760
1  Feature2     1.760078
2  Feature3     5.087117