2016-08-16 3 views
0

私はPythonでClassesを使用するのが初めてで、ループでクラスを使用する方法や相談するリソースについていくつかのガイダンスを使用できます。ループでのクラスの使用 - Python

サンプルデータ:

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD')) 
df2 = pd.DataFrame(np.random.randint(0, 1, size=(100, 1)), columns=list('E')) 
df['E']= df2 

は、ここでは、クラスの外のコードです:

styles = [1, 3, 7] 

def train_model(X, y): 
    clf = LogisticRegression(random_state=0, C=1, penalty='l1') 
    clf.fit(X, y) 

for value in styles: 
    X = df[['A', 
      'B', 
      'C']][df['D']==value] 
    y = df['E'][df['D']==value] 
    train_model(X, y) 

私はそうのように、クラスにこれを翻訳する必要があります。

class BaseTrainer(object): 
""" Abstract class to define run order """ 

    def run(self): 
     self.import_training_data() 
     for value in [1, 3, 7]: 
      self.extract_variables(value) 
      self.train_model() 
      # I think there's a better way to do this 
      if value = 1: 
       pickle_model(self.model, self.model_file) 
      if value = 3: 
       pickle_model(self.model, self.model_file2) 
      if value = 7: 
       pickle_model(self.model, self.model_file3) 


class ModelTrainer(BaseTrainer): 
""" Class to train model for predicting Traits of Customers """ 

    def __init__(self): 
     self.model_file = '/wayfair/mnt/crunch_buckets/central/data_science/customer_style/train_modern.pkl' 
     self.model_file2 = '/wayfair/mnt/crunch_buckets/central/data_science/customer_style/train_traditional.pkl' 
     self.model_file3 = '/wayfair/mnt/crunch_buckets/central/data_science/customer_style/train_rustic.pkl' 

def import_training_data(self): 
    _execute_vertica_query('get_training_data') 

    self.df = _read_data('training_data.csv') 
    self.df.columns = [['CuID', 'StyID', 'StyName', 
    'Filter', 'PropItemsViewed', 'PropItemsOrdered', 'DaysSinceView']] 

def extract_variables(self, value): 
    # Take subset of columns for training purposes (remove CuID, Segment) 
    self.X = self.df[['PropItemsViewed', 'PropItemsOrdered', 
    'DaysSinceView']][df['StyID']==value] 

    y = self.df[['Filter']][df['StyID']==value] 
    self.y = y.flatten() 

def train_model(self): 
    self.model = LogisticRegression(C=1, penalty='l1') 

    self.model.fit(self.X, self.y) 

私は思いますそれを構造化するか、スタイルリストの3つの異なる値を実行するためのより良い方法が必要です。しかし、私はこれを改善するために何を探すべきかを知りません。任意の提案、ポインタなどが評価される!

答えて

0

それを行うためのエレガントな方法は、それが例えば

を改善することができ、設計については zip

def run(self): 
    self.import_training_data() 
    for value,model_file in zip([1, 3, 7],[self.model_file, self.model_file2, self.model_file3]): 
     self.extract_variables(value) 
     self.train_model() 

     pickle_model(self.model, model_file) 

使用して、同時に両方のリストを反復処理として、あなたのモデルファイルを定義することです直接リスト:

self.model_list = map(lambda x : os.path.join('/wayfair/mnt/crunch_buckets/central/data_science/customer_style',x),['train_modern.pkl','train_traditional','train_rustic.pkl']) 

与える:

def run(self): 
    self.import_training_data() 
    for value,model_file in zip([1, 3, 7],self.model_file_list): 
     self.extract_variables(value) 
     self.train_model() 
+0

これは素晴らしいです、ありがとう! – user2573355

0

あなただけそう

files = [self.model_file, self.model_file2, self.model_file3] 
values = [1 ,5 ,7] 
for n in range(len(value)): 
    pickle_model(self.model, files[n]) 

などのファイルを列挙可能性があり、これは質問に答えるでしょうか?

+0

パーティー。しかし私はより明確にできたかもしれない。私は "def run"にループを含める必要がありますか、クラス全体をループしてリストをループすることはできますか?基本的に、このタイプのループを含める最良の方法は何ですか?ここで、クラス内の複数の関数をループする必要がありますか?ありがとう! – user2573355

関連する問題