1

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?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Vladimir
  • 369
  • 1
  • 3
  • 12
  • 1
    I can't confirm but SFrame takes a dictionary. Try: `{str(i): np.arange(16).reshape(4, 4).T[i] for i in range(4)}` – piRSquared Jul 25 '16 at 21:38
  • if SFrame(np.arange(16).reshape(4, 4)) works, try SFrame(pd.DataFrame(np.arange(16).reshape(4, 4)).values) .values will return the properly shaped np.array – nico Jul 26 '16 at 10:41
  • I want to return SFrame from numpy array. It works if I do: numpy => pandas DataFrame => SFrame But I would love to exclude intermediate step and have: numpy => SFrame – Vladimir Jul 26 '16 at 17:38

1 Answers1

1

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
suvy
  • 693
  • 6
  • 18