-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeature_Importance .py
173 lines (134 loc) · 4.43 KB
/
Feature_Importance .py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# coding: utf-8
# In[2]:
#Linear Regression Feature Importance
import sklearn
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression
from matplotlib import pyplot
# define the dataset
X,y= make_regression(n_samples=1000, n_features=10,n_informative=5,random_state=1)
#defien the model
model=LinearRegression()
#fit the model
model.fit(X,y)
#get importance
importance=model.coef_
#summarize features importance
for i,v in enumerate(importance):
print('Feature:%0d,Score: %.5f' %(i,v))
# plot fearure importance
pyplot.bar([x for x in range (len(importance))], importance)
pyplot.show()
# In[6]:
# logistic Regresstion for future importance
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
#define the Data set
X,y= make_classification(n_samples=1000, n_features=10, n_informative=5,n_redundant=5, random_state=1)
#define the model
model=LogisticRegression()
#fit the model
model.fit(X,y)
#get importance
importance = model.coef_[0]
#summarize feature importancefor
for i,v in enumerate(importance):
print('Feature: %0d ,Score: %.5f',(i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))],importance)
pyplot.show()
# In[8]:
#Desision Tree for features importance on a regresstion problem
from sklearn.datasets import make_regression
from sklearn.tree import DecisionTreeRegressor
from matplotlib import pyplot
#define the dataset
X,y=make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)
#definethe model
model= DecisionTreeRegressor()
#fit the model
model.fit(X,y)
#get the importance
importance=model.feature_importances_
#summrize feature importance
for i,v in enumerate(importance):
print('Feature:%0d ,Score %.5f: ' %(i,v))
#plot feature importance
pyplot.bar([ x for x in range(len(importance))],importance)
pyplot.show()
# In[13]:
#decision tree for feature importance on a regression problem
from sklearn.datasets import make_classification
from sklearn.tree import DecisionTreeClassifier
# define the dataset
X,y=make_classification(n_samples=1000,n_features=10,n_informative=5, random_state=1)
#define the model
model=DecisionTreeClassifier()
#fit the model
model.fit(X,y)
#get importancabse
importance=model.feature_importances_
#summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score:%.5f' %(i,v))
#plot feature importane
pyplot.bar([x for x in range(len(importance))],importance)
pyplot.show()
# In[18]:
# Randome feature importance
from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor
# define the Dataset
X,y=make_regression(n_samples=1000,n_features=10,n_informative=5,random_state=1)
#define the model
model=RandomForestRegressor()
#fit the model
model.fit(X,y)
#get the importance Features
importance=model.feature_importances_
for i,v in enumerate(importance):
print('Feature: %0d, Score:%.5f' %(i,v))
# plot features importance
pyplot.bar([x for x in range(len(importance))] ,importance)
pyplot.show()
# In[19]:
#Random forest features importance on classification problem
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
#define the Dataset
X,y=make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5,random_state=1)
#define the model
model=RandomForestClassifier()
#fit the model
model.fit(X,y)
#get the importancabse
importance=model.feature_importances_
#summarize features importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' %(i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
# In[27]:
from sklearn.datasets import make_regression
from sklearn.neighbors import KNeighborsRegressor
from sklearn.inspection import permutation_importance
from matplotlib import pyplot
# define dataset
X, y = make_regression(n_samples=1000, n_features=10, n_informative=5, random_state=1)
#define the model
model=KNeighborsRegressor()
#fit thr model
model.fit(X,y)
# perform permutation importance
results=permutation_importance(model,X,y,scoring='neg_mean_squared_error')
#get importance
importance = results.importances_mean
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
# In[ ]: