我的训练数据train
SFrame
看起来像这样,有 4 列(“Store”列在此 SFrame中不唯一):
+-------+------------+---------+-----------+
| Store | Date | Sales | Customers |
+-------+------------+---------+-----------+
| 1 | 2015-07-31 | 5263.0 | 555.0 |
| 2 | 2015-07-31 | 6064.0 | 625.0 |
| 3 | 2015-07-31 | 8314.0 | 821.0 |
| 4 | 2015-07-31 | 13995.0 | 1498.0 |
| 3 | 2015-07-20 | 4822.0 | 559.0 |
| 2 | 2015-07-10 | 5651.0 | 589.0 |
| 4 | 2015-07-11 | 15344.0 | 1414.0 |
| 5 | 2015-07-23 | 8492.0 | 833.0 |
| 2 | 2015-07-19 | 8565.0 | 687.0 |
| 10 | 2015-07-09 | 7185.0 | 681.0 |
+-------+------------+---------+-----------+
[986159 rows x 4 columns]
给定第二个store
SFrame
(“Store”列在此 SFrame 中是唯一的):
+-------+-----------+
| Store | StoreType |
+-------+-----------+
| 1 | c |
| 2 | a |
| 3 | a |
| 4 | c |
| 5 | a |
| 6 | a |
| 7 | a |
| 8 | a |
| 9 | a |
| 10 | a |
+-------+-----------+
我可以通过遍历中的每一行并找到适当的 from然后保留列和 ise来将适当的附加StoreType
到我的:train
SFrame
train
StoreType
store
SFrame.add_column()
store_type_col = []
for row in train:
row_store = row['Store']
row_storetype = next(i for i in store if i['Store'] == row_store)['StoreType']
store_type_col.append(row_storetype)
train.add_column(graphlab.SArray(store_type_col, dtype=str), name='StoreType')
要得到:
+-------+------------+---------+-----------+-----------+
| Store | Date | Sales | Customers | StoreType |
+-------+------------+---------+-----------+-----------+
| 1 | 2015-07-31 | 5263.0 | 555.0 | c
| 2 | 2015-07-31 | 6064.0 | 625.0 | a
| 3 | 2015-07-31 | 8314.0 | 821.0 | a
| 4 | 2015-07-31 | 13995.0 | 1498.0 | c
| 3 | 2015-07-20 | 4822.0 | 559.0 | a
| 2 | 2015-07-10 | 5651.0 | 589.0 | a
| 4 | 2015-07-11 | 15344.0 | 1414.0 | c
| 5 | 2015-07-23 | 8492.0 | 833.0 | a
| 2 | 2015-07-19 | 8565.0 | 687.0 | a
| 10 | 2015-07-09 | 7185.0 | 681.0 | a
+-------+------------+---------+-----------+-----------+
[986159 rows x 5 columns]
但我确信有一种更简单、更快捷的方法可以使用Graphlab
. 当前方法具有O(n*m)
n = no的最坏情况。中的行数train
,m = 否。中的行数m
。
想象一下,我store
SFrame
有 8 列要附加到train
. 上面的代码效率非常低。
我还能如何将数据列从一个 SFrame 附加到另一个 SFrame?(也欢迎 Pandas 解决方案)