Skip to content

Update LinearRegression.py #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions LinearRegression.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
# Import the libraries
from random import randint
from sklearn.linear_model import LinearRegression
import numpy as np

# Create a range limit for random numbers in the training set, and a count of the number of rows in the training set
TRAIN_SET_LIMIT = 1000
TRAIN_SET_COUNT = 100


# Create an empty list of the input training set 'X' and create an empty list of the output for each training set 'Y'
TRAIN_INPUT = list()
TRAIN_OUTPUT= list()
# Create an empty numpy array of shape (TRAIN_SET_COUNT, 3) for the input training set 'X' and create an empty numpy array for the output for each training set 'y'
TRAIN_INPUT = np.empty((TRAIN_SET_COUNT, 3))
TRAIN_OUTPUT = np.empty(TRAIN_SET_COUNT)

#Create and append a randomly generated data set to the input and output
for i in range(TRAIN_SET_COUNT):
a = randint(0, TRAIN_SET_LIMIT)
b = randint(0, TRAIN_SET_LIMIT)
c = randint(0, TRAIN_SET_LIMIT)
#Create a linear function for the output dataset 'Y'
a, b, c = randint(0, TRAIN_SET_LIMIT), randint(0, TRAIN_SET_LIMIT), randint(0, TRAIN_SET_LIMIT)
#Create a linear function for the output dataset 'y'
op = (10*a) + (2*b) + (3*c)
TRAIN_INPUT.append([a,b,c])
TRAIN_OUTPUT.append(op)
TRAIN_INPUT[i] = [a, b, c]
TRAIN_OUTPUT[i] = op

predictor = LinearRegression(n_jobs=-1) #Create a linear regression object NOTE n_jobs = the number of jobs to use for computation, -1 means use all processors
predictor = LinearRegression(n_jobs=-1) #Create a linear regression object with all processors
predictor.fit(X=TRAIN_INPUT, y=TRAIN_OUTPUT) #fit the linear model (approximate a target function)

X_TEST = [[10,20,30]] #Create our testing data set, the ouput should be 10*10 + 2*20 + 3*30 = 230
outcome = predictor.predict(X=X_TEST) # Predict the ouput of the test data using the linear model
X_TEST = np.array([[10, 20, 30]]) #Create our testing data set, the ouput should be 10*10 + 2*20 + 3*30 = 230
outcome = predictor.predict(X=X_TEST) # Predict the output of the test data using the linear model

coefficients = predictor.coef_ #The estimated coefficients for the linear regression problem.

print('Outcome: {} \n Coefficients: {}'.format(outcome, coefficients))
print(f"Outcome: {outcome} \nCoefficients: {coefficients}")