techTreksBooks

Regression Algorithms

Source: NESA Software Engineering Course Specifications (page 28).

Linear regression and polynomial regression algorithms are used to predict values in a continuous range, such as integers. These regression algorithms are used for machine learning. Logistic regression is used for classification problems.

Students should know how to design programs which use and apply these algorithms but are not expected to implement (or code) these complex algorithms. The following Python code represents linear regression using NumPy and Scikit-learn machine learning frameworks.

# Import frameworks import numpy

from sklearn.linear_model import LinearRegression

# Create the data for the two features

x = np.array([[2], [4], [6], [8], [10], [12], [14], [16]])
y = np.array([1, 3, 5, 7, 9, 11, 13, 15])

# Create the model

model = LinearRegression()

# Fit the model to the data (that is determine the line of best fit through the data) model.fit(x, y)
# We can now use the model for predictions with existing or new data
# The model expects a value for x and will predict a value for y – so if we asked for a prediction on 4 it would return 3 (as that is known data).

y_prediction = model.predict(4)
>> 3

# If we asked for a prediction of 4.5 it should return 3.5 (even though that is unknown data we can tell what it should return, given there is a linear relationship)

y_prediction = model.predict(4.5)
>> 3.5