如何返回每行中出现次数最多的元素呢

请问如何返回每行中出现次数最多的元素,把它们组成一个列表呢?并且如果0和1出现一样多,就返回0;1和2出现一样多,就返回1.
就比如这个数组:
[2 2 2 2 2 2]
[1 1 1 2 2 0]
[1 1 1 0 0 0]
[2 2 2 1 1 1]
我想让他返回
[2]
[1]
[0]
[1]
请问应该如何实现呢?

from collections import Counter

array = [
    [2, 2, 2, 2, 2, 2],
    [1, 1, 1, 2, 2, 0],
    [1, 1, 1, 0, 0, 0],
    [2, 2, 2, 1, 1, 1]
]

result = []
for row in array:
    count = Counter(row)
    max_count = max(count.values())
    most_common = [k for k, v in count.items() if v == max_count]
    if len(most_common) == 2 and 1 in most_common and 2 in most_common:
        result.append(0)
    elif count[2] > count[1]:
        result.append(1)
    else:
        result.append(most_common[0])

print(result) # 输出 [2, 1, 0, 1]

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/1063593
  • 你也可以参考下这篇文章:梯度下降算法主要通过哪两个控制因子实现最优参数选择?这两个因子分别起到什么作用?为什么计算损失函数最优值采用梯度下降算法而不是直接对损失函数求导数等于0时的最优解?如何判断梯度下降算法是否正确工作?
  • 除此之外, 这篇博客: 量化交易实战【1】自己搭建一个的股票交易回测框架,并通过均线择时策略进行回测中的 2.2 产生交易信号:即找出买入与卖出信号 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    # 找买入信号
    # 当天的短期均线大于等于长期均线
    condition1 = (df['ma_short'] >= df['ma_long'])
    # 上一个交易日的短期均线小于长期均线
    condition2 = (df['ma_short'].shift(1) < df['ma_long'].shift(1))
    # 将买入信号当天的signal设为1
    df.loc[condition1 & condition2, 'signal'] = 1
    
    # 找卖出信号
    # 当天的短期均线小于等于长期均线
    condition1 = (df['ma_short'] <= df['ma_long'])
    # 上一个交易日的短期均线大于长期均线
    condition2 = (df['ma_short'].shift(1) > df['ma_long'].shift(1))
    # 将买入信号当天的signal设为0
    df.loc[condition1 & condition2,'signal'] = 0
    
    # 浏览产生交易信号的日期
    df[df['signal'].notnull()].head(10)
    
    dateopenclosehighlowchange_pctma_shortma_longsignal
    322015-02-258.778.628.778.59-0.0137308.6828.68101.0
    382015-03-058.438.358.458.30-0.0141688.5628.60750.0
    432015-03-128.879.129.348.790.0543358.7208.63301.0
    802015-05-0611.9611.8812.3211.71-0.00917412.31412.34450.0
    942015-05-2612.5712.5612.6212.310.00079712.25012.11901.0
    992015-06-0212.0111.9212.0211.73-0.00831911.94011.98850.0
    1022015-06-0512.6212.3212.7012.12-0.00404212.12012.03651.0
    1102015-06-1711.9711.8912.0111.680.00592212.13612.24500.0
    1762015-09-228.198.288.368.180.0122258.2028.18301.0
    1772015-09-238.218.098.258.07-0.0229478.1728.19650.0
  • 您还可以看一下 孙伟老师的【孙伟】新手必看的设计思维与绘图制作视频教程课程中的 1案例介绍与设计思路小节, 巩固相关知识点
  • 以下回答来自chatgpt:

    我可以给出代码来解决这个问题。假设数组为一个二维列表,每一行元素的类型是整数,代码如下:

    from collections import Counter
    
    def find_most_common(lst):
        """
        找到列表中出现最多的元素,如果0和1出现次数相同则返回0,如果1和2出现次数相同则返回1。
        """
        counter = Counter(lst)
        most_common = counter.most_common()   # 出现频率最高的项列表,[(item1, frequency1), (item2, frequency2), ...]
        most_common_numbers = [tup[0] for tup in most_common if tup[1] == most_common[0][1]]   # 出现频率最高的项的列表
        if 0 in most_common_numbers and 1 in most_common_numbers:
            return 0
        if 1 in most_common_numbers and 2 in most_common_numbers:
            return 1
        return most_common_numbers[0]   # 返回第一个出现频率最高的元素
    
    
    def find_most_commons(arr):
        """
        从一个数组中找出每一行中出现次数最多的元素,并将它们组成一个列表。
        """
        result = []
        for row in arr:
            most_common = find_most_common(row)
            result.append(most_common)
        return result
    

    接下来,我将举例说明如何使用这个代码。假设我们有一个包含5行3列的二维列表:

    arr = [[1, 2, 0], [0, 1, 1], [1, 1, 2], [0, 1, 2], [1, 2, 2]]
    

    我们可以像下面这样调用函数 find_most_commons() 来找出每一行中出现次数最多的元素,并将它们组成一个列表:

    res = find_most_commons(arr)
    print(res)
    # 输出: [1, 1, 1, 1, 2]
    

    这说明,在每一行中出现次数最多的元素分别是1、1、1、1和2。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^