ML_aPP:8.Logistic regression笔记

Logistic回归对于{0,1}分类有很好的效果;Logistic model 算是Generalized Linear Model中的一类。 

本章目录:


sigmoid函数


p(y=1|x,w)>0.5预测y=1;否则预测y=0.下面两个图中的分类可以展示Logistic的魔力。



8.2 Model specification

p(y=1|x,w)可以看成一次试验的成功的概率,所以有下面的分布形式:


sigmoid函数在参数w是二维时的图像


=================================================

8.3 Model Fitting

本节讨论Logistic回归的参数估计


以下的推导使用了一些小技巧,另外Hessian阵是对称的,对f(w)不同分类wi,wj先后求导结果相同,故求Hessian阵时对g的转置求导亦得到正确结果。

注意:ui =p(y=1|xi,w) ;g=X'(u-y)中的y∈{0 1};

另外:



可以看到,不像线性回归,这时候,很难得到w的解析形式。故需要使用优化的手段来求解w的数值解。

数值解的部分,略去。过段时间<2013/01/10的样子>写篇《机器学习中的优化方法》,统一处理了。

8.3.4 迭代加权最小二乘

使用Newton公式:


算法8.2


计算Hessian阵可能是费时的,在优化的研究里面提出了共轭梯度等方法。

8.3.6 l2正则化<l2 regularization>

正则化在分类中很重要。


8.3.7  多类别Logistic回归<Multi-class logistic regression>

以上讨论都是binary logistic regression,以下讨论多类别回归。


参数估计等。

=================================================

8.4 Bayesian logistic regression

略。

8.5 Online learning and stochastic optimization

对于流数据需要使用在线学习在处理。在offline learning情况中如果data太大不能一次性装入内存,就可以使用online learning技术来学习。

略。

8.6 Generative vs discriminative classifiers

略。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
把这段代码的PCA换成LDA:LR_grid = LogisticRegression(max_iter=1000, random_state=42) LR_grid_search = GridSearchCV(LR_grid, param_grid=param_grid, cv=cvx ,scoring=scoring,n_jobs=10,verbose=0) LR_grid_search.fit(pca_X_train, train_y) estimators = [ ('lr', LR_grid_search.best_estimator_), ('svc', svc_grid_search.best_estimator_), ] clf = StackingClassifier(estimators=estimators, final_estimator=LinearSVC(C=5, random_state=42),n_jobs=10,verbose=1) clf.fit(pca_X_train, train_y) estimators = [ ('lr', LR_grid_search.best_estimator_), ('svc', svc_grid_search.best_estimator_), ] param_grid = {'final_estimator':[LogisticRegression(C=0.00001),LogisticRegression(C=0.0001), LogisticRegression(C=0.001),LogisticRegression(C=0.01), LogisticRegression(C=0.1),LogisticRegression(C=1), LogisticRegression(C=10),LogisticRegression(C=100), LogisticRegression(C=1000)]} Stacking_grid =StackingClassifier(estimators=estimators,) Stacking_grid_search = GridSearchCV(Stacking_grid, param_grid=param_grid, cv=cvx, scoring=scoring,n_jobs=10,verbose=0) Stacking_grid_search.fit(pca_X_train, train_y) Stacking_grid_search.best_estimator_ train_pre_y = cross_val_predict(Stacking_grid_search.best_estimator_, pca_X_train,train_y, cv=cvx) train_res1=get_measures_gridloo(train_y,train_pre_y) test_pre_y = Stacking_grid_search.predict(pca_X_test) test_res1=get_measures_gridloo(test_y,test_pre_y) best_pca_train_aucs.append(train_res1.loc[:,"AUC"]) best_pca_test_aucs.append(test_res1.loc[:,"AUC"]) best_pca_train_scores.append(train_res1) best_pca_test_scores.append(test_res1) train_aucs.append(np.max(best_pca_train_aucs)) test_aucs.append(best_pca_test_aucs[np.argmax(best_pca_train_aucs)].item()) train_scores.append(best_pca_train_scores[np.argmax(best_pca_train_aucs)]) test_scores.append(best_pca_test_scores[np.argmax(best_pca_train_aucs)]) pca_comp.append(n_components[np.argmax(best_pca_train_aucs)]) print("n_components:") print(n_components[np.argmax(best_pca_train_aucs)])
最新发布
07-22
如果要将代码中的PCA替换为LDA,可以按照以下步骤进行修改: ```python LR_grid = LogisticRegression(max_iter=1000, random_state=42) LR_grid_search = GridSearchCV(LR_grid, param_grid=param_grid, cv=cvx ,scoring=scoring,n_jobs=10,verbose=0) LR_grid_search.fit(lda_X_train, train_y) estimators = [ ('lr', LR_grid_search.best_estimator_), ('svc', svc_grid_search.best_estimator_), ] clf = StackingClassifier(estimators=estimators, final_estimator=LinearSVC(C=5, random_state=42),n_jobs=10,verbose=1) clf.fit(lda_X_train, train_y) estimators = [ ('lr', LR_grid_search.best_estimator_), ('svc', svc_grid_search.best_estimator_), ] param_grid = {'final_estimator': [LogisticRegression(C=0.00001),LogisticRegression(C=0.0001), LogisticRegression(C=0.001),LogisticRegression(C=0.01), LogisticRegression(C=0.1),LogisticRegression(C=1), LogisticRegression(C=10),LogisticRegression(C=100), LogisticRegression(C=1000)]} Stacking_grid = StackingClassifier(estimators=estimators,) Stacking_grid_search = GridSearchCV(Stacking_grid, param_grid=param_grid, cv=cvx, scoring=scoring, n_jobs=10, verbose=0) Stacking_grid_search.fit(lda_X_train, train_y) Stacking_grid_search.best_estimator_ train_pre_y = cross_val_predict(Stacking_grid_search.best_estimator_, lda_X_train, train_y, cv=cvx) train_res1 = get_measures_gridloo(train_y, train_pre_y) test_pre_y = Stacking_grid_search.predict(lda_X_test) test_res1 = get_measures_gridloo(test_y, test_pre_y) best_lda_train_aucs.append(train_res1.loc[:,"AUC"]) best_lda_test_aucs.append(test_res1.loc[:,"AUC"]) best_lda_train_scores.append(train_res1) best_lda_test_scores.append(test_res1) train_aucs.append(np.max(best_lda_train_aucs)) test_aucs.append(best_lda_test_aucs[np.argmax(best_lda_train_aucs)].item()) train_scores.append(best_lda_train_scores[np.argmax(best_lda_train_aucs)]) test_scores.append(best_lda_test_scores[np.argmax(best_lda_train_aucs)]) lda_comp.append(n_components[np.argmax(best_lda_train_aucs)]) print("n_components:") print(n_components[np.argmax(best_lda_train_aucs)]) ``` 在这个修改后的代码中,将`pca_X_train`和`pca_X_test`替换为`lda_X_train`和`lda_X_test`,并相应地修改变量和参数的名称。这样就可以使用LDA进行特征降维和模型训练了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值