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

python回归

发布时间: 2022-01-14 08:52:01

python做回归用什么库

许多都可以,推荐numpy、matplotlib、pandas

㈡ 怎么看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))),似然函数是求最大值,一般是要稳定了才算最好。

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

㈢ 如何用python实现含有虚拟自变量的回归



参考资料:
DataRobot | Ordinary Least Squares in Python

DataRoboe | Multiple Regression using Statsmodels

AnalyticsVidhya | 7 Types of Regression Techniques you should know!



㈣ python该如何得到ols回归后的系数的t值

regstats(y1,x1,'linear','tstat');
c1=ans.tstat.beta(1,1);
beta1=ans.tstat.beta(2,1);
t1=ans.tstat.t(2,1);
c1是常数项,beta1是回归系数,t1就是beta1的t值,这里是单变量线性回归

㈤ 使用python进行回归分析,如何利用Excel的数据生成结果

用pandas+numpy应该可以实现

㈥ python 岭回归



所求参数是alpha的函数,比如记为f(alpha),f(alpha)随alpha的改变的轨迹就是岭迹。
实际计算中可选非常多的alpha值,做出一个岭迹图,看看这个图在取哪个值的时候变稳定了,
那就确定alpha值了,从而确定参数。

Ridge(alpha=1.0,fit_intercept=False)
model.fit(x,y)

这样就等于你算的,因为你numpy是用增广矩阵算的,所以应该将setfit_intercept=False
model.coef_
array([[1.06059732,0.48614918,0.44596739]])

㈦ python线性回归散点图怎么做

import matplotlib.pyplot as plt
plt.scatter(xdata,ydata)
(xdata,ydata为两个需要作图的数据集)

㈧ 请教怎样用Python做stepwise回归

所说所有的变量都是对象。 对象在python里,其实是一个指针,指向一个数据结构,数据结构里有属性,有方法。
对象通常就是指变量。从面向对象OO的概念来讲,对象是类的一个实例。在python里很简单,对象就是变量。
class A:
myname="class a"
上面就是一个类。不是对象
a=A()
这里变量a就是一个对象。
它有一个属性(类属性),myname,你可以显示出来
print a.myname
所以,你看到一个变量后面跟点一个小数点。那么小数点后面。

㈨ 回归方程表达式:如y=b+a1x1+a2x2+a3x3用python编程如何实现

样本中心点为横坐标是x的平均值,纵坐标是y的平均值。
回归方程所代表的直线经过样本中心点,单单给出方程表达式,应该是没法求样本中心点的!

热点内容
如何提高三星a7安卓版本 发布:2024-09-20 08:42:35 浏览:659
如何更换服务器网站 发布:2024-09-20 08:42:34 浏览:306
子弹算法 发布:2024-09-20 08:41:55 浏览:284
手机版网易我的世界服务器推荐 发布:2024-09-20 08:41:52 浏览:812
安卓x7怎么边打游戏边看视频 发布:2024-09-20 08:41:52 浏览:158
sql数据库安全 发布:2024-09-20 08:31:32 浏览:89
苹果连接id服务器出错是怎么回事 发布:2024-09-20 08:01:07 浏览:503
编程键是什么 发布:2024-09-20 07:52:47 浏览:653
学考密码重置要求的证件是什么 发布:2024-09-20 07:19:46 浏览:477
电脑主服务器怎么开机 发布:2024-09-20 07:19:07 浏览:728