python回归结果输出,python数据回归

本文目录一览:

关于python简单线性回归

线性回归:

设x,y分别为一组数据,代码如下

import matplotlib.pyplot as plt

import numpy as np

ro=np.polyfit(x,y,deg=1) #deg为拟合的多项式的次数(线性回归就选1)

ry=np.polyval(ro,x) #忘记x和ro哪个在前哪个在后了。。。

print ro #输出的第一个数是斜率k,第二个数是纵截距b

plt.scatter(x,y)

plt.plot(x,ry)

python逻辑回归结果怎么看

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

python 的LinearRegression包,怎么导出回归模型公式?

线性回归是机器学习算法中最简单的算法之一,它是监督学习的一种算法,主要思想是在给定训练集上学习得到一个线性函数,在损失函数的约束下,求解相关系数,最终在测试集上测试模型的回归效果。

也就是说 LinearRegression 模型会构造一个线性回归公式

y’ = w^T x + b

,其中 w 和 x 均为向量,w 就是系数,截距是 b,得分是根据真实的 y 值和预测值 y’ 计算得到的。

python回归模型保存

1、首先需要使用公式将回归结果计算出来。

2、其次选择回归。

3、最后将其另存为,另存为到word中就可以保存了。Python由荷兰数学和计算机科学研究学会的吉多范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。Python提供了高效的高级数据结构,还能简单有效地面向对象编程。

python怎么保存回归结果

1、需要使用公式将回归结果计算出来。

2、选择回归。

3、结果将其另存为,另存为到word中就可以保存了。

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

下图为计算结果,可以看到训练集的准确率为73%,测试集的准确率为78%。

这个时候,我去看了一下数据集,发现没个特征的数量级不一致,于是我想到要进行归一化处理:

归一化处理句修改列loadData(filename)函数:

def 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]

max = x.max(0)

min = x.min(0)

x = (x – min)/((max-min)*1.0)     #scaling    y = data[:,n-1:n]    return x,y

在没有归一化的时候,我的学习速率取了0.0000025(加大就会震荡,因为有些特征的值很大,学习速率取的稍大,波动就很大),由于学习速率小,迭代了4000次也没有完全稳定。现在当把特征归一化后(所有特征的值都在0~1之间),这样学习速率可以加大,迭代次数就可以大大减少,以下是学习速率=0.005,迭代次数=500的结果:

此时的训练集的准确率为72%,测试集的准确率为73%

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

原创文章,作者:XXEG,如若转载,请注明出处:https://www.506064.com/n/139400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
XXEGXXEG
上一篇 2024-10-04 00:22
下一篇 2024-10-04 00:22

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29

发表回复

登录后才能评论