Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use several lines of code to make feature selection

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

From sklearn.feature_selection import RFEfrom sklearn.linear_model import LinearRegression#Load boston housing dataset as an exampleX = np.array (train1 1 [feature _ use]. Fillna (- 1)) [1Partition:] Y = np.array (train1 ['target']) [1:train1.size] # print (X) # print (Y) names = feature_use#use linear regression as the modellr = LinearRegression () # rank all features, i.e continue the elimination until the last onerfe = RFE (lr, n_features_to_select=1) rfe.fit (X) Y) print ("Features sorted by their score:") # print (sorted (map (lambda x: round (x, 4), rf.feature_importances_), names), reverse=True)) sortedlist = sorted (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)

The above X is the feature set of the dataset, Y is the label set.

Ranking the importance of features in sortlist

Recently, one of the insights of machine learning is that the influence of features is much greater than that of model parameters, and the feature is the reflection of the real world in the algorithm.

In feature engineering, we should have a very deep understanding of the business, emphasize returning to nature, delete invalid features, and reduce the features that cause interference.

The process of adding features requires one by one, and we need to think more about whether the relationship between these features is strongly linear.

# random forest select features'''from sklearn.ensemble import RandomForestRegressorimport numpy as np#Load boston housing dataset as an exampleX = np.array (room1 [feature _ use] .fillna (- 1)) [1Participation 1.sizeMagna:] Y = np.array (train1 ['target']) [1:train1.size] print (X) print (Y) names = feature_userf = 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)''

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report