AD1

Saturday, 20 January 2018

Implementing Linear Regression Algorithm in Python


in this PDF created by me , a short and good introduction into one of the most popular machine learning algorithms it is called Linear regression
in this tutorial we deal with linear regression with single variable
check attached file

Linear Regression.PDF 
import matplotlib.pyplot as plt
import pandas as pnds
import numpy as np
# First step we may consider while implementing Machine Learning algorithm is to visualize our data set
# reading data from csv file
dataset=pnds.read_csv('TV.csv')
# read_csv() return pandas data frame
# Data Frame in pandas can be considered for simplicity a two dimensional array (matrix) composed from rows and columns
# then we may print all data set items to check that every thing is ok
# output will be in matrix from
print(dataset.iloc[:,:])
# next we will scatter plot money spent on TV advertising vs sales
dataset.plot(kind='scatter',x='TV',y='sales')
l1=pnds.Series.tolist(dataset)
# Define Our main variables
x=np.zeros((200,1))
Y=np.zeros((200,1))
X=np.zeros((200,2))
i=0
for item in l1:
x[i,0]=item[0]
Y[i,0]=item[1]
i=i+1
X=np.concatenate((np.ones((200,1)),x),axis=1)
theta=np.linalg.lstsq(X,Y)[0] # solving the system of linear equation using least square method
print(theta)
plt.plot(x,np.dot(X,theta),color="red")
plt.show()
view raw linear_reg.py hosted with ❤ by GitHub