Skip to main content
 首页 » 编程设计

python之在for循环中对压缩列表进行排序

2025年01月19日9emanlee

如果你想运行它,这是我的代码

import numpy as np 
import pandas as pd 
from sklearn.datasets import make_classification 
from sklearn.ensemble import RandomForestClassifier 
from treeinterpreter import treeinterpreter as ti 
import operator 
 
 
X, y = make_classification(n_samples=1000, 
                           n_features=6, 
                           n_informative=3, 
                           n_classes=2, 
                           random_state=0, 
                           shuffle=False) 
 
# Creating a dataFrame 
df = pd.DataFrame({'Feature 1':X[:,0], 
                                  'Feature 2':X[:,1], 
                                  'Feature 3':X[:,2], 
                                  'Feature 4':X[:,3], 
                                  'Feature 5':X[:,4], 
                                  'Feature 6':X[:,5], 
                                  'Class':y}) 
 
 
y_train = df['Class'] 
X_train = df.drop('Class',axis = 1) 
 
rf = RandomForestClassifier(n_estimators=50, 
                               random_state=0) 
 
rf.fit(X_train, y_train) 
 
 
importances = rf.feature_importances_ 
importances = X_train.columns 
 
instances = X_train.iloc[[60]] 
 
 
prediction, biases, contributions = ti.predict(rf, instances) 

我尝试用两种方式对列表进行排序,首先是使用 itemgetter

for i in range(len(instances)): 
    for c, feature in sorted(zip(contributions[i], importances), key=operator.itemgetter(1)): 
        print (feature, np.round(c, 5)) 

其次,使用key= lambda

for i in range(len(instances)): 
    for c, feature in sorted(zip(contributions[i], importances), key=lambda x: x[0].any()): 
        print (feature, np.round(c, 5)) 

但是运行这两个解决方案产生了相同的输出

Feature 1 [ 0.16033 -0.16033] 
Feature 2 [-0.02422  0.02422] 
Feature 3 [-0.15412  0.15412] 
Feature 4 [ 0.17162 -0.17162] 
Feature 5 [ 0.02897 -0.02897] 
Feature 6 [ 0.01889 -0.01889] 

我想使用上面输出的第一列对列表进行排序,知道我做错了什么吗?

更新 2:只是澄清问题

按特征顺序而不是特征括号内的值对它们进行排序

更新 3:如果您对第一列进行排列,输出应该是怎样的

Feature 4 [ 0.17162 -0.17162] 
Feature 1 [ 0.16033 -0.16033] 
Feature 5 [ 0.02897 -0.02897] 
Feature 6 [ 0.01889 -0.01889] 
Feature 2 [-0.02422  0.02422] 
Feature 3 [-0.15412  0.15412] 

如果你排列第二列,输出应该是什么样子

Feature 3 [-0.15412  0.15412] 
Feature 2 [-0.02422  0.02422] 
Feature 6 [ 0.01889 -0.01889] 
Feature 5 [ 0.02897 -0.02897] 
Feature 1 [ 0.16033 -0.16033] 
Feature 4 [ 0.17162 -0.17162] 

更新 4 在排序中包含一个 if 条件

打印大于 0.01 和 -0.01 的值

请您参考如下方法:

首先,您需要将数据转换为可行的格式:contributions.shape是(1, 6, 2) .有contributions[0]可以轻松地使用 zip 进行迭代:

zip(importances, contributions[0]) 

将产生 name + [values]对。以下是如何通过链接索引使用 lambda 进行迭代和排序:

for name, values in sorted(zip(importances, contributions[0]), key=lambda pair: pair[1][0]): 
    print(name, values) 

Lambda 将占用 name + values对,拿values与 [1] , 而第一列的值是 [0] .

过滤是另一项任务。之后读取/调试代码的最简单方法是只检查 for 中的值循环:

for name, values in sorted(zip(importances, contributions[0]), key=lambda pair: pair[1][0]): 
    if -0.01 < values[0] > 0.01:  
        print(name, values) 

如果a < b > c是神秘的,你可以把它切换到not a < b < c ,或(在您的情况下)abs(b) > a