# -*- coding:utf-8 -*-
import numpy as np
import pandas as pd
from sklearn.feature_selection import selectpercentile
from sklearn.feature_selection import chi2
class chi2select(
): def __init__(self):
pass
# 卡方檢驗特徵篩選
def chi2func(self,data_final,flag,spec_score):
#保留百分比的最高得分的特徵
selector = selectpercentile (chi2, percentile=spec_score)
selector.fit(data_final,flag)
return selector
# 載入及呼叫
def load_transform(self):
path = r'e:\programgao\csdnprogram'
data = pd.read_excel(path + '/dataset.xlsx', 'all'
) x = np.array(data.iloc[:,2:]
) y = np.array(data[
'flag'])
print(
'檢視初始資料 : \n', x)
selector = self.chi2func(x, y, spec_score=90)
# 保留90%的最高得分特徵
print(
'特徵得分 : \n', selector.scores_)
print(
'特徵得分的p_value值 : \n', selector.pvalues_)
print(
'篩選後保留特徵 : \n', selector.get_support(indices=true))
print(
'還原保留特徵 : \n', selector.transform(x))
if __name__ ==
'__main__'
: chi2select(
).load_transform(
)
Python資料探勘學習6卡方檢驗
1.定義 一種用途很廣的計數資料的假設檢驗方法。它屬於非引數檢驗的範疇,主要是比較兩個及兩個以上樣本率 構成比 以及兩個分類變數的關聯性分析。2.基本思想 統計樣本的實際觀測值與理論推斷值之間的偏離程度,實際觀測值與理論推斷值之間的偏離程度就決定卡方值的大小,如果卡方值越大,二者偏差程度越大 反之,...
SQL Server中匯入匯出資料三方法比較
當我們建立乙個資料庫時,並且想將分散在各處的不同型別的資料庫分類彙總在這個新建的資料庫中時,尤其是在進行資料檢驗 淨化和轉換時,將會面臨很大的挑戰。幸好sql server為我們提供了強大 豐富的資料匯入匯出功能,並且在匯入匯出的同時可以對資料進行靈活的處理。在sql server中主要有三種方式匯...
Python爬蟲使用bs4方法實現資料解析
聚焦爬蟲 爬取頁面中指定的頁面內容。編碼流程 資料解析分類 資料解析原理概述 解析的區域性的文字內容都會在標籤之間或者標籤對應的屬性中進行儲存 1.進行指定標籤的定位 2.標籤或者標籤對應的屬性中儲存的資料值進行提取 解析 bs4進行資料解析資料解析的原理 1.標籤定位 2.提取標籤 標籤屬性中儲存...