In [1]:
import random
from copy import deepcopy

import numpy as np
In [2]:
"""
按列打印/按行打印numpy array
[1,2,3,4]
[5,6,7,8]
"""

a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

for i in range(0, a.shape[1]):
    print(a[:, i])
print()
for i in range(0, a.shape[0]):
    print(a[i, :])
[1 5]
[2 6]
[3 7]
[4 8]

[1 2 3 4]
[5 6 7 8]
In [3]:
a = np.array([[1, 2, 3], [2, 4, 5], [5, 6, 7]])
b = np.array([15, 5, 5])
b1 = b.reshape(1, -1)
c = np.concatenate((b1, a), axis=0)

print("a")
print(a)
print()
print("b")
print(b)
print()
print("b1")
print(b1)
print()
print("c")
print(c)
a
[[1 2 3]
 [2 4 5]
 [5 6 7]]

b
[15  5  5]

b1
[[15  5  5]]

c
[[15  5  5]
 [ 1  2  3]
 [ 2  4  5]
 [ 5  6  7]]
In [4]:
def highest_freq(lst):
    counter = {}
    for item in lst:
        if item in counter.keys():
            counter[item] += 1
        else:
            counter[item] = 1

    maxC = -1
    mostElement = None
    for key in counter.keys():
        if counter[key] > maxC:
            mostElement = key
            maxC = counter[key]
    return mostElement


lst = [1, 1, 1, 1, 2, 2, 2]
print(highest_freq(lst))
1
In [5]:
a = np.zeros((3, 10), dtype=int)
b = np.array([[1, 2], [4, 5], [5, 6]])
start_index = 0
a[:, start_index : start_index + b.shape[1]] = b
print(a)
[[1 2 0 0 0 0 0 0 0 0]
 [4 5 0 0 0 0 0 0 0 0]
 [5 6 0 0 0 0 0 0 0 0]]
In [6]:
a = np.zeros((3, 10), dtype=int)
b = np.array([[1, 2], [4, 5], [5, 6]])
start_index = 1
a[:, start_index : start_index + b.shape[1]] = b
print(a)
[[0 1 2 0 0 0 0 0 0 0]
 [0 4 5 0 0 0 0 0 0 0]
 [0 5 6 0 0 0 0 0 0 0]]
In [7]:
y = np.array([[1, 2, 3.1], [3, 4, 100], ["喊", 3, 5]])


print(np.issubdtype(y.dtype, np.integer))


y = np.array([[1, 2, 3.1], [3, 4, 100], [10.1, 3, 5]])
print(np.issubdtype(y.dtype, np.integer))


y = np.array([[1, 2, 3], [3, 4, 100], [10, 3, 5]])
print(np.issubdtype(y.dtype, np.integer))
False
False
True
In [8]:
"""
好像就是从一个list里面挑选出出现次数最多的那个元素;如果出现次数最多的元素不止一个,那就从这些出现次数最多的元素中随机选择一个
"""


def highest_freq_helper(reverse_counter, item):
    for key in reverse_counter.keys():
        for value in reverse_counter[key]:
            if value == item:
                reverse_counter[key].remove(item)
                if (key + 1) in reverse_counter.keys():
                    reverse_counter[key + 1].append(item)
                else:
                    reverse_counter[key + 1] = [item]
                return deepcopy(reverse_counter)


def highest_freq(lst):
    print("original list: %s" % lst)
    reverse_counter = {0: list(set(lst))}

    # 遍历每一个list元素
    for item in lst:
        reverse_counter = highest_freq_helper(reverse_counter, item)

    print("reverse counter: %s" % reverse_counter)
    # print('debug here')
    # print(reverse_counter)
    max_count = -1
    for key in reverse_counter.keys():
        if key > max_count:
            max_count = key

    print(
        "max count: %d, random choose from %s" % (max_count, reverse_counter[max_count])
    )
    print("random choice result: %d" % random.choice(reverse_counter[max_count]))


highest_freq([1, 2, 3, 4, 5, 5, 4, 1])
print("-----")
highest_freq([1, 6, 6, 2, 3, 4, 5, 5, 6, 4, 1])
original list: [1, 2, 3, 4, 5, 5, 4, 1]
reverse counter: {0: [], 1: [2, 3], 2: [5, 4, 1]}
max count: 2, random choose from [5, 4, 1]
random choice result: 4
-----
original list: [1, 6, 6, 2, 3, 4, 5, 5, 6, 4, 1]
reverse counter: {0: [], 1: [2, 3], 2: [5, 4, 1], 3: [6]}
max count: 3, random choose from [6]
random choice result: 6
In [ ]: