用python

时间:2018-11-05 02:12:38

标签: python

我很难在python中对rbg值的数据集进行排序。我想使用一种贪婪的启发式方法,选择一个随机的rgb值,找到具有另一个rgb值的最短欧几里得距离,然后坚持该rgb值...重复直到所有rbg值都被排序为止。

我遇到以下错误

TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U20') dtype('<U20') dtype('<U20')

它正确地选择了一个随机值,并且我认为该错误正在发生,因为我正试图减去一个具有此值的值(这是返回的值)

['0.9664535356921388' '0.4407325991753527' '0.007491470058587191']

来自rgb值列表

数据集,第一行是数据集的长度。每种颜色的RGB值都存储在.txt文件中。

10
0.9664535356921388 0.4407325991753527 0.007491470058587191
0.9109759624491242 0.939268997363764 0.5822275730589491
0.6715634814879851 0.08393822683708396 0.7664809327917963
0.23680977536311776 0.030814021726609964 0.7887727172362835
0.3460889655971231 0.6232814750391685 0.6158156951036152
0.14855463870828756 0.18309064740993164 0.11441296968868764
0.014618780486909122 0.48675154060475834 0.9649015609162157
0.06456228097718608 0.5410881855511303 0.46589855900830957
0.6014634495610515 0.08892882999066232 0.5790026861873665
0.26958550381944824 0.5564325605562156 0.6446342341782827

脚本

def greedy(colours):
    newcolours = []
    colours  = np.array(colours)
    firstchoice = random.choice(colours)
    print(firstchoice)
    for colour in colours:
     dist = np.linalg.norm(firstchoice - colour)
     shortest = min(dist)
     del(shortest)
     print(shortest)
     newcolours.append(shortest)

def read_file(fname):
    with open(fname, 'r') as afile:
        lines = afile.readlines()
    n = int(lines[3])    # number of colours  in the file
    col = []
    lines = lines[4:]    # colors as rgb values
    for l in lines:
        rgb = l.split()
        col.append(rgb)
    return n, col

0 个答案:

没有答案