機械学習の独熱符号化

1252 ワード

  • 機械学習の独熱符号化
  • # -*- coding: utf-8 -*-
    """
    Created on Sat Dec  8 15:12:29 2018
    
    @author: muli
    """
    
    from sklearn.preprocessing import OneHotEncoder
    
    def test_OneHotEncoder():
        '''
          OneHotEncoder  
    
        :return: None
        '''
        X=[   [1,2,3,4,5],
              [5,4,3,2,1],
              [3,3,3,3,3,],
              [1,1,1,1,1] ]
        print("before transform:",X)
        encoder=OneHotEncoder(sparse=False)
        #  
    #    encoder=OneHotEncoder(sparse=True)
        encoder.fit(X)
        print("active_features_:",encoder.active_features_)
        print("feature_indices_:",encoder.feature_indices_)
        print("n_values_:",encoder.n_values_)
        print("-----------------------")
        print("after transform:",encoder.transform([[1,2,3,4,5]]))
        print("-----------------------")
    #    print("after transform:",encoder.transform([[1,1,1,1,1]]))
        print("after transform2:",encoder.transform(X))
        print("***********************")
        # [[1,1,1,1,1]]  
        print("after transform3:",encoder.fit_transform(X,[[1,1,1,1,1]]))
    
        
    if __name__=='__main__':
        test_OneHotEncoder() #   test_OneHotEncoder