[英]Type error: plotting linear regression
Python的新手,试图绘制两个列表的线性回归。 这是我到目前为止的内容:
#!/usr/bin/python
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from numpy import arange,array,ones#,random,linalg
from pylab import plot,show
from scipy import stats
from sys import argv
a = argv[1]
b = argv[2]
list1 = open(a)
list2 = open(b)
xi = list1.read().splitlines()
y = list2.read().splitlines()
slope, intercept, r_value, p_value, std_err = stats.linregress(xi,y)
print 'r value', r_value
line = slope*xi+intercept
plot(xi,line,'r-',xi,y,'o')
plt.savefig('myfig')
我收到以下错误:
Traceback (most recent call last):
File "plot.py", line 21, in <module>
slope, intercept, r_value, p_value, std_err = stats.linregress(xi,y)
File "/export/apps/Python/2.7.2-CentOS6.0/lib/python2.7/site-packages/scipy/stats/stats.py", line 3007, in linregress
xmean = np.mean(x,None)
File "/export/apps/Python/2.7.2-CentOS6.0/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2727, in mean
out=out, keepdims=keepdims)
File "/export/apps/Python/2.7.2-CentOS6.0/lib/python2.7/site-packages/numpy/core/_methods.py", line 66, in _mean
ret = umr_sum(arr, axis, dtype, out, keepdims)
TypeError: cannot perform reduce with flexible type
我的猜测是传递给linregress
的字符串列表不是犹太洁食。 尝试先将它们转换为浮点数:
xi = [float(xk) for xk in xi]
y = [float(yk) for yk in y]
slope, intercept, r_value, p_value, std_err = stats.linregress(xi,y)
根据xi
和y
,您可能必须先剥离一些空字符串,否则要清理输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.