zoukankan      html  css  js  c++  java
  • 20171029机器学习之特征值选择

    在我们对于有很多特征值数据处理时,往往需要找到特征值对于结果Y权重最大的几个,便于做降维。

    于是我们可以用以下这段代码:

    GitHub:https://github.com/chenjunhaolefa/AI/blob/master/MachineLearning/FeatureSelection.py

    # coding=utf-8
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.datasets import make_classification
    from sklearn.ensemble import ExtraTreesClassifier
    
    #解决画图产生的中文乱码问题
    mpl.rcParams['font.sans-serif']=[u'simHei']
    mpl.rcParams['axes.unicode_minus']=False
    
    #测试一下创建了X和Y的矩阵
    X = np.random.random((3,3))
    Y = np.arange(1,4)
    print X,Y
    
    def FeatureSelection(X,Y):
        # Build a classification task using 3 informative features
        '''
        X, Y = make_classification(n_samples=10,  #该函数负责创建一个自定义的矩阵(X->Y)的关系
                                   n_features=10,
                                   n_informative=3,
                                   n_redundant=0,
                                   n_repeated=0,
                                   n_classes=2,
                                   random_state=0,
                                   shuffle=False)'''
        # Build a forest and compute the feature importances
        forest = ExtraTreesClassifier(n_estimators=10,random_state=0) #创建一个额外树
        #forest = RandomForestClassifier (n_estimators = 10)  #创建一个随机树
    
        #计算X特征值对于Y的影响,并排序出来
        forest.fit(X, Y)
        importances = forest.feature_importances_
        std = np.std([tree.feature_importances_ for tree in forest.estimators_],
                     axis=0)
        indices = np.argsort(importances)[::-1]
        print indices
        # Print the feature ranking
        print(u"特征排名 :")
    
        for f in range(X.shape[1]):
            print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
    
        # Plot the feature importances of the forest
        plt.figure()
        plt.title(u"特征选择")
        plt.bar(range(X.shape[1]), importances[indices],
               color="r", yerr=std[indices], align="center")
        plt.xticks(range(X.shape[1]), indices)
        plt.xlim([-1, X.shape[1]])
        plt.show()
    
    FeatureSelection(X,Y)
  • 相关阅读:
    pix-PID介绍
    我的四轴专用PID参数整定方法及原理---超长文慎入(转)
    卡尔曼滤波原理
    5种常用的四轴飞行器PID算法讲解集合
    深入浅出无人机姿态,欧拉角,四元数,指数表示及数据转换与程序实现
    理解四元数
    Normal Equation(正规方程)
    特征与多项式回归
    实际梯度下降中的两个重要调节方面
    BZOJ 3624 Apio2008 免费道路
  • 原文地址:https://www.cnblogs.com/Ootori/p/7751865.html
Copyright © 2011-2022 走看看