Path: blob/master/trees/__pycache__/tree.cpython-36.pyc
2585 views
3
ţ�[C � @ sR d dl ZG dd� d�Zdd� Zdd� Zdd � Zd
d� Zdd
� Zddd�ZegZ dS )� Nc @ s: e Zd ZdZddd�Zdd � Zd
d� Zdd
� Zdd� ZdS )�Treea
Classification tree using information gain with entropy as impurity
Parameters
----------
max_features : int or None, default None
The number of features to consider when looking for the best split,
None uses all features
min_samples_split : int, default 10
The minimum number of samples required to split an internal node
max_depth : int, default 3
Maximum depth of the tree
minimum_gain : float, default 1e-7
Minimum information gain required for splitting
� N�H�����z>�
c C s || _ || _|| _|| _d S )N)� max_depth�max_features�minimum_gain�min_samples_split)�selfr r r r � r �//Users/mingyuliu/machine-learning/trees/tree.py�__init__ s z
Tree.__init__c C s� t j|�jd | _| jdks,| j|jd kr8|jd | _t j|jd �| _t||| j| j | j| j
| j| j|jd � | _| jt j| j� _| S )z4pass in the 2d-array dataset and the response columnr N� )
�np�unique�shape�n_classr �zeros�feature_importance�_create_decision_treer r r �tree�sum)r
�X�yr r r �fit s
zTree.fitc C s | j |�}tj|dd�}|S )Nr )�axis)�
predict_probar �argmax)r
r �proba�predr r r �predict3 s
zTree.predictc C sP t j|jd | jf�}x4t|jd �D ]"}| j||d d �f | j�||<