创建矩阵时的Python错误(Python bug when creating matrices)

编程入门 行业动态 更新时间:2024-10-21 19:33:47
创建矩阵时的Python错误(Python bug when creating matrices)

我已经用Python编写了一个代码来创建数据的转换概率矩阵,但是我一直在为两个特定的数据点获取错误的值。 我花了好几天的时间试图找出问题,但没有成功。

关于代码:输入是csv文件中的4列。 在准备好数据之后,前两列是新旧状态值。 我需要计算每个旧状态值转移到新状态的频率(基本上,每个对(x,y)在数据的前两列中出现的频率)。 这些列中的值是从0到99.在trans_pr矩阵中,我想获得一个数字,在数据中出现pair(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

最满意答案

这听起来很像一个四舍五入的问题。 我假设例如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.

更多推荐

本文发布于:2023-07-06 08:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   错误   Python   matrices   bug

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!