Skip to content

Commit 6faa23a

Browse files
author
garrysingh2930
committed
Added regression work using sklearn lib
1 parent ad08d9f commit 6faa23a

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Regression ML SkLearn Python script
4+
Author: Garry Singh
5+
6+
This script covers regression computations in python using Scikit Learn library
7+
8+
"""
9+
10+
# Importing necessary libraries
11+
import numpy as np
12+
from sklearn.linear_model import SGDRegressor # lib for computing gradient descent
13+
from sklearn.preprocessing import StandardScaler # lib for computing z score normalized features (feature scaling)
14+
import matplotlib.pyplot as plt
15+
16+
# let's prepare sample input data
17+
18+
# Create a NumPy array with feature names as strings
19+
x_features = np.array(["Square Feet", "Bedrooms", "Bathrooms", "Neighborhood", "Garage"])
20+
21+
# Sample input data as a 2D NumPy array
22+
x_train = np.array([
23+
[1500, 3, 2, 1, 1],
24+
[1800, 4, 2.5, 2, 0],
25+
[1200, 2, 1, 0, 0],
26+
[2100, 4, 3, 1, 1],
27+
[1600, 3, 2.5, 2, 1],
28+
[1400, 2, 1.5, 1, 0],
29+
[2500, 4, 3.5, 2, 1]
30+
])
31+
32+
# Corresponding prices as a 1D NumPy array
33+
y_train = np.array([250000, 320000, 180000, 400000, 300000, 220000, 450000])
34+
35+
# let's normalize the given input training data
36+
scalar = StandardScaler()
37+
x_norm = scalar.fit_transform(x_train)
38+
39+
#print(f"Normalized housing feature data is: \n {x_norm}")
40+
41+
# let's try to fit the normalized feature data with given price training values
42+
sgdr = SGDRegressor(max_iter=100000)
43+
sgdr.fit(x_norm, y_train)
44+
45+
n_iter_ran = sgdr.n_iter_
46+
n_weight_updates = sgdr.t_
47+
b_norm = sgdr.intercept_
48+
w_norm = sgdr.coef_
49+
50+
#print(f"Number of iterations used: {n_iter_ran}")
51+
#print(f"Number of updates completed to weight: {n_weight_updates}")
52+
53+
#print(f"Normalized bias is: {b_norm} ")
54+
#print(f"Normalized weight is: {w_norm} ")
55+
56+
# now let's try to predict price for given denormalized data
57+
58+
y_pred_sgd = sgdr.predict(x_norm)
59+
y_pred_dot = np.dot(x_norm, w_norm) + b_norm
60+
61+
for i in range(x_train.shape[1]):
62+
plt.scatter(x_train[:,i], y_train,marker='o', color='g')
63+
plt.ylabel("House price in $")
64+
plt.xlabel(x_features[i])
65+
plt.show()
File renamed without changes.

0 commit comments

Comments
 (0)