使用相同的边比较不同的 histogram2d binnings

发布于 2025-01-11 12:40:54 字数 2030 浏览 2 评论 0原文

我有一个如下所示的数据集:

tsne_results_x  tsne_results_y  team_id
0   -22.796648  -26.514051  107
1   11.985229   40.674446   107
2   -28.231720  -49.302216  107
3   31.942875   -14.427114  107
4   -46.436501  -7.750005   107
76  24.252718   -20.551889  8071
77  2.362172    17.170067   8071
78  7.212677    -9.056982   8071
79  -5.865472   -32.999077  8071

我想对 tsne_results_x 和 tsne_results_y 列进行分箱,为此我使用 numpy 函数 histogram2d

grid, xe, ye = np.histogram2d(df['tsne_results_x'], df['tsne_results_y'], bins=15)
gridx = np.linspace(min(df['tsne_results_x']),max(df['tsne_results_x']),15)
gridy = np.linspace(min(df['tsne_results_y']),max(df['tsne_results_y']),15)

plt.figure()
#plt.plot(x, y, 'ro')
plt.grid(True)

#plt.figure()
plt.pcolormesh(gridx, gridy, grid)
plt.colorbar()

plt.show()

在此处输入图像描述

但是,如您所见,我有一些 team_id在数据框中,我想将一个团队的各个数据箱与整个数据框进行比较。例如,对于一个团队,在一个特定的容器中,我想将其除以包括所有团队的总数。

因此,我认为在特定团队数据集上运行 histogram2d 并为整个数据集使用相同的行空间就可以解决问题。事实并非如此,因为 histogram2d 会对 one_team_df 进行不同的分类,因为数据具有不同的范围

one_team_df = df.loc[(df['team_id'] == str(299))]

grid_team, a, b = np.histogram2d(one_team_df['tsne_results_x'], one_team_df['tsne_results_y'], bins=15)



gridx = np.linspace(min(df['tsne_results_x']),max(df['tsne_results_x']),15)
gridy = np.linspace(min(df['tsne_results_y']),max(df['tsne_results_y']),15)

plt.figure()
#plt.plot(x, y, 'ro')
plt.grid(True)

#plt.figure()
plt.pcolormesh(gridx, gridy, grid_team)
#plt.plot(x, y, 'ro')
plt.colorbar()

plt.show()

在此处输入图像描述

我想知道如何制作这些两种表述具有可比性。是否可以运行 histogram2d 并给出 xedgesyedges ?这样我就可以使用整体分组的边缘对一个团队进行分组。

问候

I have a dataset that looks like this:

tsne_results_x  tsne_results_y  team_id
0   -22.796648  -26.514051  107
1   11.985229   40.674446   107
2   -28.231720  -49.302216  107
3   31.942875   -14.427114  107
4   -46.436501  -7.750005   107
76  24.252718   -20.551889  8071
77  2.362172    17.170067   8071
78  7.212677    -9.056982   8071
79  -5.865472   -32.999077  8071

I want to bin the tsne_results_x and tsne_results_y columns and for that I am using numpy function histogram2d

grid, xe, ye = np.histogram2d(df['tsne_results_x'], df['tsne_results_y'], bins=15)
gridx = np.linspace(min(df['tsne_results_x']),max(df['tsne_results_x']),15)
gridy = np.linspace(min(df['tsne_results_y']),max(df['tsne_results_y']),15)

plt.figure()
#plt.plot(x, y, 'ro')
plt.grid(True)

#plt.figure()
plt.pcolormesh(gridx, gridy, grid)
plt.colorbar()

plt.show()

enter image description here

However, as you can see, I have a few team_ids in the data frame and I would like to compare one team's individual bins to the whole data frame. For example, for one team, at one specific bin, I want to divide it by the total count that includes all the teams.

So, I thought that running histogram2d on a specific team dataset, using the same linespace for the whole dataset would do the trick. It does not, because the histogram2d will bin the one_team_df differently because the data has different ranges

one_team_df = df.loc[(df['team_id'] == str(299))]

grid_team, a, b = np.histogram2d(one_team_df['tsne_results_x'], one_team_df['tsne_results_y'], bins=15)



gridx = np.linspace(min(df['tsne_results_x']),max(df['tsne_results_x']),15)
gridy = np.linspace(min(df['tsne_results_y']),max(df['tsne_results_y']),15)

plt.figure()
#plt.plot(x, y, 'ro')
plt.grid(True)

#plt.figure()
plt.pcolormesh(gridx, gridy, grid_team)
#plt.plot(x, y, 'ro')
plt.colorbar()

plt.show()

enter image description here

I would like to know how do I make these two representations comparable. Is it possible to run histogram2d giving the xedges and yedges ? This way I could bin one team using the edges of the overall binning.

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书信已泛黄 2025-01-18 12:40:54

np.histomgram2d 文档

binsint 或 array_like 或 [int, int] 或 [array, array],可选
料仓规格:

如果是 int,则为二维的 bin 数量 (nx=ny=bins)。

如果是 array_like,则为二维的 bin 边缘 (x_edges=y_edges=bins)。

如果为 [int, int],则为每个维度中的 bin 数量 (nx, ny = bins)。

如果为 [array, array],则每个维度中的 bin 边缘 (x_edges, y_edges = bins)。

组合 [int, array] 或 [array, int],其中 int 是 bin 数量,array 是 bin 边缘。

这意味着您可以根据需要指定垃圾箱。例如:

grid_team, a, b = np.histogram2d(
    one_team_df['tsne_results_x'], one_team_df['tsne_results_y'], 
    bins=[np.linspace(-40,40,15), np.linspace(-40,40,15)]
)

documentation of np.histomgram2d

binsint or array_like or [int, int] or [array, array], optional
The bin specification:

If int, the number of bins for the two dimensions (nx=ny=bins).

If array_like, the bin edges for the two dimensions (x_edges=y_edges=bins).

If [int, int], the number of bins in each dimension (nx, ny = bins).

If [array, array], the bin edges in each dimension (x_edges, y_edges = bins).

A combination [int, array] or [array, int], where int is the number of bins and array is the bin edges.

This means you can specify the bins as you want. For instance:

grid_team, a, b = np.histogram2d(
    one_team_df['tsne_results_x'], one_team_df['tsne_results_y'], 
    bins=[np.linspace(-40,40,15), np.linspace(-40,40,15)]
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文