gpt4 book ai didi

python - 来自 numpy 数组的 SFrame

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:57 26 4
gpt4 key购买 nike

我想创建一个SFrame来自 NumPy 数组。

我具体想要的是:

np.arange(16).reshape(4, 4) 

=>

+----+----+----+----+
| 0 | 1 | 2 | 3 |
+----+----+----+----+
| 0 | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 |
+----+----+----+----+
[4 rows x 4 columns]

如果我这样做:

print SFrame(np.arange(16).reshape(4, 4))

我得到:

+--------------------------+
| X1 |
+--------------------------+
| [0.0, 1.0, 2.0, 3.0] |
| [4.0, 5.0, 6.0, 7.0] |
| [8.0, 9.0, 10.0, 11.0] |
| [12.0, 13.0, 14.0, 15.0] |
+--------------------------+
[4 rows x 1 columns]

如果我将 NumPy 数组转换为 Pandas DataFrame 并从 Pandas 中得到我想要的结果> DataFrameSFrame:

print SFrame(pd.DataFrame(np.arange(16).reshape(4, 4)))

+----+----+----+----+
| 0 | 1 | 2 | 3 |
+----+----+----+----+
| 0 | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 |
+----+----+----+----+
[4 rows x 4 columns]

我的问题是:

如何以 Pandas DataFrame 读取它的方式从 NumPy 数组创建 SFame (数组 NxM => DataFrame,具有 N 行和 M 列),但不使用 Pandas 作为中间步骤?

最佳答案

我也有这个问题,我也发现 SFrame 中的多重索引很难。

可能是愚蠢的修复,但仍然可行;

from graphlab import SFrame,SArray

data=np.arange(16).reshape(4, 4).T
sf=SFrame(map(SArray,data)

应该会产生类似这样的结果

X1  X2  X3  X4
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

关于python - 来自 numpy 数组的 SFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com