fork download
  1. # Required libraries
  2. import pandas as pd
  3. import numpy as np
  4. from sklearn.datasets import load_boston
  5. from sklearn.model_selection import train_test_split
  6. import matplotlib.pyplot as plt
  7. boston = load_boston()
  8. print(boston.data.shape)
  9. print(boston.feature_names)
  10. print(boston.target.shape)
  11. # Creating Pandas dataframe
  12. bos = pd.DataFrame(boston.data)
  13. print(bos.head())
  14. bos['PRICE'] = boston.target
  15. X = bos.drop('PRICE', axis = 1)
  16. Y = bos['PRICE']
  17. Y
  18. # Test and train are two random subsets.Split data into these two subsets.
  19. X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.33, random_state = 5)
  20. print(X_train.shape)
  21. print(X_test.shape)
  22. print(Y_train.shape)
  23. print(Y_test.shape)
  24. # systamization
  25. from sklearn.preprocessing import StandardScaler
  26. std = StandardScaler()
  27. X_train = std.fit_transform(X_train)
  28. X_test = std.fit_transform(X_test)
  29. from sklearn.linear_model import SGDRegressor
  30. from sklearn.metrics import mean_squared_error, r2_score
  31. clf = SGDRegressor()
  32. clf.fit(X_train, Y_train)
  33. Y_pred = clf.predict(X_test)
  34. print("Coefficients: \n", clf.coef_)
  35. print("Y_intercept", clf.intercept_)
Success #stdin #stdout 1.35s 105488KB
stdin
Standard input is empty
stdout
(506, 13)
['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO'
 'B' 'LSTAT']
(506,)
        0     1     2    3      4   ...   8      9     10      11    12
0  0.00632  18.0  2.31  0.0  0.538  ...  1.0  296.0  15.3  396.90  4.98
1  0.02731   0.0  7.07  0.0  0.469  ...  2.0  242.0  17.8  396.90  9.14
2  0.02729   0.0  7.07  0.0  0.469  ...  2.0  242.0  17.8  392.83  4.03
3  0.03237   0.0  2.18  0.0  0.458  ...  3.0  222.0  18.7  394.63  2.94
4  0.06905   0.0  2.18  0.0  0.458  ...  3.0  222.0  18.7  396.90  5.33

[5 rows x 13 columns]
(339, 13)
(167, 13)
(339,)
(167,)
Coefficients: 
 [-1.20826388  0.71054081 -0.48592306  0.25711637 -1.23634687  2.92020304
 -0.41924555 -2.63070839  1.76392467 -1.0457381  -2.0901674   1.02941922
 -3.29717613]
Y_intercept [22.53076708]