2017-11-21 3 views
0

2つのテキストファイルにトレーニングデータと対応するラベル(整数1,2、...、9)が与えられています。両方のテキストファイルは一連の数字です。トレーニングセットにおけるラベルに基づいたトレーニングデータのサブセットの抽出

最初の500の番号は、最初のデータ点に対応する、第二の500の番号は、第2のデータ点に対応する、等

Iラベル2またはを有するトレーニング・ポイントのサブセットを抽出しますラベル3。私の実装は非常に遅いです:

import numpy as np 

ytrain_old = np.genfromtxt('TrainLabels.txt') 
Xtrain_old = np.genfromtxt('Train.txt') 

Xtrain = [] 
ytrain = [] 

for i in range(10000): 
    if (ytrain_old[i]==2) or (ytrain_old[i]==3): 
     ytrain.append(ytrain_old[i]) 
     Xtrain.append([Xtrain_old[i*500:(i+1)*500]]) 

これを行うにはどうすればよいでしょうか?私はそれを実際にパンダのデータフレームとして持つことを望んでいます。

+0

あなたは何をしているのですか'' Xtrain [i * 700:(i + 1)* 700] 'の中に入っていますか? – MaxU

+0

ああ、XtrainではなくXtrain_oldでなければなりません。私がしようとしているのは、2または3の各ラベルに対して、対応するテストデータ(つまり、対応する500個の数字)にアクセスしたいということです@MaxU – denmarksucks

+0

ラベルを追加することはできますか? ndf = pd.concat([Xtrain_old、ytrain_old]、1) 'それで' train = ndf.groupby( 'y_train_column_header')。head(500) 'ブール値を列挙する' train = train [train ['y_train_column_header']。isin ([2,3])]後にy_trainとx_trainに分割できます。 – Dark

答えて

0

何約:

sel = np.logical_or(ytrain_old == 2, ytrain_old == 3) 
Xtrain = Xtrain_old.reshape((-1,500))[sel] 
ytrain = ytrain_old[sel] 
0

まず、xtrainとytrainをマージします。そのために我々はあなたのXフレームを旋回させる必要があります。

xtrain_old = pd.Series(np.random.random(10000)).to_frame() 
ytrain_old = pd.Series(np.random.randint(5, size=20)) 

xtrain_old['column_names'] = 'feature_'+ (xtrain_old.index%500).astype(str) 
xtrain_old.index = np.floor(xtrain_old.index/500).astype(int) 
xtrain_old = xtrain_old.pivot(columns='column_names') 
xtrain_old.columns = xtrain_old.columns.droplevel() 

今、私たちはラベルマージすることができます:

ytrain_old = ytrain_old.rename('label') 
df = pd.concat([xtrain_old, ytrain_old], axis=1) 

を我々は気にラベルを持つすべての行を選択します。

df_selected = df.loc[df['label'].isin([2,3])] 
関連する問題