为什么python sklearn特征选择卡方(chi2)测试不对称?

时间:2019-02-27 12:09:31

标签: python feature-selection chi-squared

sklearn功能选择包中的chi2函数返回卡方统计量和p值。卡方统计量是相同的,这与对称性无关,而与给出变量的顺序无关,但似乎并非如此。

这是一个代码示例:

            // var testOuterData = [5, 20];
            var testNotSubmittedData = [4, 1];
            var testSubmittedData = [5, 6, 6, 3];

            window.onload = function () {

                outerLabels = [
                    "Not Submitted",
                    "Submitted"
                ];
                innerLabels = [
                    "Overdue",
                    "Due",
                    "Not Reviewed yet",
                    "Accepted with Comments",
                    "Returned",
                    "Accepted"
                ];

      

                buildTestChart(outerLabels, innerLabels);
            };

            function buildTestChart(outerLabels, innerLabels) {

                var testInnerData = testNotSubmittedData.concat(testSubmittedData);

                var testConfig = {
                    type: 'doughnut',
                    data: {
                        datasets: [{
                            data: [12, 23, 10, 72],
                            backgroundColor: [
                                'rgb(168, 187, 208)',
                                'rgb(232, 241, 254)',
                                'rgb(211, 225, 242)',
                                'rgb(0, 37, 105)'
                            ],
                        }],
                    },

                    data: {
                        datasets: [
                            {
                                data:  [5, 20],
                                explodeSection: 0,
                                backgroundColor: ["#c34258", "#34bfa2"],
                                borderWidth: [0, 0],
                                borderColor: ["white", "transparent"],
                                label: 'Outer Data',
                                labels:  [
                                            "Not Submitted",
                                            "Submitted"
                                          ]
                            },
                            {
                                data: testInnerData,
                                explodeSection: 0,
                                backgroundColor: [
                                    "#f2676a",
                                    "#fdca6e",
                                    "#fff",
                                    "#36a3f6",
                                    "#f4516c",
                                    "#7fd8bc"
                                ],
                                borderWidth: [0, 0, 0, 0, 0, 0],

                                label: 'Inner Data',
                                labels: [
                                            "Overdue",
                                            "Due",
                                            "Not Reviewed yet",
                                            "Accepted with Comments",
                                            "Returned",
                                            "Accepted"
                                        ]
                            }
                        ],
                        labels: ['Not Submitted', 'Submitted']
                    },

                    options: {
                        legend: {
                            display: false
                        },
                        responsive: true,
                        maintainAspectRatio: false,
                        cutoutPercentage: 0,
                        tooltips: {
                            callbacks: {
                                label: function (tooltipItem, data) {
                                    var label = data.datasets[tooltipItem.datasetIndex].labels[tooltipItem.index];
                                    var value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                                    return label + ': ' + value;
                                }
                            }
                        },
                        plugins: {
                            labels: [
                                {
                                    render: function (data) {
                                        if (data.dataset.label === 'Outer Data') {
                                            return data.label;
                                        }
                                        else {
                                            return '';
                                        }
                                    },
                                    position: 'outside',
                                    fontFamily: "'Lato', 'Lato', 'Arial', sans-serif",
                                    fontSize: 14,
                                    textMargin: 8,
                                    fontColor: '#808080'
                                },
                                {
                                    render: function (data) {
                                        if (data.dataset.label === 'Inner Data') {
                                            return '';
                                        }
                                    },
                                    render: 'value',
                                    fontSize: 12,
                                    position: 'border',
                                    textMargin: 4,
                                    fontColor: [
                                        "#fff",
                                        "#fff",
                                        "#000",
                                        "#fff",
                                        "#fff",
                                        "#fff"
                                    ],
                                    fontFamily: "'Lato', 'Lato', 'Arial', sans-serif"
                                }
                            ],
                            datalabels: {
                                display: false
                            }
                        },
       
                    }
                };


                var ctxTest = document.getElementById('test').getContext('2d');
                window.myPie = new Chart(ctxTest, testConfig);
                testSubmittalsChart = window.myPie;
            }

            var helpers = Chart.helpers;

            Chart.controllers.doughnut = Chart.controllers.doughnut.extend({
              // function to increase inner charts diameter
                update: function (reset) {
                    var me = this;

                    if (me.index === 0) {// Outer chart

                        var chart = me.chart,
                            chartArea = chart.chartArea,
                            opts = chart.options,
                            arcOpts = opts.elements.arc,
                            availableWidth = chartArea.right - chartArea.left - arcOpts.borderWidth,
                            availableHeight = chartArea.bottom - chartArea.top - arcOpts.borderWidth,
                            minSize = Math.min(availableWidth, availableHeight),
                            offset = {
                                x: 0,
                                y: 0
                            },
                            meta = me.getMeta(),
                            cutoutPercentage = 70,
                            circumference = opts.circumference;

                        chart.borderWidth = me.getMaxBorderWidth(meta.data);
                        chart.outerRadius = Math.max((minSize - chart.borderWidth) / 2, 0);
                        chart.innerRadius = Math.max(cutoutPercentage ? (chart.outerRadius / 100) * (cutoutPercentage) : 0, 0);
                        chart.radiusLength = ((chart.outerRadius - chart.innerRadius) / chart.getVisibleDatasetCount()) + 25;
                        chart.offsetX = offset.x * chart.outerRadius;
                        chart.offsetY = offset.y * chart.outerRadius;

                        meta.total = me.calculateTotal();

                        me.outerRadius = chart.outerRadius - (chart.radiusLength * me.getRingIndex(me.index));
                        me.innerRadius = Math.max(me.outerRadius - chart.radiusLength, 0);
                    }

                    else if (me.index === 1) { // Inner chart

                        var chart = me.chart;
                        opts = chart.options,
                            meta = me.getMeta(),
                            cutoutPercentage = opts.cutoutPercentage,
                            circumference = opts.circumference;

                        meta.total = me.calculateTotal();
                        me.outerRadius = 90;
                        me.innerRadius = 0;

                        // factor in the radius buffer if the chart has more than 1 dataset
                        if (me.index > 0) {
                            me.outerRadius = 90;
                        }
                    }
                    helpers.each(meta.data, function (arc, index) {
                        me.updateElement(arc, index, reset);
                    });
                }
            });

我的问题: 为什么chi2_1和chi2_2不一样?该示例使用随机数。但举一个例子,在我的一次运行中,我得到了chi2_1为89.59和chi2_2为37.92的卡方统计量。知道我在这里缺少什么吗?

0 个答案:

没有答案