0

我正在尝试从 graphlab SFrame 访问多行并将它们转换为 numpy 数组。

我有一个 96000 行和 4096 列的数据库 fd,需要检索存储在 numpy 数组中的行号。我想出的方法很慢。我怀疑这是因为我在每次迭代时不断增加 sframe 的大小,但我不知道是否有任何方法可以预先分配值。我需要抓取 20000 行并且当前方法没有完成。

fd=fd.add_row_number()
print(indexes)
xs=fd[fd['id'] == indexes[0]] #create the first entry

t=time.time()
for i in indexes[1:]: #Parse through and get indeces
    t=time.time()
    xtemp=fd[fd['id'] == i]
    xs=xs.append(xtemp) #append the new row to the existing sframe
    print(time.time()-t)

xs.remove_column('id') #remove the ID Column
print(time.time()-t)
x_sub=xs.to_numpy() #Convert the sframe to numpy
4

1 回答 1

0

您可以将您的转换SFramepandas.DataFrame,从 中查找具有 id 的行indexes,删除 DataFrame 的列'id'并将此 DataFrame 转换为numpy.ndarray

例如:

import numpy as np

fd=fd.add_row_number()
df = fd.to_dataframe()
df_indexes = df[df.id.isin(indexes)]
df_indexes = df_indexes.drop(labels='id', axis=1)
x_sub = np.array(df_indexes)
于 2017-04-11T13:02:38.963 回答