You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromsklearn.model_selectionimporttrain_test_splitX_train, X_test, y_train, y_test=train_test_split(X, y, test_size=0.2)
X_train.shape
(160, 2)
X_test.shape
(40, 2)
Create model
classClassificationLinearCustom():
""" Implementation Classification Linear Model. """def__init__(self, learning_rate=0.0001, max_iter=100):
""" Initializer parameters model """self.lrate=learning_rateself.max_iter=max_iterself.coefs_=Noneself.error_gradient=Noneself.is_fited=Falsedefsigmoid(self, coefs, x):
""" Sigmoid funtion """return1/(1+np.exp(-(coefs*x).sum()))
deferror_coefs(self, coefs, xi, yi, m, lr):
""" Calcul coefs gradient: -- Parameters : - xi : value for one sample in data - yi : real value prediction for this precedent sample - m : dataset size - lr : learning rate """returnlr/m* (xi.T*self.sigmoid(coefs, xi)-yi)
deffit(self,x, y):
""" Fit fuction, learning parameters -- Parameters: - x, sample data - y, predict data """#scalling datax= (x-x.min())/(x.max()-x.min())
ifx.shape[0] !=y.shape[0]:
returnValueError("x and y must have same sample")
m=x.shape[0] # size du datasetself.coefs_=1# nuers of featuresfor_inrange(self.max_iter):
forxi,yiinzip(x,y):
self.coefs_-=self.error_coefs(self.coefs_, xi, yi, m, self.lrate)
self.is_fited=Trueprint('ClassificationLinearCustom(learning_rate={}, max_iter={})'.format(self.lrate, self.max_iter))
defpredict(self, x):
""" Predict function : -- Parameters: - x, sample data what to predict """ypred_proba= []
ifnotself.is_fited:
returnValueError("model must fited after predict")
ifx.shape[1] !=self.coefs_.shape[0]:
returnValueError("The features of x do not have the same size as those to train")
forxiinx:
ypred_proba+=[1] ifself.sigmoid(self.coefs_, xi) >0.5else [0]
returnnp.array(ypred_proba)