Vladimir
Vladimir

Reputation: 404

SFrame from numpy array

I would like to create an SFrame from a NumPy array.

What i want specifically is:

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]

If I do:

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

I get:

+--------------------------+
|            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]

I can get what I want if I convert the NumPy array to a Pandas DataFrame and from the Pandas DataFrame to an 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]

My question is:

How can I create an SFame from a NumPy array in the way that a Pandas DataFrame reads it (array NxM => DataFrame with N rows and M columns), but without using Pandas as an intermediate step?

Upvotes: 1

Views: 1111

Answers (1)

suvy
suvy

Reputation: 693

I also has this issue, I also find multi-indexing hard in SFrame.

may be silly fix but still workable;

from graphlab import SFrame,SArray

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

should result in something like this

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

Upvotes: 1

Related Questions