Machine Learning News Hubb
Advertisement Banner
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us
Machine Learning News Hubb
No Result
View All Result
Home Machine Learning

Simple Linear Regression Menggunakan Python | by Echo Simanjuntak | Sep, 2023

admin by admin
September 12, 2023
in Machine Learning


Berikut merupakan cara memvisualisasikan data linear regression serta menghitung konstanta dan koefisien juga melakukan prediksi pada variable terikat menggunakan bahasa pemrograman python.

Tutorial ini merupakan lanjutan dari tutorial sebelumnya yang membahas cara melakukan perhitungan pada simple linear regression secara manual, bagi anda yang belum membaca, silahkan dibaca terlebih dahulu pada link berikut ini:

Kita mulai pemrograman dengan melakukan import module yang dibutuhkan seperti pandas untuk visualisasi dataframe, matplotlib dan seaborn untuk visualisasi grafik.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Kemudian data yang ada kita assign ke dalam list variable dan selanjutnya di assign juga ke dalam dictionary untuk di import ke pandas dataframe.

# Create label and data variable 
cellphonelabel = ['Name','Camera','Price']
cellphonename = ['H1','H2','H3','H4','H5','H6','H7','H8']
cellphonecamera = [8,2,6,9,4,5,7,3]
cellphoneprice = [7,3,7,None,2,None,8,3]

# Create data dictionary
D = {cellphonelabel[0]:cellphonename, cellphonelabel[1]:cellphonecamera, cellphonelabel[2]:cellphoneprice}

# Import data dictionary to pandas dataframe
df = pd.DataFrame(data=D)

df

Dan selanjutnya kita akan mencari Σy, (Σx²), Σx dan Σxy. Rumusnya dapat dilihat pada artikel/tutorial sebelumnya.

# Find Σy
sigmaY = sum(filter(lambda y: isinstance(y,int),cellphoneprice))

# Find (Σx²), Σx and Σxy
sigmaXsquare = 0
sigmaX = 0
sigmaXY = 0
dataLength = 0

for index, val in enumerate(cellphonecamera):
if isinstance(cellphoneprice[index],int):
sigmaXsquare += val**2
sigmaX += val
sigmaXY += val*cellphoneprice[index]
dataLength +=1

sumsigmaXsquare = sigmaX**2

Menggunakan variable tersebut selanjutnya kita akan menghitung nilai konstanta a.

constantA = ((sigmaY*sigmaXsquare)-(sigmaX*sigmaXY))/((dataLength*sigmaXsquare)-sumsigmaXsquare)
round(constantA,3)

Lalu kita cari koefisien b dengan cara berikut ini.

# Find coefficient b
coeffB = ((dataLength*sigmaXY)-(sigmaX*sigmaY))/((dataLength*sigmaXsquare)-sumsigmaXsquare)
round(coeffB,3)

Kemudian kita lakukan prediksi terhadap data harga handphone H4 dan H6 menggunakan code berikut ini.

# Find missing price H4 and H6
# y = a+bx
for index,val in enumerate(cellphoneprice):
if not isinstance(val,int):
cellphoneprice[index] = round(constantA+(coeffB*cellphonecamera[index]),2)

Lalu hasil perhitungan beserta data yang ada kita visualisasikan menggunakan seaborn dan matplotlib.

# creater linear graph
sns.regplot(x = cellphonecamera, y = cellphoneprice, data=df)
plt.title('Regression of Cellphone Camera and Price')
plt.show()

Berikut grafik yang dihasilkan.

Code lengkap dapat dilihat pada link github berikut ini.

https://github.com/jeruji/machinelearningalgorithm/blob/main/simple_linear_regression.ipynb



Source link

Previous Post

Falcon 180B foundation model from TII is now available via Amazon SageMaker JumpStart

Next Post

How to Build a Data Science Strategy for Any Team Size | by Sean Easter | Sep, 2023

Next Post

How to Build a Data Science Strategy for Any Team Size | by Sean Easter | Sep, 2023

The History of Gaming Storage

Upcoming Event from Avnet and Renesas Explores AI and Connectivity Solutions

Related Post

Artificial Intelligence

Genius Cliques: Mapping out the Nobel Network | by Milan Janosov | Sep, 2023

by admin
October 1, 2023
Machine Learning

Detecting Anomalies with Z-Scores: A Practical Approach | by Akash Srivastava | Oct, 2023

by admin
October 1, 2023
Machine Learning

What are SWIFT Payments and How Does It Work?

by admin
October 1, 2023
Artificial Intelligence

Speed up your time series forecasting by up to 50 percent with Amazon SageMaker Canvas UI and AutoML APIs

by admin
October 1, 2023
Edge AI

Unleashing LiDAR’s Potential: A Conversation with Innovusion

by admin
October 1, 2023
Artificial Intelligence

16, 8, and 4-bit Floating Point Formats — How Does it Work? | by Dmitrii Eliuseev | Sep, 2023

by admin
September 30, 2023

© Machine Learning News Hubb All rights reserved.

Use of these names, logos, and brands does not imply endorsement unless specified. By using this site, you agree to the Privacy Policy and Terms & Conditions.

Navigate Site

  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us

Newsletter Sign Up.

No Result
View All Result
  • Home
  • Machine Learning
  • Artificial Intelligence
  • Big Data
  • Deep Learning
  • Edge AI
  • Neural Network
  • Contact Us

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.