Categories

# Predicting per capita income of the US using linear regression in Python

Python enables us to predict and analyze any given data using Linear regression. Linear Regression is one of the basic machine learning or statistical techniques created to solve complex problems.

In Machine Learning or in Data Science regression is known to be one of the most crucial fields and there’re many regression methods available today. Linear Regression is one of them. whereas, regression is used to find the relationship among the variables.

Using the current data along with the income and year, we can predict the future income of any year using linear regression.

I’ll be using the scikit-learn library to implement linear regression.

##### Import the relevant packages

Import package `numpy` , `matplotlib` for charts, `pandas` for reading CSV files and the class `LinearRegression` from `sklearn.linear_model` to implement linear regression.

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
```Code language: JavaScript (javascript)```

You can download Canada Per Capita Income from World Bank or from other data sources. I have gathered the data from the World Bank and it’s included in the CSV file below.

``````csv = pd.read_csv("gdp-per-capita-us.csv")
```Code language: JavaScript (javascript)```
##### Display using Scatter Chart

Display the CSV file in a scatter chart with the help of MatplotLib Library.

``````plt.scatter(csv.year, csv.income, marker="*",color="green")
plt.plot(csv.year,csv.income, color="yellow")
plt.xlabel("Year")
plt.ylabel("Income")
plt.show()
```Code language: JavaScript (javascript)```
##### Run Linear Regression

Create the linear regression model and predict the income of the year 2020.

``````l_r = linear_model.LinearRegression()
l_r.fit(csv[['year']],csv.income)
l_r.predict([])
```Code language: JavaScript (javascript)```

You can also use the equation of the straight line to predict the income of 2020 manually as shown below,

``````l_r.coef_
l_r.intercept_
y= l_r.coef_*2020 + l_r.intercept_
print("Predicted Income of 2020 is %d" %y)
```Code language: PHP (php)```

So the final program will look like this,

``````import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model

plt.scatter(csv.year, csv.income, marker="*",color="green")
plt.plot(csv.year,csv.income, color="yellow")
plt.xlabel("Year")
plt.ylabel("Income")
plt.show()

l_r = linear_model.LinearRegression()
l_r.fit(csv[['year']],csv.income)
l_r.predict([])

# To predict Manually using the equation of the straight line

l_r.coef_
l_r.intercept_
y= l_r.coef_*2020 + l_r.intercept_
print("Predicted Income of 2020 is %d" %y)
```Code language: PHP (php)```

Explore Categories