0% found this document useful (0 votes)
4 views2 pages

Linear Regression Analysis in Python

This document contains code to perform simple linear regression on Indonesian financial data. It defines functions for linear regression calculation and plotting, then calls the plotting function to generate a scatter plot with regression line showing the relationship between profits and earnings in the data.

Uploaded by

Hilal Ismail
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Linear Regression Analysis in Python

This document contains code to perform simple linear regression on Indonesian financial data. It defines functions for linear regression calculation and plotting, then calls the plotting function to generate a scatter plot with regression line showing the relationship between profits and earnings in the data.

Uploaded by

Hilal Ismail
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import numpy as np

import [Link] as plt


from matplotlib import style
[Link]("ggplot")
keuntungan = [7000000, 10000000, 7500000, 5000000, 17000000, 7000000, 14000000]
laba = [3000000, 4000000, 2000000, 1200000, 5000000, 2000000, 5000000]

def linearRegresion(data):
'''
indeks[0] -> response variable -> x
indeks[1] -> predictor variable -> y
'''
x2=[]
y2=[]
xy=[]
n = len(data[0])

for x in data[0]:
[Link](x**2)

for y in data[1]:
[Link](y**2)
i=0;
while(i<n):
dump = data[0][i]*data[1][i]
[Link](dump)
i+=1
jmlhx = sum(data[0])
jmlhy = sum(data[1])
jmlhx2 = sum(x2)
jmlhy2 = sum(y2)
jmlhxy = sum(xy)
a = ((jmlhy*jmlhx2)-(jmlhx*jmlhxy))/(n*jmlhx2-(jmlhx**2))
b = ((n*jmlhxy)-(jmlhx*jmlhy))/(n*jmlhx2-(jmlhx**2))
return(a,b)

def gambarGrafik(dataProses):
a,b = linearRegresion(dataProses)
print("Nilai a adalah %.4f"%(a))
print("Nilai b adalah %.4f"%(b))
def f1(keanggotaan,a,b):
hit = []
for x in keanggotaan:
y = b*x+a
[Link](y)
return(hit)
[Link](dataProses[0],dataProses[1],label='data aktual',s=10)
[Link](dataProses[0],f1(dataProses[0],a,b),c='k',label='hasil regresi without',linewidth=0.5)
[Link]("Hasil regresi Linear Sederhana")
[Link]("laba")
[Link]("keuntungan")
[Link]()
fig = [Link](1)
[Link].set_window_title("regresi by sufyan97")
[Link]()

gambarGrafik([keuntungan,laba])

You might also like