<s> from __future__ import division <EOL> import numpy as np <EOL> import scipy as sp <EOL> import pylab as py <EOL> def pGini ( y ) : <EOL> ty = y . reshape ( - <NUM_LIT:1> , ) . tolist ( ) <EOL> label = set ( ty ) <EOL> sum = <NUM_LIT:0> <EOL> num_case = y . shape [ <NUM_LIT:0> ] <EOL> for i in label : <EOL> sum += ( np . count_nonzero ( y == i ) / num_case ) ** <NUM_LIT:2> <EOL> return <NUM_LIT:1> - sum <EOL> class DTC : <EOL> def __init__ ( self , X , y , property = None ) : <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> self . X = np . array ( X ) . transpose ( ) <EOL> self . y = np . array ( y ) . flatten ( <NUM_LIT:1> ) <EOL> self . feature_dict = { } <EOL> self . labels , self . y = np . unique ( y , return_inverse = True ) <EOL> self . DT = list ( ) <EOL> if ( property == None ) : <EOL> self . property = np . zeros ( ( self . X . shape [ <NUM_LIT:1> ] , <NUM_LIT:1> ) ) <EOL> else : <EOL> self . property = property <EOL> for i in range ( self . X . shape [ <NUM_LIT:1> ] ) : <EOL> self . feature_dict . setdefault ( i ) <EOL> self . feature_dict [ i ] = np . unique ( self . X [ : , i ] ) <EOL> if ( self . X . shape [ <NUM_LIT:0> ] != self . y . size ) : <EOL> print "<STR_LIT>" <EOL> assert self . X . shape [ <NUM_LIT:0> ] == self . y . size <EOL> for i in range ( self . X . shape [ <NUM_LIT:1> ] ) : <EOL> for j in self . feature_dict [ i ] : <EOL> pass <EOL> pass <EOL> def Gini ( self , X , y , k , k_v ) : <EOL> if ( self . property [ k ] == <NUM_LIT:0> ) : <EOL> c1 = ( X [ X [ : , k ] == k_v ] ) . shape [ <NUM_LIT:0> ] <EOL> c2 = ( X [ X [ : , k ] != k_v ] ) . shape [ <NUM_LIT:0> ] <EOL> D = y . shape [ <NUM_LIT:0> ] <EOL> return c1 * pGini ( y [ X [ : , k ] == k_v ] ) / D + c2 * pGini ( y [ X [ : , k ] != k_v ] ) / D <EOL> else : <EOL> c1 = ( X [ X [ : , k ] >= k_v ] ) . shape [ <NUM_LIT:0> ] <EOL> c2 = ( X [ X [ : , k ] < k_v ] ) . shape [ <NUM_LIT:0> ] <EOL> D = y . shape [ <NUM_LIT:0> ] <EOL> return c1 * pGini ( y [ X [ : , k ] >= k_v ] ) / D + c2 * pGini ( y [ X [ : , k ] < k_v ] ) / D <EOL> pass <EOL> def makeTree ( self , X , y ) : <EOL> min = <NUM_LIT> <EOL> m_i , m_j = <NUM_LIT:0> , <NUM_LIT:0> <EOL> if ( np . unique ( y ) . size <= <NUM_LIT:1> ) : <EOL> return ( y [ <NUM_LIT:0> ] ) <EOL> for i in range ( self . X . shape [ <NUM_LIT:1> ] ) : <EOL> for j in self . feature_dict [ i ] : <EOL> p = self . Gini ( X , y , i , j ) <EOL> if ( p < min ) : <EOL> min = p <EOL> m_i , m_j = i , j <EOL> if ( min == <NUM_LIT:1> ) : <EOL> return ( y [ <NUM_LIT:0> ] ) <EOL> left = [ ] <EOL> righy = [ ] <EOL> if ( self . property [ m_i ] == <NUM_LIT:0> ) : <EOL> left = self . makeTree ( X [ X [ : , m_i ] == m_j ] , y [ X [ : , m_i ] == m_j ] ) <EOL> right = self . makeTree ( X [ X [ : , m_i ] != m_j ] , y [ X [ : , m_i ] != m_j ] ) <EOL> else : <EOL> left = self . makeTree ( X [ X [ : , m_i ] >= m_j ] , y [ X [ : , m_i ] >= m_j ] ) <EOL> right = self . makeTree ( X [ X [ : , m_i ] < m_j ] , y [ X [ : , m_i ] < m_j ] ) <EOL> return [ ( m_i , m_j ) , left , right ] <EOL> def train ( self ) : <EOL> self . DT = self . makeTree ( self . X , self . y ) <EOL> print self . DT <EOL> def pred ( self , X ) : <EOL> X = np . array ( X ) . transpose ( ) <EOL> result = np . zeros ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> for i in range ( X . shape [ <NUM_LIT:0> ] ) : <EOL> tp = self . DT <EOL> while ( type ( tp ) is list ) : <EOL> a , b = tp [ <NUM_LIT:0> ] <EOL> if ( self . property [ a ] == <NUM_LIT:0> ) : <EOL> if ( X [ i ] [ a ] == b ) : <EOL> tp = tp [ <NUM_LIT:1> ] <EOL> else : <EOL> tp = tp [ <NUM_LIT:2> ] <EOL> else : <EOL> if ( X [ i ] [ a ] >= b ) : <EOL> tp = tp [ <NUM_LIT:1> ] <EOL> else : <EOL> tp = tp [ <NUM_LIT:2> ] <EOL> result [ i ] = self . labels [ tp ] <EOL> return result <EOL> pass </s>
