Create a Linear Regression Model in Python using a randomly created data set.

**Linear Regression Model**

Linear regression geeks for geeks

**Generating the Training Set**

`# python library to generate random numbers ` `from` `random ` `import` `randint ` ` ` `# the limit within which random numbers are generated ` `TRAIN_SET_LIMIT ` `=` `1000` ` ` `# to create exactly 100 data items ` `TRAIN_SET_COUNT ` `=` `100` ` ` `# list that contains input and corresponding output ` `TRAIN_INPUT ` `=` `list` `() ` `TRAIN_OUTPUT ` `=` `list` `() ` ` ` `# loop to create 100 data items with three columns each ` `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) ` ` ` `# creating the output for each data item ` ` ` `op ` `=` `a ` `+` `(` `2` `*` `b) ` `+` `(` `3` `*` `c) ` ` ` `TRAIN_INPUT.append([a, b, c]) ` ` ` `# adding each output to output list ` ` ` `TRAIN_OUTPUT.append(op) ` |

**Machine Learning Model – Linear Regression**

The Model can be created in two steps:-

1. **Training** the model with Training Data

2. **Testing** the model with Test Data

**Training the Model**

The data that was created using the above code is used to train the model

`# Sk-Learn contains the linear regression model ` `from` `sklearn.linear_model ` `import` `LinearRegression ` ` ` `# Initialize the linear regression model ` `predictor ` `=` `LinearRegression(n_jobs ` `=` `-` `1` `) ` ` ` `# Fill the Model with the Data ` `predictor.fit(X ` `=` `TRAIN_INPUT, y ` `=` `TRAIN_OUTPUT) ` |

**Testing the Data**

The testing is done Manually. Testing can be done using some random data and testing if the model gives the correct result for the input data.

`# Random Test data ` `X_TEST = [[ 10, 20, 30 ]] ` ` ` `# Predict the result of X_TEST which holds testing data ` `outcome = predictor.predict(X = X_TEST) ` ` ` `# Predict the coefficients ` `coefficients = predictor.coef_ ` ` ` `# Print the result obtained for the test data ` `print(` ```
'Outcome : {}
Coefficients : {}'
``` `.format(outcome, coefficients)) ` |

The Outcome of the above provided test-data should be, **10 + 20*2 + 30*3 = 140.**

**Output**

Outcome : [ 140.] Coefficients : [ 1. 2. 3.]

## leave a comment

## 0 Comments