网站开发的挑战,广告设计专业专升本考什么,如何建立网站和网页,中企动力公司上班好不好#x1f680;个人主页#xff1a;为梦而生~ 关注我一起学习吧#xff01; #x1f4a1;专栏#xff1a;机器学习python实战 欢迎订阅#xff01;后面的内容会越来越有意思~ ⭐内容说明#xff1a;本专栏主要针对机器学习专栏的基础内容进行python的实现#xff0c;部分… 个人主页为梦而生~ 关注我一起学习吧 专栏机器学习python实战 欢迎订阅后面的内容会越来越有意思~ ⭐内容说明本专栏主要针对机器学习专栏的基础内容进行python的实现部分基础知识不再讲解有需要的可以点击专栏自取~ 往期推荐 【机器学习Python实战】线性回归 机器学习基础知识 【机器学习基础】机器学习入门1 【机器学习基础】机器学习入门2 【机器学习基础】机器学习的基本术语 【机器学习基础】机器学习的模型评估评估方法及性能度量原理及主要公式 【机器学习基础】一元线性回归适合初学者的保姆级文章 【机器学习基础】多元线性回归适合初学者的保姆级文章 【机器学习基础】对数几率回归logistic回归 ⭐本期内容针对以上的逻辑回归回归的梯度下降求解方法进行代码展示 文章目录 基于梯度下降的logistic回归sigmoid函数假设函数代价函数梯度下降 代码实现效果展示 ⭐对于逻辑回归的系列基础知识【机器学习基础】对数几率回归logistic回归这篇文章已经讲过了还没了解过的建议先看一下原理 基于梯度下降的logistic回归
sigmoid函数
由基础知识的文章我们知道sigmoid函数长这样 如何用python代码来实现它呢
def Sigmoid(z):G_of_Z float(1.0 / float((1.0 math.exp(-1.0 * z))))return G_of_Z假设函数
同样对于逻辑回归的假设函数我们也需要用python定义 对于这样一个复合函数定义方式如下
def Hypothesis(theta, x):z 0for i in range(len(theta)):z x[i] * theta[i]return Sigmoid(z)代价函数
对于这样一个cost function实现起来是有些难度的
其原理是利用的正规公式 实现过程也是相当于这个公式的计算过程
CostHistory[]
def Cost_Function(X, Y, theta, m):sumOfErrors 0for i in range(m):xi X[i]hi Hypothesis(theta, xi)if Y[i] 1:error Y[i] * math.log(hi)elif Y[i] 0:error (1 - Y[i]) * math.log(1 - hi)sumOfErrors errorCostHistory.append(sumOfErrors)const -1 / mJ const * sumOfErrors#print(cost is , J)return CostHistory梯度下降 【机器学习基础】一元线性回归适合初学者的保姆级文章 【机器学习基础】多元线性回归适合初学者的保姆级文章 在这两篇文章中已经讲过了梯度下降的一些基本概念如果不清楚的可以到前面看一下 代码定义梯度下降的方式如下
def Gradient_Descent(X, Y, theta, m, alpha):new_theta []constant alpha / mfor j in range(len(theta)):CFDerivative Cost_Function_Derivative(X, Y, theta, j, m, alpha)new_theta_value theta[j] - CFDerivativenew_theta.append(new_theta_value)return new_theta每次迭代通过学习率与微分的计算得到新的 θ \theta θ 迭代的策略这里使用的是牛顿法逻辑回归的实现使用梯度下降来更新参数同时使用二分法来逼近最优解。
def newton(X, Y, alpha, theta, num_iters):m len(Y)for x in range(num_iters):new_theta Gradient_Descent(X, Y, theta, m, alpha)theta new_thetaif x % 100 0:Cost_Function(X, Y, theta, m)print(theta , theta)print(cost is , Cost_Function(X, Y, theta, m))Declare_Winner(theta)代码实现
from sklearn import preprocessing
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from numpy import loadtxt, where
from pylab import scatter, show, legend, xlabel, ylabel
import math
import numpy as np
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt# 这是Sigmoid激活函数用于将任何实数映射到介于0和1之间的值。
def Sigmoid(z):G_of_Z float(1.0 / float((1.0 math.exp(-1.0 * z))))return G_of_Z# 这是预测函数输入参数是参数向量theta和输入向量x返回预测的概率。
def Hypothesis(theta, x):z 0for i in range(len(theta)):z x[i] * theta[i]return Sigmoid(z)# 这是代价函数输入参数是训练数据集X、标签Y、参数向量theta和样本数m返回当前参数下的代价函数值和历史误差记录。
CostHistory[]
def Cost_Function(X, Y, theta, m):sumOfErrors 0for i in range(m):xi X[i]hi Hypothesis(theta, xi)if Y[i] 1:error Y[i] * math.log(hi)elif Y[i] 0:error (1 - Y[i]) * math.log(1 - hi)sumOfErrors errorCostHistory.append(sumOfErrors)const -1 / mJ const * sumOfErrors#print(cost is , J)return CostHistory# 这是代价函数对第j个参数的导数用于计算梯度下降中的梯度。
def Cost_Function_Derivative(X, Y, theta, j, m, alpha):sumErrors 0for i in range(m):xi X[i]xij xi[j]hi Hypothesis(theta, X[i])error (hi - Y[i]) * xijsumErrors errorm len(Y)constant float(alpha) / float(m)J constant * sumErrorsreturn J# 这是梯度下降算法的实现用于更新参数向量theta。
def Gradient_Descent(X, Y, theta, m, alpha):new_theta []constant alpha / mfor j in range(len(theta)):CFDerivative Cost_Function_Derivative(X, Y, theta, j, m, alpha)new_theta_value theta[j] - CFDerivativenew_theta.append(new_theta_value)return new_theta# 这是牛顿法逻辑回归的实现使用梯度下降来更新参数同时使用二分法来逼近最优解。
def newton(X, Y, alpha, theta, num_iters):m len(Y)for x in range(num_iters):new_theta Gradient_Descent(X, Y, theta, m, alpha)theta new_thetaif x % 100 0:Cost_Function(X, Y, theta, m)print(theta , theta)print(cost is , Cost_Function(X, Y, theta, m))Declare_Winner(theta)# 该函数主要用于确定训练好的逻辑回归模型这里命名为clf对测试集的预测结果并返回一个赢家预测准确率更高的模型。
def Declare_Winner(theta):score 0winner scikit_score clf.score(X_test, Y_test)length len(X_test)for i in range(length):prediction round(Hypothesis(X_test[i], theta))answer Y_test[i]if prediction answer:score 1my_score float(score) / float(length)
min_max_scaler preprocessing.MinMaxScaler(feature_range(-1, 1))x_input1, x_input2, Y np.genfromtxt(dataset3.txt, unpackTrue, delimiter,)
print(x_input1.shape)
print(x_input2.shape)
print(Y.shape)
X np.column_stack((x_input1, x_input2))X min_max_scaler.fit_transform(X)X_train, X_test, Y_train, Y_test train_test_split(X, Y, test_size0.33)clf LogisticRegression()
clf.fit(X_train, Y_train)
print(Acuraccy is: , (clf.score(X_test, Y_test) * 100))
pos where(Y 1)
neg where(Y 0)
scatter(X[pos, 0], X[pos, 1], markero, cb)
scatter(X[neg, 0], X[neg, 1], markerx, cg)
xlabel(score 1)
ylabel(score 2)
legend([0, 1])initial_theta [0, 0]
alpha 0.01
iterations 100
m len(Y)
mycostCost_Function(X,Y,initial_theta,m)
mycostnp.asarray(mycost)
print(mycost.shape)
plt.figure()
plt.plot(range(iterations),mycost)
newton(X,Y,alpha,initial_theta,iterations)
# print(theta is: ,my_theta)
plt.show()
效果展示
首先是基于数据集做出的散点图并标记出了正例和负例 对于该散点图可以做出一条分割正负样本的直线 下面是程序的一些输出