2013-06-23 22 views
5

İki veri çerçevesine sahip makine öğrenmesi hesaplamaları yapıyorum - bunlardan biri faktörler ve hedef değerler için diğeridir. Hem eğitime hem de parçaları test etmeye ayırmam gerek. Bana yolu bulduğumu düşünüyorum ama daha zarif bir çözüm arıyorum. İşte benim kodudur:İki pandalık veri karesini aynı şekilde örnekleme

import pandas as pd 
import numpy as np 
import random 

df_source = pd.DataFrame(np.random.randn(5,2),index = range(0,10,2), columns=list('AB')) 
df_target = pd.DataFrame(np.random.randn(5,2),index = range(0,10,2), columns=list('CD')) 

rows = np.asarray(random.sample(range(0, len(df_source)), 2)) 

df_source_train = df_source.iloc[rows] 
df_source_test = df_source[~df_source.index.isin(df_source_train.index)] 
df_target_train = df_target.iloc[rows] 
df_target_test = df_target[~df_target.index.isin(df_target_train.index)] 

print('rows') 
print(rows) 
print('source') 
print(df_source) 
print('source train') 
print(df_source_train) 
print('source_test') 
print(df_source_test) 

---- düzenlenebilir - Eğer rows uzunluğunda len(df) bir boolean dizisi yaparsanız unutbu tarafından çözeltisi (midified) ---

np.random.seed(2013) 
percentile = .6 
rows = np.random.binomial(1, percentile, size=len(df_source)).astype(bool) 

df_source_train = df_source[rows] 
df_source_test = df_source[~rows] 
df_target_train = df_target[rows] 
df_target_test = df_target[~rows] 

cevap

6

, o zaman alabilirsiniz df[~rows] ile df[rows] ile True satır ve almak False satırlar:

import pandas as pd 
import numpy as np 
import random 
np.random.seed(2013) 

df_source = pd.DataFrame(
    np.random.randn(5, 2), index=range(0, 10, 2), columns=list('AB')) 

rows = np.random.randint(2, size=len(df_source)).astype('bool') 

df_source_train = df_source[rows] 
df_source_test = df_source[~rows] 

print(rows) 
# [ True True False True False] 

# if for some reason you need the index values of where `rows` is True 
print(np.where(rows)) 
# (array([0, 1, 3]),) 

print(df_source) 
#   A   B 
# 0 0.279545 0.107474 
# 2 0.651458 -1.516999 
# 4 -1.320541 0.679631 
# 6 0.833612 0.492572 
# 8 1.555721 1.741279 

print(df_source_train) 
#   A   B 
# 0 0.279545 0.107474 
# 2 0.651458 -1.516999 
# 6 0.833612 0.492572 

print(df_source_test) 
#   A   B 
# 4 -1.320541 0.679631 
# 8 1.555721 1.741279 
+0

teşekkürler! Bazı yüzdelik kullanmam gerektiğinden, satır değiştirilmiş çizgiyi değiştirdim = ... –

+0

Bu durumda, = satırları kullanabilirsiniz np.random.binomial (1, yüzdelik * 100, size = len (df_source)) '. – unutbu

+0

evet, çalışıyor, thanx –

İlgili konular