Before we create a model of Linear Regression, we need to import the libraries and data to the python correctly.
The method is quite straight forward. Here are the examples.
Import the Libraries for Linear Regression
#Import Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas
Numpy is library for scientific calculation.
Pandas is for data handling.
Matplotlib is for creating graph and visualizing data.
Import the Data for Linear Regression
#Import data
dataset = pd.read_csv(‘Data.csv’)
x = dataset.iloc[:,:-1].values
y =dataset.iloc[:,1].values
For example, this is the data in ‘Data.csv’
Column Index 0Years of Work |
Column Index 1Account Balance |
2.07 | 48,593.80 |
2.03 | 55,577.37 |
1.51 | 45,614.93 |
2.46 | 44,312.45 |
2.97 | 49,508.73 |
3.80 | 64,651.30 |
3.64 | 60,831.57 |
4.19 | 60,560.02 |
4.03 | 71,884.91 |
4.36 | 57,765.77 |
4.53 | 69,147.45 |
4.69 | 57,252.44 |
4.51 | 62,517.28 |
4.21 | 64,194.70 |
4.61 | 66,854.51 |
5.63 | 71,015.50 |
5.58 | 68,822.94 |
5.59 | 87,566.82 |
6.63 | 82,687.99 |
6.18 | 102,363.37 |
7.28 | 100,435.79 |
7.45 | 106,208.10 |
8.71 | 103,234.62 |
8.20 | 121,890.09 |
9.34 | 117,426.12 |
9.79 | 112,320.65 |
10.45 | 117,584.00 |
9.83 | 118,848.98 |
10.63 | 130,468.08 |
10.55 | 126,436.68 |
7.11 | 101,400.67 |
4.22 | 56,465.77 |
5.60 | 67,832.87 |
7.48 | 107,209.80 |
The independent variable X would be the column on the Left , ‘Years of Work’. Column Index 0
We use iloc to set x equals to all the rows and all the columns minus the one on the most right hand side.
x = dataset.iloc[:,:-1].values
The dependent variable Y would be the column on the right “Account Balance”. Column Index 1
We use ilock to set y equals to all the rows in column index 1.
y =dataset.iloc[:,1].values
Other Sections on Linear Regression :
Step 1.) Import Libraries and Import Dataset
Step 2.) Split Dataset into Training Set and Testing Set
Step 3.) Training the Simple Linear Regression Model
Learn Other Topics on Regression :
Leave a Reply