19
loading...
This website collects cookies to deliver better user experience
Mobile Phone Age (Years) | Price ($) |
---|---|
1 | 250 |
2 | 230 |
5 | 190 |
6 | 160 |
8 | 120 |
9 | 90 |
10 | 70 |
11 | 40 |
y = mx + b
. I know you're a little bit familiar with this formula. because mostly we all learned this in school.y
- What we are going to predict. In this case, mobile phone price (dependent variable)m
- Slope or constantx
- Input as 7 years (independent variable)b
- Interceptm
and b
are given by the following formula.x (Years) | y (Price) | x2 | xy |
---|---|---|---|
1 | 250 | 1 | 250 |
2 | 230 | 4 | 460 |
5 | 190 | 25 | 950 |
6 | 160 | 36 | 960 |
8 | 120 | 64 | 960 |
9 | 90 | 81 | 810 |
10 | 70 | 100 | 700 |
11 | 40 | 121 | 440 |
(∑x) = 52 | (∑y) = 1,150 | (∑x2) = 432 | (∑xy) = 5,530 |
m
and b
.y = mx + b
. The y
is the price of the mobile phone after 7 years (that we're going to predict). x
is 7 years.Jupyter Notebook
. You can use any Python IDE as you prefer. Next, Install the libraries that we need. (If you are using Jupyter Notebook
, add an exclamation mark before the command to act as if it is executed in the terminal)!pip install scikit-learn
!pip install numpy
!pip install pandas
!pip install matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
mobiledata.csv
file using pandas
data_set = pd.read_csv('mobiledata.csv')
plt.scatter(data.age, data.price, color='red')
plt.xlabel('Mobile phone Age')
plt.ylabel('Price')
x
and price values as y
. We need to convert those values to a numpy
array.x = np.array(data.age.values)
y = np.array(data.price.values)
fit
function. Also, the model.fit
function allows a two-dimensional array to x
position.model = LinearRegression()
model.fit(x.reshape((-1,1)), y)
# x.reshape((-1,1) is convert numpy array to two dimensional array
m
(Slope) and b
(Intercept).plt.scatter(data.age, data.price, color='red')
plt.xlabel('Mobile phone Age')
plt.ylabel('Price')
m,b = np.polyfit(x,y,1)
plt.plot(x,m*x+b)
model
. Predict the price to see whether it's equal to the previously calculated value or not. To do that, We need to convert x value (7) to a numpy array and two-dimensional array.year_seven = np.array([7]).reshape((-1,1))
# Predict the price
model.predict(year_seven)
# array([133.40425532])
m
and b
by executing the variable in the notebook.m
# -20.691489361702125
b
# 278.2446808510638