如何适应线性回归负的数据?

问题描述 投票:-1回答:4

我想适合我的数据并提取他们的斜坡。我用的是线性回归。我的数据是一组包含负值时钟偏移值。这是我的代码:

from scipy import stats
import scipy
import matplotlib.pyplot as plt
plt.style.use('ggplot')
x= [1549808191, 1549808192, 1549808196, 1549808201, 1549808202, 1549808206, 1549808207, 1549808214, 1549808215, 1549808221, 1549808226, 1549808267, 1549808272, 1549808290, 1549808304, 1549808315, 1549808324, 1549808332, 1549808355, 1549808395, 1549808396]
y= ['7', '0', '0', '0', '-2', '4', '-3', '2', '0', '-1', '0', '-2', '-1', '-1','2', '-2', '1', '0', '0', '-1', '-2']
print(x)
print(y)
plt.plot(x,y,'o-')
plt.show()
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(x, y)
print(slope)

enter image description here然而,它给了我这个错误:

    ret = umr_sum(arr, axis, dtype, out, keepdims)
TypeError: cannot perform reduce with flexible type

那么如何解决这个错误吗?是线性回归拟合提取与PARAMS此类数据的最佳方式?

python linear-regression
4个回答
2
投票

在您进行选择,因为你的scipy.stats.linregress(x, y)值是字符串,问题看似来自y。你可以将它们转换使用map为整型和工作的事情如预期

# import commands here 
plt.style.use('ggplot')
x= [1549808191, 1549808192, 1549808196, 1549808201, 1549808202, 1549808206, 1549808207, 1549808214, 1549808215, 1549808221, 1549808226, 1549808267, 1549808272, 1549808290, 1549808304, 1549808315, 1549808324, 1549808332, 1549808355, 1549808395, 1549808396]
y= ['7', '0', '0', '0', '-2', '4', '-3', '2', '0', '-1', '0', '-2', '-1', '-1','2', '-2', '1', '0', '0', '-1', '-2']

plt.plot(x,y,'o-')
plt.show()
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(x, list(map(int, y)))
print("The slope is %s" %slope)

# The slope is -0.009607415773244879

enter image description here


1
投票

问题方案


正如在其他的答案是说,问题是,Y值都是字符串。它为你的作品部分,因为matplotlib全自动改变你的Y型字符串转换为数字。然而SciPy的库犯规。因此,您需要到您的列表转换成数字。见下文

from scipy import stats
import scipy
import matplotlib.pyplot as plt
plt.style.use('ggplot')
x= [1549808191, 1549808192, 1549808196, 1549808201, 1549808202, 1549808206, 1549808207, 1549808214, 1549808215, 1549808221, 1549808226, 1549808267, 1549808272, 1549808290, 1549808304, 1549808315, 1549808324, 1549808332, 1549808355, 1549808395, 1549808396]
y= ['7', '0', '0', '0', '-2', '4', '-3', '2', '0', '-1', '0', '-2', '-1', '-1','2', '-2', '1', '0', '0', '-1', '-2']
y = [float(i) for i in y]
print(x)
print(y)
plt.plot(x,y,'o-')
plt.show()
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(x, y)
print(slope)

0
投票

看起来你的y是一个字符串列表。你需要你的y以整数类型或浮点做了回归。


0
投票

更改y以数字的列表:y = [7, 0, 0, 0, -2, ...]

这样,它的工作原理

from scipy import stats
import scipy
import matplotlib.pyplot as plt
plt.style.use('ggplot')
x= [1549808191, 1549808192, 1549808196, 1549808201, 1549808202, 1549808206, 1549808207, 1549808214, 1549808215, 1549808221, 1549808226, 1549808267, 1549808272, 1549808290, 1549808304, 1549808315, 1549808324, 1549808332, 1549808355, 1549808395, 1549808396]
y= [1549808191, 1549808192, 1549808196, 1549808201, 1549808202, 1549808206, 1549808207, 1549808214, 1549808215, 1549808221, 1549808226, 1549808267, 1549808272, 1549808290, 1549808304, 1549808315, 1549808324, 1549808332, 1549808355, 1549808395, 1549808396]
print(y)
plt.plot(x,y,'o-')
plt.show()
slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(x, y)
print(slope)

返回1

© www.soinside.com 2019 - 2024. All rights reserved.