资源简介

此资源主要是python代码 涵盖了人脸识别,深度学习,卷积神经网络等一些列的算法程序

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-
“““
Created on Fri Jul 10 22:04:33 2015

@author: wepon
“““

import numpy as np

class DecisionTree:
    “““决策树使用方法:
    
        - 生成实例: clf = DecisionTrees(). 参数mode可选,ID3或C4.5,默认C4.5
        
        - 训练,调用fit方法: clf.fit(Xy).  Xy均为np.ndarray类型
                            
        - 预测,调用predict方法: clf.predict(X). X为np.ndarray类型
                                 
        - 可视化决策树,调用showTree方法 
    
    “““
    def __init__(selfmode=‘C4.5‘):
        self._tree = None
        
        if mode == ‘C4.5‘ or mode == ‘ID3‘:
            self._mode = mode
        else:
            raise Exception(‘mode should be C4.5 or ID3‘)
        
            
    
    def _calcEntropy(selfy):
        “““
        函数功能:计算熵
        参数y:数据集的标签
        “““
        num = y.shape[0]
        #统计y中不同label值的个数,并用字典labelCounts存储
        labelCounts = {}
        for label in y:
            if label not in labelCounts.keys(): labelCounts[label] = 0
            labelCounts[label] += 1
        #计算熵
        entropy = 0.0
        for key in labelCounts:
            prob = float(labelCounts[key])/num
            entropy -= prob * np.log2(prob)
        return entropy
    
    
    
    def _splitDataSet(selfXyindexvalue):
        “““
        函数功能:返回数据集中特征下标为index,特征值等于value的子数据集
        “““
        ret = []
        featVec = X[:index]
        X = X[:[i for i in range(X.shape[1]) if i!=index]]
        for i in range(len(featVec)):
            if featVec[i]==value:
                ret.append(i)
        return X[ret:]y[ret]
    
    
    def _chooseBestFeatureToSplit_ID3(selfXy):
        “““ID3
        函数功能:对输入的数据集,选择最佳分割特征
        参数dataSet:数据集,最后一列为label
        主要变量说明:
                numFeatures:特征个数
                oldEntropy:原始数据集的熵
                newEntropy:按某个特征分割数据集后的熵
                infoGain:信息增益
                bestInfoGain:记录最大的信息增益
                bestFeatureIndex:信息增益最大时,所选择的分割特征的下标
        “““
        numFeatures = X.shape[1]
        oldEntropy = self._calcEntropy(y)
        bestInfoGain = 0.0
        bestFeatureIndex = -1
        #对每个特征都计算一下infoGain,并用bestInfoGain记录最大的那个
        for i in range(numFeatures):        
            featList = X[:i]
            uniqueVals = set(featList)
            newEntropy = 0.0
            #对第i个特征的各个value,得到各个子数据集,计算各个子数据集的熵,
            #进一步地可以计算得到根据第i个特征分割原始数据集后的熵newEntropy
            for value in uniqueVals:
                sub_Xsub_y = self._splitDataSet(Xyivalue)
                prob = len(sub_y)/float(len(y))
                newEntropy += prob * self._calcEntropy(sub_y)  
            #计算信息增益,根据信息增益选择最佳分割特征
            infoGain = oldEntropy - newEntropy
            if (infoGain > bestInfoGain):
                bestInfoGain = infoGain
                bestFeatureIndex = i
        return bestFeatureIndex
        
    def _chooseBestFeatureToSplit_C45(selfXy):
        “““C4.5
            ID3算法计算的是信息增益,C4.5算法计算的是信息增益比,对上面ID3版本的函数稍作修改即可
        “““
        numFeatures = X.shape[1]
        ol

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-07-28 09:44  MachineLearning-master\
     文件         180  2017-07-28 09:44  MachineLearning-master\.gitignore
     目录           0  2017-07-28 09:44  MachineLearning-master\DecisionTree\
     文件        9244  2017-07-28 09:44  MachineLearning-master\DecisionTree\id3_c45.py
     文件        1304  2017-07-28 09:44  MachineLearning-master\DecisionTree\readme.md
     文件        3283  2017-07-28 09:44  MachineLearning-master\DecisionTree\treePlotter.py
     目录           0  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\
     目录           0  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\cnn_LeNet\
     文件       12645  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\cnn_LeNet\convolutional_mlp.py
     文件       20706  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\cnn_LeNet\convolutional_mlp_commentate.py
     目录           0  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\dive_into_keras\
     文件        2197  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\dive_into_keras\cnn-svm.py
     文件        2550  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\dive_into_keras\cnn.py
     文件         756  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\dive_into_keras\data.py
     文件        1432  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\dive_into_keras\get_feature_map.py
     文件        3300  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\dive_into_keras\README.md
     目录           0  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\FaceRecognition_CNN(olivettifaces)\
     文件     1182905  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\FaceRecognition_CNN(olivettifaces)\olivettifaces.gif
     文件       15554  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\FaceRecognition_CNN(olivettifaces)\train_CNN_olivettifaces.py
     文件        7042  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\FaceRecognition_CNN(olivettifaces)\use_CNN_olivettifaces.py
     目录           0  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\keras_usage\
     文件        5233  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\keras_usage\cnn.py
     文件         764  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\keras_usage\data.py
     文件        9005  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\keras_usage\README.md
     目录           0  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\mlp\
     文件       14181  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\mlp\mlp.py
     文件       17794  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\mlp\mlp_with_commentate.py
     文件         527  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\readme.md
     目录           0  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\Softmax_sgd(or logistic_sgd)\
     文件        9456  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\Softmax_sgd(or logistic_sgd)\logistic_sgd.py
     文件       19117  2017-07-28 09:44  MachineLearning-master\DeepLearning Tutorials\Softmax_sgd(or logistic_sgd)\logistic_sgd_commentate.py
............此处省略613个文件信息

评论

共有 条评论