【发布时间】: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')]
提前致谢。
【问题讨论】: