Artificial Intelligence
Linear Regressions
A Regression is a method used to determine the relationship between one variable (usually called y) and other variables (usually called x).
In statistics, a Linear Regression is an approach to modeling a linear relationship between y and x.
Scatter Plot
This is the scatter plot (from the previous chapter):

Example
# Name the Axis
plt.title('House Prices vs Size')
plt.xlabel('Square Meters')
plt.ylabel('Price in Millions')
# Set x and y values
x = np.array([50,60,70,80,90,100,110,120,130,140,150,160])
y = np.array([7,8,8,9,9,9,9,10,11,14,14,15])
# Scatter Plot Data
plt.scatter(x, y)
plt.show()
Try it Yourself »
Predicting Values
From the scattered data above, how can we predict future prices?
- Use hand drawn linear graph
- Model a linear relationship
- Model a linear regression
Linear Graphs
This is a linear graph predicting prices based on the lowest and the highest price:

Example
# Set x and y values
x = np.array([50,60,70,80,90,100,110,120,130,140,150,160])
y = np.array([7,8,8,9,9,9,9,10,11,14,14,15])
# Plot Data
plt.scatter(x, y)
plt.plot([50, 160],[7, 15])
plt.show()
Try it Yourself »
Linear Relationships
This Model predicts prices using a linear relationship between price and size:

Example
# Set x and y values
x = np.array([50,60,70,80,90,100,110,120,130,140,150,160])
y = np.array([7,8,8,9,9,9,9,10,11,14,14,15])
slope = np.mean(y) / np.mean(x)
# Plot Data
plt.scatter(x, y)
plt.plot(x, slope * x)
plt.show()
Try it Yourself »
In the example above, the slope is a calulated average and the intercept = 0.
From Previous Chapter
A linear relationship is written as y = ax + b
Where:
- y is the price we want to predict
- a is the slope of the line
- x are the input values
- b is the intercept
Using a Linear Regression Function
This Model predicts prices using a linear regression function:

Example
# Name the Axis
plt.title('House Prices vs Size')
plt.xlabel('Square Meters')
plt.ylabel('Price in Millions')
# Set x and y values
x = np.array([50,60,70,80,90,100,110,120,130,140,150,160])
y = np.array([7,8,8,9,9,9,9,10,11,14,14,15])
# Call Linear Regression Function
slope, intercept, r, p, std_err = stats.linregress(x, y)
# Plot Data
plt.scatter(x, y)
plt.plot(x, slope * x + intercept)
plt.show()
Try it Yourself »
In the example above the slope and intercept is calculated by a function called linregress.