深度之眼西瓜书课程1.机器学习——逻辑回归

机器学习1——逻辑回归


2020.3.27
报了深度之眼的西瓜书推导课程,在此记录一下自己的学习过程。课程介绍收获:
(1)别想一次性看懂所有知识点
(2)课程之前得先自己推导下课程,找出自己的问题,课程上寻找答案

from sklearn.datasets import fetch_openml
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.utils import check_random_state
import pandas as pd
import time
import matplotlib.pyplot as plt
import numpy as np


df = pd.read_csv('/home/cai/python/PyTorch-YOLOv3-master/SBDdataset/chainercv/mnist_784.csv')# 读取数据
X = df.iloc[: , :-1].values
y = df.iloc[:, -1].values

print(__doc__)

t0=time.time()
train_samples=5000
# X,y=fetch_openml('mnist_784',version=1,return_X_y=True)
# X,y=fetch_openml('mnist_784',data_home='/home/cai/下载/')

#打乱顺序
random_state=check_random_state(0)
permutation=random_state.permutation(X.shape[0])

X=X[permutation]
y=y[permutation]
X=X.reshape((X.shape[0],-1))

#分出训练集和测试集
X_train,X_test,y_train,y_test=train_test_split(
        X,y,train_size=train_samples,test_size=10000)
scaler=StandardScaler() #归一化
X_train=scaler.fit_transform(X_train)
X_test=scaler.transform(X_test)

#设置逻辑回归分类器,
c:正则化系数λ的倒数,
multi_class:分类方式选择参数,str类型,可选参数为ovr和multinomial,默认为ovr。ovr即前面提到的one-vs-rest(OvR),而multinomial即前面提到的many-vs-many(MvM)。
penalty:惩罚项,str类型,可选参数为l1和l2,默认为l2。用于指定惩罚项中使用的规范。newton-cg、sag和lbfgs求解算法只支持L2规范。
solver:优化算法选择参数,只有五个可选参数,即newton-cg,lbfgs,liblinear,sag,saga。saga:线性收敛的随机优化算法的的变重。
tol:停止求解的标准,float类型,默认为1e-4。就是求解到多少的时候,停止,认为已经求出最优解。
clf=LogisticRegression(C=50./train_samples,
                       multi_class='multinomial',
                       penalty='l1',solver='saga',tol=0.1)
#迭代收敛
#clf.fit(X_train,y_train)
#y=w*x+b,目的是要求出w和b
#coef是权重,10×784的矩阵,相当于W,intercept是偏置,10行的向量,相当于b,

#求出权重参数中0的占比,
sparsity=np.mean(clf.coef_==0)*100
#评价测试集的回归准确度
score=clf.score(X_test,y_test)
#print('Best C %.4f'%clf.C__)

print("Sparsity with L1 penalty:%.2f%%"%sparsity)
print("Test score with L1 penalty:%.4f"%score)

coef=clf.coef_.copy()
plt.figure(figsize=(10,5))
#找出权重中最大的数值
scale=np.abs(coef).max()
for i in range(10):
    l1_plot=plt.subplot(2,5,i+1)
    #权重是10×784,把每一行取出来,转成28×28的形式显示出来,interpolation表示最近零插值,cmap表示选择的颜色类型,vmin设定最小值
    l1_plot.imshow(coef[i].reshape(28,28),interpolation='nearest',
                   cmap=plt.cm.RdBu,vmin=-scale,vmax=scale)
    l1_plot.set_xticks(())
    l1_plot.set_yticks(())
    l1_plot.set_xlabel('Class %i'%i)
plt.suptitle('Classification vector for...')

run_time=time.time()-t0
print('Example run in %.3f s'%run_time)
plt.show()

归一化: 有关StandardScaler的transform和fit_transform方法.
权重参数: Python——sklearn 中 Logistics Regression 的 coef_ 和 intercept_ 的具体意义.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值