题解 | #使用梯度下降对逻辑回归进行训练#

使用梯度下降对逻辑回归进行训练

https://www.nowcoder.com/practice/d9c4bcf3bc5e426b8a11e690f65ba601

import pandas as pd
import numpy as np

dataSet = pd.read_csv('dataSet.csv',header = None)
labels = pd.read_csv('labels.csv',header = None)

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def gradientDescent(dataSet, labels, alpha=0.001, max_num=100):
    dataSet = np.mat(dataSet)
    labels = np.mat(labels)
    n, m = dataSet.shape
    weight = np.ones((m,1))
    for i in range(max_num):
        h = sigmoid(np.dot(dataSet, weight))
        error = h - labels.T
        weight = weight - alpha * np.dot(dataSet.T, error)
    return weight

if __name__ == "__main__":
    print(gradientDescent(dataSet, labels, alpha=0.001, max_num=100))

全部评论

相关推荐

10-13 16:58
门头沟学院 Java
点赞 评论 收藏
分享
11-03 18:50
门头沟学院 Java
迷茫的大四🐶:问就是马上到,一周五天,6个月以上,全国可飞
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务