使用隨機森林進行特徵選擇的具體方法

2021-08-20 18:24:36 字數 2180 閱讀 5288

import numpy as np

import pandas as pd

from sklearn import svm

#from sklearn.linear_model import logisticregression

#——————————————————匯入訓練資料——————————————————————

data0 = pd.read_csv('data22.csv',index_col=none,parse_dates = true) #pd.read_csv預設生成dataframe物件

data1 = data0.iloc[:-227,-1].values[:,np.newaxis]#錯位一天,以前一天來**後一天

data2 =data0.iloc[227:,2:8].values #取第2-9列

data = np.concatenate((data1,data2),axis=1)

train_x=data[:,:6]

train_y=data[:,6]

import matplotlib.pyplot as plt
feature_names=['ex_value','season','isholiday','dw','weather','temperature']
x=train_x[:288*30]#30天的資料

y=train_y[:288*30]

from sklearn.cross_validation import cross_val_score, shufflesplit

from sklearn.ensemble import randomforestregressor

#load boston housing dataset as an example

names = feature_names

rf = randomforestregressor(n_estimators=100, max_depth=4)

scores =

for i in range(x.shape[1]):

score = cross_val_score(rf, x[:, i:i+1], y, scoring="r2",

cv=shufflesplit(len(x), 3, .3))

print(sorted(scores, reverse=true))

[(0.708, 'ex_value'), (0.138, 'temperature'), (0.124, 'weather'), (0.02, 'dw'), (0.018, 'isholiday'), (-0.0, 'season')]
180天,特徵相關性檢測:[(0.736, 『ex_value』), (0.162, 『temperature』), (0.103, 『season』), (0.031, 『weather』), (0.009, 『isholiday』), (0.001, 『dw』)]

7天,特徵相關性檢測:[(0.888, 『ex_value』), (-0.001, 『season』), (-0.002, 『temperature』), (-0.003, 『weather』), (-0.004, 『dw』), (-0.006, 『isholiday』)]

30天,特徵相關性檢測:[(0.716, 『ex_value』), (0.13, 『temperature』), (0.124, 『weather』), (0.023, 『dw』), (0.02, 『isholiday』), (-0.001, 『season』)]

360天特徵相關性檢測:[(0.747, 『ex_value』), (0.102, 『temperature』), (0.037, 『season』), (0.021, 『weather』), (0.005, 『isholiday』), (0.001, 『dw』)]

1天,特徵相關性檢測:[(0.919, 『ex_value』), (0.101, 『weather』), (0.085, 『temperature』), (0.057, 『dw』), (-0.005, 『season』), (-0.02, 『isholiday』)]

隨機森林進行特徵選取

在隨機森林中某個特徵x的重要性的計算方式如下 首先是對隨機森林的每一顆決策樹,使用對應的oob 袋外資料 資料來進行計算他的袋外的資料誤差,記做error1 這樣每個決策樹都可以得到乙個error1,k顆數就有k個error1.然後就是要遍歷所有的特徵,來考察該特徵的重要性,考察重要性的方式是,隨機...

隨機森林特徵選擇

隨機森林具有準確率高 魯棒性好 易於使用等優點,這使得它成為了目前最流行的機器學習演算法之一。隨機森林提供了兩種特徵選擇的方法 mean decrease impurity 和mean decrease accuracy。隨機森林由多個決策樹構成。決策樹中的每乙個節點都是關於某個特徵的條件,為的是將...

隨機森林之特徵選擇

摘要 在隨機森林介紹 中提到了隨機森林乙個重要特徵 能夠計算單個特徵變數的重要性。並且這一特徵在很多方面能夠得到應用,例如在銀行貸款業務中能否正確的評估乙個企業的信用度,關係到是否能夠有效地 貸款。但是信用評估模型的資料特徵有很多,其中不乏有很多噪音,所以需要計算出每乙個特徵的重要性並對這些特徵進行...