首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择性排序

选择性排序
EN

Stack Overflow用户
提问于 2017-03-27 20:09:22
回答 2查看 65关注 0票数 0

我是Python新手,我想实现一个处理二进制或分类列表(对数据集的特性建模)的应急表。对于那些不知道的人,一个列列表是一个矩阵,它在通用元素m_ij中有一个数字,指定第一个特性的元素i在第二个特性的元素j的相同位置中的多少倍。很明显,每个特性的每个元素(一次)都应该成为一个行或列标题。我的问题是当我处理二进制特性时。在这种情况下,列列表必须以这个刚性序列中的耦合(1,0)作为头。

代码语言:javascript
复制
_|1|0|
1| | |
0| | |

虽然我编写的代码不能保证这种刚性:如果二进制特性的第一个元素为0,则相对头将不会以1开头。

请看我的代码:

代码语言:javascript
复制
def compute_contingency_table(first_f, second_f):
'''
This method compute contingency table of two features
:param first_f: first feature
:param second_f: second feature
:return: the contingency table
'''

first_values = get_values(first_f)
second_values = get_values(second_f)
contingency_table = np.zeros([len(first_values), len(second_values)])
corresponding_values = []

# for each value of the first feature
for h in range(len(first_values)):

    # find all the indeces in which it occurs
    f_indices = [i for i, x in enumerate(first_f) if x == second_f[h]]

    # save the corresponding values in the second feature
    for ind in f_indices:
        corresponding_values.append(second_f[ind])

    # createing contingency_table
    # for each value in corresponding values of the second feature
    for val in corresponding_values:
        # take its index in the values list (i.e. the column of contingency table)
        k = second_values.index(val)

        # increment the value of the corresponding contingency table element
        contingency_table[h, k] += 1

    del corresponding_values[:]

return contingency_table

用例:

代码语言:javascript
复制
first_f=[1,0,0,0,0,0,0]
second_f=[0,1,0,0,0,1,0]

由我的代码输出的应急表:

代码语言:javascript
复制
[[ 4.  2.]
 [ 1.  0.]]

虽然它应该是:

代码语言:javascript
复制
 [[ 0.  1.]
 [ 2.  4.]]

如您所见,这是因为输出表是类型的。

代码语言:javascript
复制
_|0|1|
0| | |
1| | |

如果它用二进制对(1,0)-way中的头进行排序,那么它应该工作;如果它们是符合条件的,则不进行排序。这就是我对选择性分类的意思。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-27 21:58:09

以这种方式完成:

代码语言:javascript
复制
def compute_contingency_table(first_f, second_f):
'''
This method compute contingency table of two features
:param first_f: first feature
:param second_f: second feature
:return: the contingency table
'''


first_values = get_values(first_f)
second_values = get_values(second_f)

if first_values == [0,1]:
    first_values = [1,0]
if second_values == [0,1]:
    second_values = [1,0]

contingency_table = np.zeros([len(first_values), len(second_values)])
corrisponding_values = []
for i in range(len(first_values)):

    f_indices = [k for k, x in enumerate(first_f) if x == first_values[i]]
    for ind in f_indices:
        corrisponding_values.append(second_f[ind])

    for s_val in corrisponding_values:
        k = second_values.index(s_val)
        contingency_table[i, k] += 1
    del corrisponding_values[:]

return contingency_table

用例1:

代码语言:javascript
复制
hair=['black', 'blonde', 'red', 'blonde', 'red', 'red', 'brown']
country = ['usa', 'china', 'usa', 'germany', 'germany','china', 'usa']
print(compute_contingency_table(hair,country))

输出

代码语言:javascript
复制
[[ 1.  0.  0.]
 [ 0.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  0.  0.]]

用例2:

代码语言:javascript
复制
a = [1, 0, 0, 0, 0, 0, 0]
b = [0, 0, 0, 1, 1, 0, 0]
print(compute_contingency_table(a,b))

输出

代码语言:javascript
复制
[[ 0.  1.]
 [ 2.  4.]]
票数 0
EN

Stack Overflow用户

发布于 2017-03-27 21:21:14

如果您想知道如何在Pandas中创建应急表:

代码语言:javascript
复制
import pandas as pd

df = pd.DataFrame()
df['first'] = [1,0,0,0,0,0,0]
df['second'] = [0,1,0,0,0,1,0]
contingency_table = df.groupby(['first', 'second']).size().unstack(fill_value=0)

代码语言:javascript
复制
contingency_table = pd.crosstab(df['first'], df['second'])

关于排序,在二进制值在compute_contingency_table中执行以下操作时,交换顺序就足够了。

代码语言:javascript
复制
first_values = list(set(first_f))
if len(first_values) == 2:
    first_values = sorted(first_values, reverse=True)
second_values = list(set(second_f))
if len(second_values) == 2:
    second_values = sorted(second_values, reverse=True)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43055574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档