MACHINE
LEARNING
Simple Linear Regression
1
2
PREDICTION CLASSIFICATION
3
Linear Regression
4
Linear Regression
5
sales.csv
6
10 Simple Linear Regression.ipynb:
https://colab.research.google.com/drive/1c53Di79l9SvBN3RxChJ6fZWh36MWfxqR#scrollTo=3hmt6NUK1GQG
7
Loading the data file
# -*- coding: utf-8 -*-
"""
Simple Linear Regression
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('sales.csv')
print(data)
months = data[['Months']]
print(months)
sales = data[['Sales']]
print(sales)
sales2 = data.iloc[:,:1].values
print(sales2)
8
Seperating & Scaling the training and testing data
from sklearn.model_selection import train_test_split
x_train, x_test,y_train,y_test = train_test_split(months,sales,test_size=0.33, random_state=0)
from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
X_train = sc.fit_transform(x_train)
X_test = sc.fit_transform(x_test)
Y_train = sc.fit_transform(y_train)
Y_test = sc.fit_transform(y_test)
9
Building and Evaluating the Model for Linear Regression
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(x_train, y_train)
estimate = lr.predict(x_test)
10
Building and Evaluating the Model for Linear Regression
(without scaling)
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(months,sales,test_size=0.33, random_state=0)
from sklearn.linear_model import LinearRegression
lr=LinearRegression()
lr.fit(x_train,y_train)
estimate = lr.predict(x_test)
11
Linear Regression Result Visualization
# sorting is needed because train_test_split() method selects random data
x_train = x_train.sort_index()
y_train = y_train.sort_index()
plt.plot(x_train, y_train, 'k') # training output in black color
plt.plot(x_test, estimate, 'b') # estimate output in blue color
plt.title("Monthly Sales")
plt.xlabel("Months")
plt.ylabel("Sales")
12
13