Skip to content
Menu
The Renaissance Raven
  • Game Development
    • Project 1 – ???
    • Unity Tutorials
  • Electronics and Programming
  • Leisure
  • Blog
The Renaissance Raven

Linear Regression – You should always try it first!

You should always try to use linear regression first when you are faced with an unknown data source for regression analysis, the main and simple reason behind this is:

Linear Regression creates a stable and accurate model, unlikely to fail miserably with erratic data. 

If you are able to model the data correctly to fit a linear model, it is very likely that you have derived the true equation to solve the problem. Lets see an example with a very basic example with a very linear equation:

Y = x1+x2

from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

# generating data
x1 = np.random.randint(100,size=100)
x2 = np.random.randint(100,size=100)
X = np.stack((x1,x2),axis=1)

# purely linear equation
y = x1+x2

# splitting data
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.8)

# linear regression
linearRegressor = LinearRegression()
linearRegressor.fit(X_train,y_train)
y_pred_linear = linearRegressor.predict(X_test)
print(linearRegressor.intercept_) # prints near zero
print(linearRegressor.coef_) # prints [1,1]

# random forest regression
randomForestRegressor = RandomForestRegressor(max_depth=100,n_estimators=100)
randomForestRegressor.fit(X_train,y_train)
y_pred_randomForest = randomForestRegressor.predict(X_test)

# plotting
plt.scatter(y_test,y_pred_linear,label='Linear')
plt.scatter(y_test,y_pred_randomForest,label='Random Forest')
plt.plot([np.min(y_test),np.max(y_test)],[np.min(y_test),np.max(y_test)],'k-',label='n=1')
plt.legend()
plt.show()

The results are shown below for degree of fit, additionally from printing the linear regression intercept and slope we can see that it derived the following equation:

Y = 1*x1+1*x2+0

 

Which is spot on.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Project Hasseu – Tutorial 2 – Programming Sentry Turrets
  • Project Hasseu – Tutorial 1 – Programming a machine gun / projectile launcher
  • Project 1 – Update 3
  • Unity – Adding text above game objects without canvas
  • Book Review – A Theory of Fun for Game Design by Raph Koster.

Recent Comments

    Archives

    • February 2021
    • January 2021
    • September 2020
    • August 2020
    • March 2020
    • February 2020
    • April 2017
    • March 2017
    • August 2015
    • July 2015
    • June 2015
    • March 2015
    • February 2015

    Categories

    • Reading
    • Uncategorized

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    ©2026 The Renaissance Raven | Powered by WordPress and Superb Themes!