【发布时间】:2016-07-25 20:48:24
【问题描述】:
我想从 NumPy 数组创建一个 SFrame。
我特别想要的是:
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 DataFrame 转换为SFrame,我可以获得我想要的:
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 需要一本字典。试试:
{str(i): np.arange(16).reshape(4, 4).T[i] for i in range(4)} -
如果 SFrame(np.arange(16).reshape(4, 4)) 有效,请尝试 SFrame(pd.DataFrame(np.arange(16).reshape(4, 4)).values ) .values 将返回形状正确的 np.array
-
我想从 numpy 数组返回 SFrame。如果我这样做,它会起作用:numpy => pandas DataFrame => SFrame 但我想排除中间步骤并拥有:numpy => SFrame
标签: python numpy pandas dataframe sframe