title: Target Encoding : Fill NaN generated in expanding mean encoded values
body: <p>I am working on multi-class classification problem having five classes in the target column. I have generated features for categorical variables using expanding mean encoding(Target encoding). The method is based on encoding categorical variable values with mean of target variable per value. </p>
  
  <p>This also results in some NaN values like in 'Transaction-Type_mean_target' column. </p>
  
  <ol>
  <li><p>What is the best way to fill these NaN values? Should I fill
  these with the column mean.</p></li>
  <li><p>How do I generate mean encoding for my test data as the
  target/Dependent variable 'Complaint-Status' is not present?</p></li>
  </ol>
  
  <p>Input data :</p>
  
  <p><a href="https://i.stack.imgur.com/vodSK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vodSK.png" alt="enter image description here"></a></p>
  
  <p>Generating mean encoding :</p>
  
  <pre><code>def add_feat_mean_encoding(col_list):
      """
          Expanding mean encoding 
      """
      for i in col_list:
          cumsum = train.groupby(i)['Complaint-Status'].cumsum() - train['Complaint-Status']
          cumcnt = train.groupby(i).cumcount()
          train[i+'_mean_target'] = cumsum/cumcnt
  
  cat_var = ['Transaction-Type','Complaint-reason','Company-response','Consumer-disputes']
  add_feat_mean_encoding(cat_var)
  </code></pre>