Created
July 18, 2018 15:46
-
-
Save ashishpatel26/b1f70be3b9bbd55cfdcb342559c60390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.linear_model import LogisticRegression | |
class LR(LogisticRegression): | |
def __init__(self, threshold=0.01, dual=False, tol=1e-4, C=1.0, | |
fit_intercept=True, intercept_scaling=1, class_weight=None, | |
random_state=None, solver='liblinear', max_iter=100, | |
multi_class='ovr', verbose=0, warm_start=False, n_jobs=1): | |
#Thresold | |
self.threshold = threshold | |
LogisticRegression.__init__(self, penalty='l1', dual=dual, tol=tol, C=C, | |
fit_intercept=fit_intercept, intercept_scaling=intercept_scaling, class_weight=class_weight, | |
random_state=random_state, solver=solver, max_iter=max_iter, | |
multi_class=multi_class, verbose=verbose, warm_start=warm_start, n_jobs=n_jobs) | |
#Create L2 logistic regression using the same parameters | |
self.l2 = LogisticRegression(penalty='l2', dual=dual, tol=tol, C=C, fit_intercept=fit_intercept, intercept_scaling=intercept_scaling, class_weight = class_weight, random_state=random_state, solver=solver, max_iter=max_iter, multi_class=multi_class, verbose=verbose, warm_start=warm_start, n_jobs=n_jobs) | |
def fit(self, X, y, sample_weight=None): | |
# Training L1 logistic regression | |
super(LR, self).fit(X, y, sample_weight=sample_weight) | |
self.coef_old_ = self.coef_.copy() | |
# L2 logistic regression training | |
self.l2.fit(X, y, sample_weight=sample_weight) | |
cntOfRow, cntOfCol = self.coef_.shape | |
#Number of coefficient matrix The number of rows corresponds to the number of types of target values | |
for i in range(cntOfRow): | |
for j in range(cntOfCol): | |
coef = self.coef_[i][j] | |
# The weight coefficient of L1 logistic regression is not 0. | |
if coef != 0: | |
idx = [j] | |
#correspond to the weight coefficient in L2 logistic regression | |
coef1 = self.l2.coef_[i][j] | |
for k in range(cntOfCol): | |
coef2 = self.l2.coef_[i][k] | |
#In L2 logistic regression, the difference between the weight coefficients is less than the set threshold, and the corresponding weight in L1 is 0. | |
if abs(coef1-coef2) < self.threshold and j != k and self.coef_[i][k] == 0: | |
idx.append(k) | |
#Calculate the mean value of the weight coefficient of this type of feature | |
mean = coef / len(idx) | |
self.coef_[i][idx] = mean | |
return self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment