2016-05-16 6 views
1

をnumpyの配列から行を削除するとnumpyの配列から行を削除します。私は1ギガバイトである特定のデータセットを有するときに繰り返さ未満n回</strong></p> <p><strong>原因その繰り返し未満n回

サイズで。 サンプル数は29.118.021で、クラス数は108.390です。

ただし、一部のクラスには1サンプルしかありません。または3つのサンプルなど...

問題: numpyの配列からN回以下の行/クラスを削除します。私はそれが1つの全体の夜のためのサーバで動作して去った後、上記のコードがあまりにもlong.Even取っている

train_x, train_y, test_x, test_id = loader.load() 

n_samples = train_y.shape[0] 
unique_labels, y_inversed = np.unique(train_y, return_inverse=True) 
label_counts = bincount(y_inversed) 
min_labels = np.min(label_counts) 

print "Total Rows ", n_samples 
print "unique_labels ", unique_labels.shape[0] 
print "label_counts ", label_counts[:] 
print "min labels ", min_labels 

unique_labels = unique_labels.astype(np.uint8) 
unique_amounts = np.empty(shape=unique_labels.shape, dtype=np.uint8) 
for u in xrange(0, unique_labels.shape[0]): 
    if u % 100 == 0: 
     print "Processed ", str(u) 
    for index in xrange(0, train_y.shape[0]): 
     if train_y[index] == unique_labels[u]: 
      unique_amounts[u] = unique_amounts[u] + 1 

for k in xrange(0, unique_amounts.shape[0]): 
    if unique_amounts[k] == 1: 
     print "\n" 
     print "value :", unique_amounts[k] 
     print "at ", k 

を失敗した

リファレンス XgBoost : The least populated class in y has only 1 members, which is too few

試みは、それもdidntの半分の処理に達する。


Loadメソッド

これは私のloadメソッドです。 私はそれを読み込んでデータフレームとして保持することができました。

def load(): 
    train = pd.read_csv('input/train.csv', index_col=False, header='infer') 
    test = pd.read_csv('input/test.csv', index_col=False, header='infer') 

    # drop useless columns 
    train.drop('row_id', axis=1, inplace=True) 

    acc = train["accuracy"].iloc[:].as_matrix() 
    x = train["x"].iloc[:].as_matrix() 
    y = train["y"].iloc[:].as_matrix() 
    time = train["time"].iloc[:].as_matrix() 
    train_y = train["place_id"].iloc[:].as_matrix() 

    #################################################################################### 
    acc = acc.reshape(-1, 1) 
    x = x.reshape(-1, 1) 
    y = y.reshape(-1, 1) 
    time = time.reshape(-1, 1) 
    train_y = train_y.reshape(-1, 1) 

    #################################################################################### 

    train_x = np.hstack((acc, x, y, time)) 

    #################################################################################### 

    acc = test["accuracy"].iloc[:].as_matrix() 
    x = test["x"].iloc[:].as_matrix() 
    y = test["y"].iloc[:].as_matrix() 
    time = test["time"].iloc[:].as_matrix() 
    test_id = test['row_id'].iloc[:].as_matrix() 

    ####################### 
    acc = acc.reshape(-1, 1) 
    x = x.reshape(-1, 1) 
    y = y.reshape(-1, 1) 
    time = time.reshape(-1, 1) 
    ####################### 

    test_x = np.hstack((acc, x, y, time)) 

    return train_x, train_y, test_x, test_id 
+0

クラスまたはラベルは、データフレームのためにのみ意味を持ち、 numpyの配列ではありません – Jacquot

+0

私はデータフレームとしてロードすることができます – KenobiShan

答えて

1

numpy_indexedパッケージ(免責事項:私はその作者だが)そのような操作を実行するのは非常に読みやすい道につながる多重機能を、含まれています

import numpy_indexed as npi 
samples_mask = npi.multiplicity(train_y) >= n_min 
filtered_train_y = train_y[samples_mask] 
+0

返事をありがとう、それは確かに同じ問題を持つ他の人を助ける! – KenobiShan

+0

このnumpy-indexed実装を使用します。ありがとう! – KenobiShan

+0

しかし、問題があります。 train_y配列から 'r'行を削除すると、train_xの行rを削除する必要がありますか? – KenobiShan

1

私はデータフレーム形式でデータを保存します。 そのようにすれば、pandasモジュールのいくつかの便利なメソッドを使うことができます。これはループ処理より速いはずです。

最初に、dfに関連付けられた別のラベルをdf['labels'].value_counts()とします。 (ラベルの列名は'labels'とする)

次に、データフレームにn_min行未満のラベルしか取得しないでください。

vc = df['labels'].value_counts() 
labels = vc[vc < n_min].index 
df.drop(labels, inplace=True) 

希望します。

+0

返信ありがとう、それは確かに同じ問題を持つ他の人を助ける! – KenobiShan

関連する問題