Machine Learning Algorithm- Linear Regression| Simple

There are two parts of machine learning-

  1. Supervised Learning: The output will consider from the given input. The Supervised learning process start from-

Trained the data on the basis of past output -> Classify the trained data –> Apply Algorithm -> Predicted the data using an algorithm

Some important process happens in Supervised Learning- Input & Output available.

2. Unsupervised Learning: Unsupervised learning does not use any data for taking training.

  • It includes only inputs
  • Classify the data inputs
  • Behalf of input collection, Clustering the data

Example of Unsupervised Learning : K-Means Algorithm

Machine Learning used some algorithm for prediction

  • Linear Regression
  • Logistic Regression
  • Decision Tree
  • SVM
  • Naive Bayes
  • KNN
  • K-Means
  • Random Forest

Linear Regression :

Code :

#For Mathematical calculation
import numpy as np

#For handling datasets
import pandas as pd


#For plotting graphs
from matplotlib import pyplot as plt

#import the sklearn library for linear regression
from sklearn.linear_model import LinearRegression

#import the csv file from dataset, which is located in Drive C path has been given 
df=pd.read_csv('C:\ClassStudies_Python\data.csv',delim_whitespace=True)

#prints the top 5 rows
print(df.head())
#print(df['Father'].values)

#prepare the traning set
x_train=df['Father'].values[:,np.newaxis]  #newaxis sperate values
y_train=df['Son'].values
#x_train=[[65.0],[63.3],[ 65.0],[ 65.8],[ 61.1]]
#y_train=[[59.8],[63.2],[ 63.3],[ 62.8],[ 64.3]]

#print(x_train)
lm=LinearRegression()     #lm is object of linearRegression


#Train the Model
lm.fit(x_train,y_train)  # Fit() is fubction (independent varable x,y)

#Prepare the test data
x_test=[[72.8],[61.1],[67.4],[70.2],[75.6],[60.2],[59.2]]       #new value to find out son height if father's height given

#Test the model
predictions=lm.predict(x_test)

print(predictions)

#plot the traning data
plt.scatter(x_train,y_train,color='b')

#plot the best fit line using predicted values
plt.plot(x_test,predictions,color='black',linewidth=3)
plt.xlabel("Father height in inches")
plt.ylabel('Son height in inches')
plt.show()

Output :

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top