Question: import numpy as np import matplotlib.pyplot as plt import pandas as pd class LinearRegression: def _ _ init _ _ ( self , X ,

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
class LinearRegression:
def __init__(self,X,Y): # x ve y ayrlp verilecek
ones=np.ones(X.shape)
X=np.append(ones,X,axis=1)
self.X=X
self.Y=Y
self.m=X.shape[0]
self.n=X.shape[1]
self.theta=np.random.randn(X.shape[1])
def computeCostFunction(self):
h=np.matmul(self.X,self.theta)
self.J=(1/(2*self.m))*np.sum((h-self.Y)**2)
return self.J
def performGradientDescent(self,num_of_iter,alpha):# iterasyon says
self.Cost_history=[]
self.theta_history=[]
for x in range(num_of_iter):
h=np.matmul(self.X,self.theta)
J=self.computeCostFunction()
self.Cost_history.append(J)
self.theta_history.append(self.theta)
temp=h-self.Y
self.theta=self.theta-(alpha/self.m)*(self.X.T.dot(temp))
return self.theta,self.Cost_history,self.theta_history
def predict(self,X_test,Y_test):
ones=np.ones(X_test.shape)
X_test=np.append(ones,X_test,axis=1)
self.Y_pred=np.matmul(X_test,self.theta)
self.error_percentage=(abs(self.Y_pred-Y_test)/Y_test)*100
return self.Y_pred,self.error_percentage
def predictUsingNormalEquation(self,X_test,Y_test):
ones=np.ones(X_test.shape)
X_test=np.append(ones,X_test,axis=1)
inv=np.linalg.inv(np.matmul(self.X.T,self.X))
self.w=np.matmul(np.matmul(inv,self.X.T),self.Y)
y_pred=np.matmul(X_test,self.w)
return y_pred,(abs(Y_test-y_pred)/Y_test)*100 bu kodu bir rnekte Bu almada Pythonda Dorusal Regresyon (Linear Regression) sfrdan uygulayacaksnz.
Veri Seti znitelikleri:
Kategori,
Sayfa toplam beenisi: irketin sayfasn beenen kii says),
Tr: erik tr(Balant, Fotoraf, Durum, Video),
Gnderi ay: Gnderinin yaynland ay (Ocak,ubat, Mart,..., Aralk),
Gnderim saati: Gnderinin yaynland saat (0,1,2,3,4,...,23),
Hafta ii gnderi: Gnderinin yaynland hafta ii (Pazar, Pazartesi, ..., Cumartesi),
cretli: irketin reklam iin Facebook'a deme yapmas durumunda (evet, hayr)
Modellemek iin:
Modellemeye alabileceimiz birok olaszellik var ancak biz Total Interactionse
odaklanacaz.zellik alanmz unlar ierecektir: Category,Page total likes,Post month,
Post hour,Post weekday, ve Paid.n ilemeyi nlemek iin "Type" seeneini brakyoruz.
Veri Seti: Facebook Posts Metrics
Takip Edilmesi Gereken Admlar:
Verinin indirilmesi
Verinin okunmas
Verileri Blme (X ve Y (X_train, X_Test ve y_train, y_test))
a. Veri kmesini X ve Y'ye bln.
b. Salanan yzdesel blmeyi kullanarak X ve Y'yi eitim ve test setlerine ayrn
(varsaylan,%80 eitim ve %20 testtir).
Modelin eitimi ve test edilmesi
a. train_test_split()esini ararak eitim ve test setini aln
b. Bir arlk (\theta ) vektrn tanmlayn
c. Yukardaki bilgileri kullanarak Gradyan niini (Gradient Descent) uygulayn
d. Eitim ve test verileri iin Toplam Kare Hatay(Sum Squared Error) kaydedin
e. Arlk matrisini, eitim hatalarn ve test hatalarn dndrn
f. Eitim ve test hatalarnizin ve grafik zerinde yorum yapn bu devi uygula

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!