Correlation and Regression (TP)
Correlation and Regression (TP)
ipynb - Colaboratory
import pandas as pd
from google.colab import files
uploaded = files.upload()
filename = next(iter(uploaded))
dataset=pd.read_csv(filename,encoding='latin1')
dataset.head()
dataset.plot(kind='scatter', x='Radio', y=['Sales'], style='.=')
<matplotlib.axes._subplots.AxesSubplot at 0x7f49e3c71410>
variabel = dataset[['Radio', 'Sales']]
korelasi = variabel.corr(method='pearson')
# korelasi = variabel.corr(method='spearman)
korelasi
https://colab.research.google.com/drive/1Tv7K1YA8fw8RJ5RPFZwnc-p03gOcatBe?authuser=1#scrollTo=gvXjr6_eHHO1&uniqifier=4&printMode=true 1/4
7/2/2021 Correlation and Regression(TP).ipynb - Colaboratory
Radio Sales
dt_test.tail()
from sklearn.linear_model import LinearRegression
x=dt_train[['Radio']]
y=dt_train[['Sales']]
# Linear Regression Model
linreg=LinearRegression()
# #Membuat model dengan latih
linreg.fit(x,y)
print('Coefisien regressi: ', linreg.coef_) #coefisien
#membuat prediksi pada data uji
x_test=dt_test[['Sales']]
y_pred=linreg.predict(x_test)
# #membandingkan hasil prediksi dengan data test
https://colab.research.google.com/drive/1Tv7K1YA8fw8RJ5RPFZwnc-p03gOcatBe?authuser=1#scrollTo=gvXjr6_eHHO1&uniqifier=4&printMode=true 2/4
7/2/2021 Correlation and Regression(TP).ipynb - Colaboratory
# #membandingkan hasil prediksi dengan data test
result=x_test
result["y_pred"]=y_pred
result.head()
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:7: SettingWithCopyWar
A value is trying to be set on a copy of a slice from a DataFrame.
Sales y_pred
#prepare plot
import matplotlib.pyplot as plt
plt.scatter(x,y,color='red')#data training
plt.scatter(x_test["Sales"], y_pred, color='blue')#data predict/data test
plt.plot(x,linreg.predict(x),color='green')#line
plt.xlabel('Radio')
plt.ylabel('Sales')
plt.show()
import math
def average(x):
assert len(x) > 0
return float(sum(x)) / len(x)
https://colab.research.google.com/drive/1Tv7K1YA8fw8RJ5RPFZwnc-p03gOcatBe?authuser=1#scrollTo=gvXjr6_eHHO1&uniqifier=4&printMode=true 3/4
7/2/2021 Correlation and Regression(TP).ipynb - Colaboratory
def pearson_def(x, y):
assert len(x) == len(y)
n = len(x)
assert n > 0
avg_x = average(x)
avg_y = average(y)
diffprod = 0
xdiff2 = 0
ydiff2 = 0
for idx in range(n):
xdiff = x[idx] - avg_x
ydiff = y[idx] - avg_y
diffprod += xdiff * ydiff
xdiff2 += xdiff * xdiff
ydiff2 += ydiff * ydiff
print(x+y)
return diffprod / math.sqrt(xdiff2 * ydiff2)
hasil_pearson= pearson_def([1,2,3], [1,5,7])
[1, 2, 3, 1, 5, 7]
[1, 2, 3, 1, 5, 7]
[1, 2, 3, 1, 5, 7]
https://colab.research.google.com/drive/1Tv7K1YA8fw8RJ5RPFZwnc-p03gOcatBe?authuser=1#scrollTo=gvXjr6_eHHO1&uniqifier=4&printMode=true 4/4