当前位置:首页 » 编程语言 » python逻辑回归

python逻辑回归

发布时间: 2022-01-12 08:44:39

1. 怎么看逻辑回归的python代码

你把大于0,改成大于等于0,再重新试试。 另外你的逻辑弄得复杂了,好好想想,把逻辑简化一下。

如果你会画状态图,可以画个图给自己看看,好多逻辑是重复的。

比如if H3MRRFlag == 1: 象这样的语句是一需要的,直接删除。因为从python语法角度看,可能会有runtime error, 因为你没有初始化变量

2. python逻辑回归调用哪个包

可以使用机器学习,使用很方便(相当于别人早已经把具体过程做好了,像公式、模板一样自己代入数据就可以得到结果)

from sklearn.linear_model import LogisticRegression

3. Python sklearn 训练完逻辑回归模型之后,怎么使用他做预判

from sklearn import linear_model
建立模型
model = linear_model.LinearRegression()
model.fit(x_train,y_train)
评估模型
score = model.score(x_test, y_test)
预测模型
result = model.predict(x_test)

4. 怎么看python中逻辑回归输出的解释

以下为python代码,由于训练数据比较少,这边使用了批处理梯度下降法,没有使用增量梯度下降法。

##author:lijiayan##data:2016/10/27
##name:logReg.pyfrom numpy import *import matplotlib.pyplot as pltdef loadData(filename):
data = loadtxt(filename)
m,n = data.shape print 'the number of examples:',m print 'the number of features:',n-1 x = data[:,0:n-1]
y = data[:,n-1:n] return x,y#the sigmoid functiondef sigmoid(z): return 1.0 / (1 + exp(-z))#the cost functiondef costfunction(y,h):
y = array(y)
h = array(h)
J = sum(y*log(h))+sum((1-y)*log(1-h)) return J# the batch gradient descent algrithmdef gradescent(x,y):
m,n = shape(x) #m: number of training example; n: number of features x = c_[ones(m),x] #add x0 x = mat(x) # to matrix y = mat(y)
a = 0.0000025 # learning rate maxcycle = 4000 theta = zeros((n+1,1)) #initial theta J = [] for i in range(maxcycle):
h = sigmoid(x*theta)
theta = theta + a * (x.T)*(y-h)
cost = costfunction(y,h)
J.append(cost)

plt.plot(J)
plt.show() return theta,cost#the stochastic gradient descent (m should be large,if you want the result is good)def stocGraddescent(x,y):
m,n = shape(x) #m: number of training example; n: number of features x = c_[ones(m),x] #add x0 x = mat(x) # to matrix y = mat(y)
a = 0.01 # learning rate theta = ones((n+1,1)) #initial theta J = [] for i in range(m):
h = sigmoid(x[i]*theta)
theta = theta + a * x[i].transpose()*(y[i]-h)
cost = costfunction(y,h)
J.append(cost)
plt.plot(J)
plt.show() return theta,cost#plot the decision boundarydef plotbestfit(x,y,theta):
plt.plot(x[:,0:1][where(y==1)],x[:,1:2][where(y==1)],'ro')
plt.plot(x[:,0:1][where(y!=1)],x[:,1:2][where(y!=1)],'bx')
x1= arange(-4,4,0.1)
x2 =(-float(theta[0])-float(theta[1])*x1) /float(theta[2])

plt.plot(x1,x2)
plt.xlabel('x1')
plt.ylabel(('x2'))
plt.show()def classifyVector(inX,theta):
prob = sigmoid((inX*theta).sum(1)) return where(prob >= 0.5, 1, 0)def accuracy(x, y, theta):
m = shape(y)[0]
x = c_[ones(m),x]
y_p = classifyVector(x,theta)
accuracy = sum(y_p==y)/float(m) return accuracy

调用上面代码:

from logReg import *
x,y = loadData("horseColicTraining.txt")
theta,cost = gradescent(x,y)print 'J:',cost

ac_train = accuracy(x, y, theta)print 'accuracy of the training examples:', ac_train

x_test,y_test = loadData('horseColicTest.txt')
ac_test = accuracy(x_test, y_test, theta)print 'accuracy of the test examples:', ac_test

学习速率=0.0000025,迭代次数=4000时的结果:

似然函数走势(J = sum(y*log(h))+sum((1-y)*log(1-h))),似然函数是求最大值,一般是要稳定了才算最好。

从上面这个例子,我们可以看到对特征进行归一化操作的重要性。

5. 机器学习中,使用逻辑回归(python)做二分类时,recall,f1_score,support的含义是

假设预测目标为0和1
数据中1的个数为a,预测1的次数为b,预测1命中的次数为c
准确率 precision = c / b
召回率 recall = c / a
f1_score = 2 * precision * recall / (precision + recall)

6. python怎么实现逻辑回归的梯度下降和梯度上升法有区别吗

多数函数解不出导数得0的解析解.梯度下降法是种数值算法,一般可以用计算机求出很好的近似解

7. python怎么实现逻辑回归的梯度下降法

import sys

#Training data set
#each element in x represents (x0,x1,x2)
x = [(1,0.,3) , (1,1.,3) ,(1,2.,3), (1,3.,2) , (1,4.,4)]
#y[i] is the output of y = theta0 * x[0] + theta1 * x[1] +theta2 * x[2]
y = [95.364,97.217205,75.195834,60.105519,49.342380]

epsilon = 0.0001
#learning rate
alpha = 0.01
diff = [0,0]
max_itor = 1000
error1 = 0
error0 =0
cnt = 0
m = len(x)

#init the parameters to zero
theta0 = 0
theta1 = 0
theta2 = 0

while True:

cnt = cnt + 1

#calculate the parameters
for i in range(m):

diff[0] = y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] )

theta0 = theta0 + alpha * diff[0] * x[i][0]
theta1 = theta1 + alpha * diff[0]* x[i][1]
theta2 = theta2 + alpha * diff[0]* x[i][2]

#calculate the cost function
error1 = 0
for lp in range(len(x)):
error1 += ( y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] ) )**2/2

if abs(error1-error0) < epsilon:
break
else:
error0 = error1

print ' theta0 : %f, theta1 : %f, theta2 : %f, error1 : %f'%(theta0,theta1,theta2,error1)

print 'Done: theta0 : %f, theta1 : %f, theta2 : %f'%(theta0,theta1,theta2)

8. python做逻辑回归 怎么把导入的数据分成x,y

简介
本例子是通过对一组逻辑回归映射进行输出,使得网络的权重和偏置达到最理想状态,最后再进行预测。其中,使用GD算法对参数进行更新,损耗函数采取交叉商来表示,一共训练10000次。
2.python代码
#!/usr/bin/python

import numpy
import theano
import theano.tensor as T
rng=numpy.random

N=400
feats=784
# D[0]:generate rand numbers of size N,element between (0,1)
# D[1]:generate rand int number of size N,0 or 1
D=(rng.randn(N,feats),rng.randint(size=N,low=0,high=2))
training_steps=10000

# declare symbolic variables
x=T.matrix('x')
y=T.vector('y')
w=theano.shared(rng.randn(feats),name='w') # w is shared for every input
b=theano.shared(0.,name='b') # b is shared too.

print('Initial model:')
print(w.get_value())
print(b.get_value())

# construct theano expressions,symbolic
p_1=1/(1+T.exp(-T.dot(x,w)-b)) # sigmoid function,probability of target being 1
prediction=p_1>0.5
xent=-y*T.log(p_1)-(1-y)*T.log(1-p_1) # cross entropy
cost=xent.mean()+0.01*(w**2).sum() # cost function to update parameters
gw,gb=T.grad(cost,[w,b]) # stochastic gradient descending algorithm

#compile
train=theano.function(inputs=[x,y],outputs=[prediction,xent],updates=((w,w-0.1*gw),(b,b-0.1*gb)))
predict=theano.function(inputs=[x],outputs=prediction)

# train
for i in range(training_steps):
pred,err=train(D[0],D[1])

print('Final model:')
print(w.get_value())
print(b.get_value())
print('target values for D:')
print(D[1])
print('prediction on D:')
print(predict(D[0]))

print('newly generated data for test:')
test_input=rng.randn(30,feats)
print('result:')
print(predict(test_input))

3.程序解读
如上面所示,首先导入所需的库,theano是一个用于科学计算的库。然后这里我们随机产生一个输入矩阵,大小为400*784的随机数,随机产生一个输出向量大小为400,输出向量为二值的。因此,称为逻辑回归。
然后初始化权重和偏置,它们均为共享变量(shared),其中权重初始化为较小的数,偏置初始化为0,并且打印它们。
这里我们只构建一层网络结构,使用的激活函数为logistic sigmoid function,对输入量乘以权重并考虑偏置以后就可以算出输入的激活值,该值在(0,1)之间,以0.5为界限进行二值化,然后算出交叉商和损耗函数,其中交叉商是代表了我们的激活值与实际理论值的偏离程度。接着我们使用cost分别对w,b进行求解偏导,以上均为符号表达式运算。
接着我们使用theano.function进行编译优化,提高计算效率。得到train函数和predict函数,分别进行训练和预测。
接着,我们对数据进行10000次的训练,每次训练都会按照GD算法进行更新参数,最后我们得到了想要的模型,产生一组新的输入,即可进行预测。

9. python逻辑回归结果怎么看

假设预测目标为0和1 数据中1的个数为a,预测1的次数为b,预测1命中的次数为c 准确率 precision = c / b 召回率 recall = c / a f1_score = 2 * precision * recall / (precision + recall)

10. python sklearn逻辑回归怎么调参

from sklearn import linear_model#线性回归clf = linear_model.LinearRegression()#训练clf.fit ([[0, 0], [1, 1], [2, 2]], [0, 1, 2])#表达式参数clf.coef_#测试improt numpy as npx = np.array([1,1])y = x.dot(clf.coef_)

热点内容
ftp连接工具安装 发布:2024-09-19 09:56:57 浏览:192
nba2kol2安卓手游怎么下 发布:2024-09-19 09:55:28 浏览:11
服务器换位置了ip地址怎么换 发布:2024-09-19 09:33:50 浏览:798
javarest 发布:2024-09-19 09:28:43 浏览:753
密码子的原料是什么 发布:2024-09-19 09:11:42 浏览:348
半夜编程 发布:2024-09-19 09:11:36 浏览:104
海康威视存储卡质量如何 发布:2024-09-19 08:55:35 浏览:941
python3默认安装路径 发布:2024-09-19 08:50:22 浏览:517
环卫视频拍摄脚本 发布:2024-09-19 08:35:44 浏览:419
sqlserveronlinux 发布:2024-09-19 08:16:54 浏览:257