第一课第二周: 逻辑回归

1.我的学习路线:

  • 1.数据全部读进去,分为dataX.dataY
  • 2.打出shape
  • 3.然后选取一个sample
  • 4.随机将TD按照2:8划分
  • 5.搞懂LR原理,代码实现
  • 6.框架函数的shape要打印出来
  • 7.思考:在反向传播之前,神经网络是如何训练的?
  • 8.processing of normalization
  • 9.nb feature extraction
  • 10.模块组合成模型
  • 11. loop 每间隔100次记录cost,画出cost的hist
  • 12. 错误:gradient中重复定义了theta
  • 13. cost函数写错

2.实战:(数据来源于文献中公开的coil dataset)

蛋白质位点预测
问题描述:
通过提取特征、特征选择、机器学习的分类模型去预测蛋白质在细胞中的定位位置并将结果可视化,要求对所有的训练数据集进行交叉验证。
 
数据描述:
*336个coli数据,每个数据有八个属性*,如: AAT_ECOLI 0.49 0.29 0.48 0.50 0.56 0.24 0.35 cp 1-8是属性值,cp是定位的位点名称。
 
属性信息:
1.Sequence Name: 序列名称。
2.mcg: McGeoch的序列识别方法。
3..gvh: von Heijne的序列识别方法。
4.lip: von Heijne的序列得分。
5.chg:对蛋白质N端的预测。
6.aac: 氨基酸含量得分。
7.alm1: 跨膜区预测评分。
8.alm2: ALOM得分。
9.数据中定位的位点有八种,分别是:cp(定位名称):143(个数) im : 77 pp:52 imU :35 om : 20 omL : 5 imL : 2 imS: 2
 
Logic Regression:
  • 算法描述:
    找出合适的权值w和偏移量b,通过sigmod函数求出拟合真实情景的程度(取值介于0-1之间)。

    通过cost函数计算出yhat和y之间的损失,通过设置循环次数,求出最合适的w和b。在多次迭代后得出训练模型,通过测试样例对数据进行分析。
  • 步骤描述:
    定义结构:
    层数、输入x的数量、输入y的数量。


    选取初始参数值:
    通常w不为0,权值b取0。


    循环:
    前向传播,计算损失函数值J=−1m∑i=0m(y(i)log(a[2](i))+(1−y(i))log(1−a[2](i))),后向传播,更新参数。


    最终,得出训练的模型。

代码实现:

import numpy as np
import math
import pandas
import matplotlib.pyplot as plt
from sklearn.model_selection  import train_test_split
# load dataset
dataframe = pandas.read_csv("../input/ecoli.csv", delim_whitespace=True)
# Assign names to Columns
dataframe.columns = ['seq_name', 'mcg', 'gvh', 'lip', 'chg', 'aac', 'alm1', 'alm2', 'site']
dataframe = dataframe.drop('seq_name', axis=1)
# Encode Data
dataframe.site.replace(('cp', 'im', 'pp', 'imU', 'om', 'omL', 'imL', 'imS'),(1,0,0,0,0,0,0,0), inplace=True)
dataset = dataframe.values
#dataset divide
DataX = np.array(dataset[:,0:7])
DataY = np.transpose([dataset[:,7]])
print (DataX.shape, DataY.shape)
#dataset split
X_train,  X_test,Y_train, Y_test = train_test_split(DataX, DataY, test_size = 0.2)
#Initialization parameters
def thetafunc(X):
    m, n = X.shape
    theta = np.mat(np.zeros(n))
    return theta
#this is linear calculate part
# theta is a vector of b,w  
#X is a sample feature matrix,each row represents a sample
def linear_cal(theta, X):
    result = np.dot(theta, X.T)
    return result
#the sigmoid function in order to calculate the y hat
#X is a sample feature matrix,each row represents a sample  
#shape is (1, 268)
def sigmoid(theta, X):
    result = 1.0 / (1 + np.exp(-linear_cal(theta, X)))
    return result
# cost function
def costfunction(theta, X, Y):
    #cost =  -(Y * np.log(sigmoid(theta, X)) -  np.log(1 - sigmoid(theta, X)) * (1- Y)) / Y.size
    yhat = sigmoid(theta, X)
    loss =  Y * np.log(yhat) + (1- Y) * np.log(1 - yhat)  
    cost = -np.sum(loss) / Y.size
    return cost
#calculate the gredient
#theta is matrix of db,dw
#Y is corresponding to the labeled samples in X
def gredient(theta, X, Y):
    #theta = thetafunc(X)
    yhat = sigmoid(theta, X)
    gredient = (yhat - Y.T) * X / Y.size
    return gredient
#estimation parameters of gredient descent method, update dw, db
#alpha means step length ,we define the alpha equal to 0.001
#iter_num is number of iterations ,we define the iter_num equal to 2000
def gradient_descent(X, Y,alpha, iter_num):
    theta = thetafunc(X)
    J = {}
    g = gredient(theta, X_train , Y_train)
    print (g)
    for i in range(iter_num):
        theta = theta - alpha * gredient(theta, X, Y)
        cost = costfunction(theta, X, Y)
        if (i % 10 == 0):
            J[i] =cost
    plt.plot(J.keys(), J.values())
    #print (J)
    return theta.shape
# debug coding
gradient_descent(X_train, Y_train, 0.001, 2000)
 
3.思考和总结:
 
       逻辑回归是深度学习的基础和关键知识点,所用到的技能和知识点对后面的学习比较重要。所以,从头开始,从理论到代码实现一步一步走过来,收获最大的就是对逻辑回归有了一个全面的认识,这也是我们对待知识的态度。还有做算法研究也需要对理论有一个深入的研究,然后构建数学模型,自己提出一个简单的想法,加以实现,再通过实验验证。fighting!还是很享受这个学习过程的,还要感谢partners跟我一起交流。
 
 
原文地址:https://www.cnblogs.com/ylHe/p/8151356.html