创建矩阵时的 Python bug
我已经用 Python 编写了一段代码,用于根据数据创建转移概率矩阵,但我不断得到两个特定数据点的错误值。我花了几天时间试图找出问题所在,但没有成功。
关于代码:输入是csv文件中的4列。准备好数据后,前两列是新旧状态值。我需要计算每个旧状态值转移到新状态值的频率(基本上,每对 (x,y) 出现在数据的前两列中的频率)。这些列中的值从 0 到 99。在 trans_pr 矩阵中,我想获取一个数字(x,y)在数据中出现的频率,并将该数字放在 trans_pr 中相应坐标(x,y)处矩阵。由于值是从 0 到 99,每次它们出现在数据中时,我只需在该坐标处将 1 添加到矩阵即可。
问题:代码工作正常,但我总是在坐标 (:,29) 和 (:,58) 以及 (29,:) 和 (58;:) 处得到零,尽管在那里进行了观察。有时它似乎还会将此坐标处的数字添加到上一行。再说一遍,对我来说没有任何意义。
如果有人能提供帮助,我将非常感激。 (我是 Python 新手,因此代码可能效率低下,但只有 bug 相关。)
代码非常简单:
from numpy import *
import csv
my_data = genfromtxt('99c_test.csv', delimiter=',')
"""prepares data for further calculations"""
my_data1=zeros((len(my_data),4))
my_data1[1:,0]=100*my_data[1:,0]
my_data1[1:,1]=100*my_data[1:,3]
my_data1[1:,2]=my_data[1:,1]
my_data1[1:,3]=my_data[1:,2]
my_data2=my_data1
trans_pr=zeros((101,101))
print my_data2
"""fills the matrix with frequencies of observations"""
for i in range(len(my_data2)):
trans_pr[my_data2[i,1],my_data2[i,0]]=trans_pr[my_data2[i,1],my_data2[i,0]]+1
c = csv.writer(open("trpr1.csv", "wb"))
c.writerows(trans_pr)
您可以使用此输入测试代码(只需将其保存为 csv 文件):
p_cent,p_euro,p_euro_old,p_cent_old
0.01,1,1,0.28
0.01,1,1,0.29
0.01,1,1,0.3
0.01,1,1,0.28
0.01,1,1,0.29
0.01,1,1,0.3
0.01,1,1,0.57
0.01,1,1,0.58
0.01,1,1,0.59
0.01,1,1,0.6
I have written a code in Python to create a transition probability matrix from the data, but I keep getting wrong values for two specific data points. I have spent several days on trying to figure out the problem, but with no success.
About the code: The input is 4 columns in csv file. After preparation of the data, the first two columns are the new and old state values. I need to calculate how often each old state value transfers to a new one (basically, how often each pair (x,y) occurs in the first two columns of the data). The values in these columns are from 0 to 99. In the trans_pr matrix I want to get a number how often a pair (x,y) occurs in the data and have this number at the corresponding coordinates (x,y) in the trans_pr matrix. Since the values are from 0 to 99 I can just add 1 to the matrix at this coordinates each time they occur in the data.
The problem: The code works fine, but I always get zeros at coordinates (:,29) and (:,58) and (29,:) and (58;:) despite having observations there. It also sometimes seems to add the number at this coordinates to the previous line. Again, doesn't make any sense to me.
I would be very grateful if anyone could help. (I am new to Python, therefore the code is probably inefficient, but only the bug is relevant.)
The code is as simple as it can be:
from numpy import *
import csv
my_data = genfromtxt('99c_test.csv', delimiter=',')
"""prepares data for further calculations"""
my_data1=zeros((len(my_data),4))
my_data1[1:,0]=100*my_data[1:,0]
my_data1[1:,1]=100*my_data[1:,3]
my_data1[1:,2]=my_data[1:,1]
my_data1[1:,3]=my_data[1:,2]
my_data2=my_data1
trans_pr=zeros((101,101))
print my_data2
"""fills the matrix with frequencies of observations"""
for i in range(len(my_data2)):
trans_pr[my_data2[i,1],my_data2[i,0]]=trans_pr[my_data2[i,1],my_data2[i,0]]+1
c = csv.writer(open("trpr1.csv", "wb"))
c.writerows(trans_pr)
You can test the code with this input (just save it as csv file):
p_cent,p_euro,p_euro_old,p_cent_old
0.01,1,1,0.28
0.01,1,1,0.29
0.01,1,1,0.3
0.01,1,1,0.28
0.01,1,1,0.29
0.01,1,1,0.3
0.01,1,1,0.57
0.01,1,1,0.58
0.01,1,1,0.59
0.01,1,1,0.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来很像一个舍入问题。我假设例如 100*0.29 (作为浮点数)向下舍入(即截断),因此产生 28 而不是 29。在将它们用作数组索引。
更新:通过测试验证了我的猜想,甚至数字也如上所述 - 请参阅此处。
This sound very much like a rounding issue. I'd suppose that e.g. 100*0.29 (as a floating point number) is rounded downwards (i.e. truncated) and thus yields 28 instead of 29. Try rounding the numbers by yourself (i.e. a up/down rounding) before using them as an array index.
Update: Verified my conjecture by testing it, even the numbers are as described above - see here.
您可能会发现来自
numpy
的rint()
很有用。它将值四舍五入到最接近的整数(请参阅 numpy.rint() 文档)。您是否尝试过以下操作:You may find
rint()
useful, fromnumpy
. It rounds a value to its nearest integer (seenumpy.rint()
doc). Have you tried the following :