- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python 实现逻辑回归由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
适用类型:解决二分类问题 。
逻辑回归的出现:线性回归可以预测连续值,但是不能解决分类问题,我们需要根据预测的结果判定其属于正类还是负类。所以逻辑回归就是将线性回归的结果,通过Sigmoid函数映射到(0,1)之间 。
线性回归的决策函数:数据与θ的乘法,数据的矩阵格式(样本数×列数),θ的矩阵格式(列数×1) 。
将其通过Sigmoid函数,获得逻辑回归的决策函数 。
可以对(-∞, +∞)的结果,映射到(0, 1)之间作为概率 。
可以将1/2作为决策边界 。
数学特性好,求导容易 。
线性回归的损失函数维平方损失函数,如果将其用于逻辑回归的损失函数,则其数学特性不好,有很多局部极小值,难以用梯度下降法求解最优 。
这里使用对数损失函数 。
解释:如果一个样本为正样本,那么我们希望将其预测为正样本的概率p越大越好,也就是决策函数的值越大越好,则logp越大越好,逻辑回归的决策函数值就是样本为正的概率;如果一个样本为负样本,那么我们希望将其预测为负样本的概率越大越好,也就是(1-p)越大越好,即log(1-p)越大越好 。
为什么使用对数函数:样本集中有很多样本,要求其概率连乘,概率为0-1之间的数,连乘越来越小,利用log变换将其变为连加,不会溢出,不会超出计算精度 。
损失函数:: y(1->m)表示Sigmoid值(样本数×1),hθx(1->m)表示决策函数值(样本数×1),所以中括号的值(1×1) 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import
numpy as np
from
matplotlib
import
pyplot as plt
from
scipy.optimize
import
minimize
from
sklearn.preprocessing
import
PolynomialFeatures
class
MyLogisticRegression:
def
__init__(
self
):
plt.rcParams[
"font.sans-serif"
]
=
[
"SimHei"
]
# 包含数据和标签的数据集
self
.data
=
np.loadtxt(
"./data2.txt"
, delimiter
=
","
)
self
.data_mat
=
self
.data[:,
0
:
2
]
self
.label_mat
=
self
.data[:,
2
]
self
.thetas
=
np.zeros((
self
.data_mat.shape[
1
]))
# 生成多项式特征,最高6次项
self
.poly
=
PolynomialFeatures(
6
)
self
.p_data_mat
=
self
.poly.fit_transform(
self
.data_mat)
def
cost_func_reg(
self
, theta, reg):
"""
损失函数具体实现
:param theta: 逻辑回归系数
:param data_mat: 带有截距项的数据集
:param label_mat: 标签数据集
:param reg:
:return:
"""
m
=
self
.label_mat.size
label_mat
=
self
.label_mat.reshape(
-
1
,
1
)
h
=
self
.sigmoid(
self
.p_data_mat.dot(theta))
J
=
-
1
*
(
1
/
m)
*
(np.log(h).T.dot(label_mat)
+
np.log(
1
-
h).T.dot(
1
-
label_mat))\
+
(reg
/
(
2
*
m))
*
np.
sum
(np.square(theta[
1
:]))
if
np.isnan(J[
0
]):
return
np.inf
return
J[
0
]
def
gradient_reg(
self
, theta, reg):
m
=
self
.label_mat.size
h
=
self
.sigmoid(
self
.p_data_mat.dot(theta.reshape(
-
1
,
1
)))
label_mat
=
self
.label_mat.reshape(
-
1
,
1
)
grad
=
(
1
/
m)
*
self
.p_data_mat.T.dot(h
-
label_mat)
+
(reg
/
m)
*
np.r_[[[
0
]], theta[
1
:].reshape(
-
1
,
1
)]
return
grad
def
gradient_descent_reg(
self
, alpha
=
0.01
, reg
=
0
, iterations
=
200
):
"""
逻辑回归梯度下降收敛函数
:param alpha: 学习率
:param reg:
:param iterations: 最大迭代次数
:return: 逻辑回归系数组
"""
m, n
=
self
.p_data_mat.shape
theta
=
np.zeros((n,
1
))
theta_set
=
[]
for
i
in
range
(iterations):
grad
=
self
.gradient_reg(theta, reg)
theta
=
theta
-
alpha
*
grad.reshape(
-
1
,
1
)
theta_set.append(theta)
return
theta, theta_set
def
plot_data_reg(
self
, x_label
=
None
, y_label
=
None
, neg_text
=
"negative"
, pos_text
=
"positive"
, thetas
=
None
):
neg
=
self
.label_mat
=
=
0
pos
=
self
.label_mat
=
=
1
fig1
=
plt.figure(figsize
=
(
12
,
8
))
ax1
=
fig1.add_subplot(
111
)
ax1.scatter(
self
.p_data_mat[neg][:,
1
],
self
.p_data_mat[neg][:,
2
], marker
=
"o"
, s
=
100
, label
=
neg_text)
ax1.scatter(
self
.p_data_mat[pos][:,
1
],
self
.p_data_mat[pos][:,
2
], marker
=
"+"
, s
=
100
, label
=
pos_text)
ax1.set_xlabel(x_label, fontsize
=
14
)
# 描绘逻辑回归直线(曲线)
if
isinstance
(thetas,
type
(np.array([]))):
x1_min, x1_max
=
self
.p_data_mat[:,
1
].
min
(),
self
.p_data_mat[:,
1
].
max
()
x2_min, x2_max
=
self
.p_data_mat[:,
2
].
min
(),
self
.p_data_mat[:,
2
].
max
()
xx1, xx2
=
np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
h
=
self
.sigmoid(
self
.poly.fit_transform(np.c_[xx1.ravel(), xx2.ravel()]).dot(thetas))
h
=
h.reshape(xx1.shape)
ax1.contour(xx1, xx2, h, [
0.5
], linewidths
=
3
)
ax1.legend(fontsize
=
14
)
plt.show()
@staticmethod
def
sigmoid(z):
return
1.0
/
(
1
+
np.exp(
-
z))
if
__name__
=
=
'__main__'
:
my_logistic_regression
=
MyLogisticRegression()
# my_logistic_regression.plot_data(x_label="线性不可分数据集")
thetas, theta_set
=
my_logistic_regression.gradient_descent_reg(alpha
=
0.5
, reg
=
0
, iterations
=
500
)
my_logistic_regression.plot_data_reg(thetas
=
thetas, x_label
=
"$\\lambda$ = {}"
.
format
(
0
))
thetas
=
np.zeros((my_logistic_regression.p_data_mat.shape[
1
],
1
))
# 未知错误,有大佬解决可留言
result
=
minimize(my_logistic_regression.cost_func_reg, thetas,
args
=
(
0
, ),
method
=
None
,
jac
=
my_logistic_regression.gradient_reg)
my_logistic_regression.plot_data_reg(thetas
=
result.x, x_label
=
"$\\lambda$ = {}"
.
format
(
0
))
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
import
numpy as np
from
matplotlib
import
pyplot as plt
from
scipy.optimize
import
minimize
from
sklearn.preprocessing
import
PolynomialFeatures
class
MyLogisticRegression:
def
__init__(
self
):
plt.rcParams[
"font.sans-serif"
]
=
[
"SimHei"
]
# 包含数据和标签的数据集
self
.data
=
np.loadtxt(
"./data2.txt"
, delimiter
=
","
)
self
.data_mat
=
self
.data[:,
0
:
2
]
self
.label_mat
=
self
.data[:,
2
]
self
.thetas
=
np.zeros((
self
.data_mat.shape[
1
]))
# 生成多项式特征,最高6次项
self
.poly
=
PolynomialFeatures(
6
)
self
.p_data_mat
=
self
.poly.fit_transform(
self
.data_mat)
def
cost_func_reg(
self
, theta, reg):
"""
损失函数具体实现
:param theta: 逻辑回归系数
:param data_mat: 带有截距项的数据集
:param label_mat: 标签数据集
:param reg:
:return:
"""
m
=
self
.label_mat.size
label_mat
=
self
.label_mat.reshape(
-
1
,
1
)
h
=
self
.sigmoid(
self
.p_data_mat.dot(theta))
J
=
-
1
*
(
1
/
m)
*
(np.log(h).T.dot(label_mat)
+
np.log(
1
-
h).T.dot(
1
-
label_mat))\
+
(reg
/
(
2
*
m))
*
np.
sum
(np.square(theta[
1
:]))
if
np.isnan(J[
0
]):
return
np.inf
return
J[
0
]
def
gradient_reg(
self
, theta, reg):
m
=
self
.label_mat.size
h
=
self
.sigmoid(
self
.p_data_mat.dot(theta.reshape(
-
1
,
1
)))
label_mat
=
self
.label_mat.reshape(
-
1
,
1
)
grad
=
(
1
/
m)
*
self
.p_data_mat.T.dot(h
-
label_mat)
+
(reg
/
m)
*
np.r_[[[
0
]], theta[
1
:].reshape(
-
1
,
1
)]
return
grad
def
gradient_descent_reg(
self
, alpha
=
0.01
, reg
=
0
, iterations
=
200
):
"""
逻辑回归梯度下降收敛函数
:param alpha: 学习率
:param reg:
:param iterations: 最大迭代次数
:return: 逻辑回归系数组
"""
m, n
=
self
.p_data_mat.shape
theta
=
np.zeros((n,
1
))
theta_set
=
[]
for
i
in
range
(iterations):
grad
=
self
.gradient_reg(theta, reg)
theta
=
theta
-
alpha
*
grad.reshape(
-
1
,
1
)
theta_set.append(theta)
return
theta, theta_set
def
plot_data_reg(
self
, x_label
=
None
, y_label
=
None
, neg_text
=
"negative"
, pos_text
=
"positive"
, thetas
=
None
):
neg
=
self
.label_mat
=
=
0
pos
=
self
.label_mat
=
=
1
fig1
=
plt.figure(figsize
=
(
12
,
8
))
ax1
=
fig1.add_subplot(
111
)
ax1.scatter(
self
.p_data_mat[neg][:,
1
],
self
.p_data_mat[neg][:,
2
], marker
=
"o"
, s
=
100
, label
=
neg_text)
ax1.scatter(
self
.p_data_mat[pos][:,
1
],
self
.p_data_mat[pos][:,
2
], marker
=
"+"
, s
=
100
, label
=
pos_text)
ax1.set_xlabel(x_label, fontsize
=
14
)
# 描绘逻辑回归直线(曲线)
if
isinstance
(thetas,
type
(np.array([]))):
x1_min, x1_max
=
self
.p_data_mat[:,
1
].
min
(),
self
.p_data_mat[:,
1
].
max
()
x2_min, x2_max
=
self
.p_data_mat[:,
2
].
min
(),
self
.p_data_mat[:,
2
].
max
()
xx1, xx2
=
np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
h
=
self
.sigmoid(
self
.poly.fit_transform(np.c_[xx1.ravel(), xx2.ravel()]).dot(thetas))
h
=
h.reshape(xx1.shape)
ax1.contour(xx1, xx2, h, [
0.5
], linewidths
=
3
)
ax1.legend(fontsize
=
14
)
plt.show()
@staticmethod
def
sigmoid(z):
return
1.0
/
(
1
+
np.exp(
-
z))
if
__name__
=
=
'__main__'
:
my_logistic_regression
=
MyLogisticRegression()
# my_logistic_regression.plot_data(x_label="线性不可分数据集")
thetas, theta_set
=
my_logistic_regression.gradient_descent_reg(alpha
=
0.5
, reg
=
0
, iterations
=
500
)
my_logistic_regression.plot_data_reg(thetas
=
thetas, x_label
=
"$\\lambda$ = {}"
.
format
(
0
))
thetas
=
np.zeros((my_logistic_regression.p_data_mat.shape[
1
],
1
))
# 未知错误,有大佬解决可留言
result
=
minimize(my_logistic_regression.cost_func_reg, thetas,
args
=
(
0
, ),
method
=
None
,
jac
=
my_logistic_regression.gradient_reg)
my_logistic_regression.plot_data_reg(thetas
=
result.x, x_label
=
"$\\lambda$ = {}"
.
format
(
0
))
|
以上就是python 实现逻辑回归的详细内容,更多关于python 实现逻辑回归的资料请关注我其它相关文章! 。
原文链接:https://www.cnblogs.com/aitiknowledge/p/12668794.html 。
最后此篇关于python 实现逻辑回归的文章就讲到这里了,如果你想了解更多关于python 实现逻辑回归的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
当我尝试加载库 Raster 时,我收到如下错误: 错误:inDL(x, as.logic(local), as.logic(now), ...) 中的“raster”的包或命名空间加载失败:无法加载
当我尝试加载库 Raster 时,我收到如下错误: 错误:inDL(x, as.logic(local), as.logic(now), ...) 中的“raster”的包或命名空间加载失败:无法加载
望着help section about_Comparison_Operators of PowerShell我是这样理解的: PS C:\> $false,$false -eq $true PS C
我刚刚修改了旧代码,现在似乎没有任何效果。请您指导我哪里出错了。 一些不起作用的事情是: 以前,焦点始终停留在屏幕上唯一的输入字段上。 (现在不行了),代码中的 if else 条件也不起作用。 On
请帮我找到一个使用普通 'ol javascript 的解决方案(我无法使用外部框架)。此外,CSS :hover 选择器不适用于现实世界的实现。 注册事件发生的事情设置所有调用最后注册事件数组项。
我想创建一个软件来为残障 child 交通规划公交路线(及其最佳载客量)。 这些总线具有以下规范: m 个座位(最多 7 个 - 因为有司机和助理) o 轮椅“座位”(最多 4 个) 固定的最大负载量
有人能帮我吗?似乎我的 for 逻辑根本不起作用,因为它一直在上午 12:00 返回我的开始时间 这是我的代码 Sub forlogic() Dim i As Single Dim t
我正在尝试设置 OR两个切片器过滤器之间的逻辑。两个切片器来自相同的数据集。以下是更多详细信息: 我的源表: 带切片器的视觉效果: 我的目标是,如果我从切片器 1 和切片器 2 中选择任何值,我的视觉
我有以下 C 语句: int res = x & (x ^ y); 有没有办法做同样的事情,但每次只使用一次x和y? 例如: x | (~x & y) == x | y 最佳答案 是的,通过扩展 xo
我正在创建 Azure 逻辑应用程序以将新的 Sharepoint 文件添加到 Azure Blob。 Sharepoint 由我的公司运行,我使用我的凭据登录来为逻辑应用程序创建 Sharepoin
我有一个问题要求为给定函数合成最简单的乘积表达式总和。基本上,如果 AB == CD,则函数为 1,否则为 0,结果如下: (!A && !B && !C && !D) || (!A && B &&
我正在尝试确定是否可以在不溢出的情况下计算两个 32 位整数的总和,同时仅使用某些按位运算符和其他运算符。因此,如果整数 x 和 y 可以相加而不会溢出,则以下代码应返回 1,否则返回 0。 ((((
处理乍一看需要许多嵌套 if 语句的复杂业务逻辑的好方法是什么? 例子: 折扣券。可能: 1a) 超值折扣 1b) 百分比折扣 2a) 正常折扣 2b) 累进折扣 3a) 需要访问优惠券 3b) 不需
假设我有一个“numbers”对象数组,其中包含“startNo”整数和“endNo”整数。 数组中可以有多个“数字”,我想获取一个包含修改对象的新数组,该数组仅具有不重叠的范围。 例如:如果数组有:
我在这个问题上遇到了困难。我正在使用 JavaScript。 我有一个文本区域,用于检测 @ 输入并将其位置存储在数组中。 var input = "@a @b @c" //textarea var
默认 IN 使用 OR 基本逻辑。有没有办法在范围内使用 AND 基本逻辑。 例如下面的查询 SELECT ItemId,CategoryID FROM ItemCategories WHERE Ca
我想在您将鼠标悬停在网站图像上时添加叠加层。我在这里实现了这个,它工作正常http://jsfiddle.net/stujLbjh/ 这是js代码: var divs = document.query
这个问题在这里已经有了答案: Which is faster: x>2 是否比 x>>31 快?换句话说,sar x, 2 是否比 sar x, 31 快?我做了一些简单的测试,他们似乎有相同的速度
我有grails criteriaQuery,我在这里再次检查OR逻辑,就像这样一个状态变量: or { eq("status", Status.ONE) eq("status",
我有grails criteriaQuery,我在这里再次检查OR逻辑,就像这样一个状态变量: or { eq("status", Status.ONE) eq("status",
我是一名优秀的程序员,十分优秀!