微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

返回bin坐标python

我有一个散点图,它被分类到箱子里.有4个箱子,两个弧线在中间用一条线分开.它逐行排序到列表列表中.例如.如果每个bin中有一个散点,则导出为:

x[0],y[0] = [(x,y)],[(x,y)],[(x,y)],[(x,y)]

问题是我必须手动导出每一行.因此,如果我想导出第二行散点图,我将更改为x [1],y [1]并将其添加到第1行.如果我有多行,这不是很有效.

如果我使用x,y我得到一个错误:ValueError:操作数无法与形状一起广播(70,)(10,)

是否有一种方法可以逐行导出整个数据集,或者使用相同的代码并遍历每一行.

import math
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

x = np.random.randint(80, size=(400, 10))
y = np.random.randint(80, size=(400, 10)) 

fig, ax = plt.subplots()
ax.grid(False)

plt.scatter(x[0],y[0])

#Creating the arcs
BIN_23_X = 50 
ang1 = 0, 50
ang2 = 100, 50
angle = math.degrees(math.acos(5.5/9.15))

#Adding the arcs and halfway line
Halfway = mpl.lines.Line2D((BIN_23_X,BIN_23_X), (0,100), c = 'black', lw = 2.5, alpha = 0.8, zorder = 1)
arc1 = mpl.patches.Arc(ang1, 65, 100, angle = 0, theta2 = angle, theta1 = 360-angle, lw = 2)
arc2 = mpl.patches.Arc(ang2, 65, 100, angle = 0, theta2 = 180+angle, theta1 = 180-angle, lw = 2)

ax.add_line(Halfway)
ax.add_patch(arc1)
ax.add_patch(arc2)

#Sorting the coordinates into bins   
def get_nearest_arc_vert(x, y, arc_vertices):
    err = (arc_vertices[:,0] - x)**2 + (arc_vertices[:,1] - y)**2
    nearest = (arc_vertices[err == min(err)])[0]
    return nearest

arc1v = ax.transData.inverted().transform(arc1.get_verts())
arc2v = ax.transData.inverted().transform(arc2.get_verts())

def classify_pointset(vx, vy):
    bins = {(k+1):[] for k in range(4)}
    for (x,y) in zip(vx, vy):
        nx1, ny1 = get_nearest_arc_vert(x, y, arc1v)
        nx2, ny2 = get_nearest_arc_vert(x, y, arc2v)

        if x < nx1:                         
            bins[1].append((x,y))
        elif x > nx2:                      
            bins[4].append((x,y))
        else:
            if x < BIN_23_X:               
                bins[2].append((x,y))
            else:                          
                bins[3].append((x,y))
    return bins

#Bins Output
bins_red  = classify_pointset(x[0], y[0])

all_points = [None] * 5
for bin_key in [1,2,3,4]:
    all_points[bin_key] = bins_red[bin_key] 

print(all_points) 

我想要分类垃圾箱的行是:

bins = classify_pointset(x[0], y[0])

我可以更改bin = classify_pointset(x [0],y [0])或添加循环来遍历每一行吗?

我正在努力实现的例子

如果我们使用数据的第一行来返回我将使用的分箱坐标:

bins = classify_pointset(x[0], y[0])

输出

[None, [(17, 20), (20, 36), (23, 30), (0, 65), (15, 35)], [(44, 57), (45, 3), (43, 0)], [(61, 21)], [(78, 23)]]

enter image description here

正如你所看到的那样,第一个箱[(17,20),(20,36),(23,30),(0,65),(15,35)]中有5个坐标.第2个[(44,57),(45,3),(43,0)]中的3个,第3个箱[(61,21)]中的1个和第4个箱子中的1个[(78,23)]

要返回第二行分档坐标,我会改变:

bins = classify_pointset(x [0],y [0])到bins = classify_pointset(x [1],y [1]).

然后我会将第二行附加到第一行来创建它:

0 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
1 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]

这个问题是我必须手动更改行和追加.例如

返回bin = classify_pointset(x [2],y [2])然后追加:

输出

2 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]

附加:

0 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
1 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]
2 = [(x,y)],[(x,y)],[(x,y)],[(x,y)]

我需要一些东西,将整个xy数据集按行格式返回到各自的bin中.而不是一次导出一行然后追加.

这有道理吗?

解决方法:

好的,这是我对此的抨击.有一些事情我不确定为什么你选择这样做,所以我改变了它们.随意让他们知道他们是否需要这样,我可以改回来.

首先,我不确定为什么你在classify_pointset中使用字典而不是列表.我已将其更改为使用列表,现在它已被零索引.

def classify_pointset(vx, vy):                                                                       
    bins = [[] for k in range(4)]                                                                    
    for (x,y) in zip(vx, vy):                                                                        
        nx1, ny1 = get_nearest_arc_vert(x, y, arc1v)                                                 
        nx2, ny2 = get_nearest_arc_vert(x, y, arc2v)                                                 

        if x < nx1:                                                                                  
            bins[0].append((x,y))                                                                    
        elif x > nx2:                                                                                
            bins[3].append((x,y))                                                                    
        else:                                                                                        
            if x < BIN_23_X:                                                                         
                bins[1].append((x,y))                                                                
            else:                                                                                    
                bins[2].append((x,y))                                                                

    return bins

接下来,我不确定你为什么要将你所拥有的字典变成列表.我上面做的改变实现了这一点.

现在,为了自动浏览每个bin,我们可以使用range函数和数组的形状.我正在手动计算第一个bin,以便我们可以将它与最终的bin进行比较并确保它匹配.

first_bin = classify_pointset(x[0],y[0])                                                             

#Bins Output                                                                                         
all_points = []                                                                                      

for i in range(x.shape[0]):                                                                          
    bins = classify_pointset(x[i],y[i])                                                              
    all_points.append(bins)                                                                          

print first_bin                                                                                      
print all_points[0] 

任何不在上面收听的代码都保留原样.如果有什么不清楚或不清楚,请告诉我.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐