<pre name="code" class="python">#alpha error theta
#虽然是python 标签 但缺少
sigmoid <- function(x){
return (1.0/(1+exp(-x)))
}
#用什么样的优化算法 1 gradient descent 2 stochastic gradient descent
# 3
controlnumber = 3
#set max iterator times
maxIter = 200
dir = "E:/A_Ten algorithm of machine learning/logistic.txt"
#运行结果存储
pdf("E:/A_Ten algorithm of machine learning/logistic.pdf")
original.data = read.table(file = dir, header = FALSE)
head(original.data)
#original.data = as.matrix(original.data)
#data x
s = rep(1,nrow(original.data))
train_x = cbind(s,original.data[,c(1,2)])
#data labels y 0/1
train_y = original.data[,3]
# parameter theta
theta = rep(1,length(train_x[1,]))
#Conversion train_x to matrix
train_x = as.matrix(train_x)
#Conversion theta to matrix
theta = as.matrix(theta)
#plot points
plot(train_x[,2],train_x[,3],col = "red",xlab = "x1",ylab = 'x2')
for( i in 1: 100){
if(train_y[i] == 1){
points(train_x[i,2],train_x[i,3],col = "RED")
R语言实现 logistic 模型
使用R语言实现Logistic回归
最新推荐文章于 2025-01-04 01:00:00 发布
该博客通过R语言展示了如何实现逻辑回归算法,包括sigmoid函数的定义、数据读取、训练集创建、梯度下降法(包括普通梯度、随机梯度和平滑随机梯度)的实现,并在二维平面上绘制了训练数据点和预测决策边界。

最低0.47元/天 解锁文章
1127

被折叠的 条评论
为什么被折叠?



