您好,登錄后才能下訂單哦!
from sklearn.feature_selection import RFE
from sklearn.linear_model import LinearRegression
#Load boston housing dataset as an example
X = np.array(train1[feature_use].fillna(-1))[1:train1.size,:]
Y = np.array(train1['target'])[1:train1.size]
#print(X)
#print(Y)
names = feature_use
#use linear regression as the model
lr = LinearRegression()
#rank all features, i.e continue the elimination until the last one
rfe = RFE(lr, n_features_to_select=1)
rfe.fit(X,Y)
print("Features sorted by their score:")
#print(sorted(zip(map(lambda x: round(x, 4), rf.feature_importances_), names), reverse=True))
sortedlist = sorted(zip(map(lambda x: round(x, 4), rfe.ranking_), names),
reverse=True)
print(sortedlist)
feature_use = []
for index in sortedlist[len(sortedlist)-70 : ]:
if index[0]>0:
feature_use.append(index[1])
print(feature_use)
上面的X為數(shù)據(jù)集的特征集合 Y為標簽集合
在sortlist里對特征的重要性進行了排序
最近做機器學習的一點感悟是,特征的影響遠比模型參數(shù)來的大,特征是現(xiàn)實世界在算法中的倒影。
在特征工程中要對業(yè)務有非常深的理解,強調返璞歸真,刪除無效特征,減少引起干擾的特征。
加特征的過程需要一個一個來,還要多思考這些特征之間的關系,是否是強烈線性相關的。
# random forest select features
'''
from sklearn.ensemble import RandomForestRegressor
import numpy as np
#Load boston housing dataset as an example
X = np.array(train1[feature_use].fillna(-1))[1:train1.size,:]
Y = np.array(train1['target'])[1:train1.size]
print(X)
print(Y)
names = feature_use
rf = RandomForestRegressor()
rf.fit(X, Y)
print("Features sorted by their score:")
print(sorted(zip(map(lambda x: round(x, 4), rf.feature_importances_), names),
reverse=True))
'''
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。