【问题标题】:How to get correlated features in a list with a specific format?如何在具有特定格式的列表中获取相关特征?
【发布时间】:2019-11-20 07:51:31
【问题描述】:

我正在学习熊猫。我需要以下帮助。 我试图从相关矩阵中找出最高相关的特征。

# Iris Dataset
features = ['sepal_length','sepal_width','petal_length','petal_width','class']
data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",\
                           header = None,\
                           names = features)
correlation = data.corr()
c = correlation.where(np.triu(np.ones(correlation.shape),k=1).astype(np.bool)).stack().sort_values(ascending = False)
highest = c[c>0.5]
print(highest)
print(highest.index)

上面sn-p的输出是:

petal_length  petal_width     0.962757
sepal_length  petal_length    0.871754
              petal_width     0.817954
dtype: float64
MultiIndex(levels=[['sepal_length', 'sepal_width', 'petal_length', 'petal_width'], ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']],
           labels=[[2, 0, 0], [3, 2, 3]])

是否有可能将“最高”系列的输出转换为具有以下指定格式的列表?

list = [['petal_length','petal_width',0.962757],['sepal_length','petal_length',0.871754]['sepal_length','petal_width',0.817954]]

用外行的话来说,我需要系列列表中的索引列(两列)。

我试过了,它的工作原理。但我需要上面的列表:

length = highest.shape[0]
list = []
for i in range(length):
    list.append(highest.index[i])
print('list =',list)

输出:

list = [('petal_length', 'petal_width'), ('sepal_length', 'petal_length'), ('sepal_length', 'petal_width')]

提前致谢。

【问题讨论】:

    标签: python pandas list series


    【解决方案1】:

    是的,使用:

    highest.reset_index().values.tolist()
    

    输出:

    [['petal_length', 'petal_width', 0.9627570970509667],
     ['sepal_length', 'petal_length', 0.8717541573048719],
     ['sepal_length', 'petal_width', 0.8179536333691635]]
    

    【讨论】:

    • @JSabariVishnuJayanthan 你会考虑accepting这个答案吗?
    • 是的。非常感谢。
    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多