Python - how to put a combination of one that always goes in a map and one that transforms

Asked 2 years ago, Updated 2 years ago, 58 views

from multiprocessing import pool
dist_result = []

with Pool() as p:
    dist_result.append(p.map(dist_cal, !!!!(total_point, range(100) !!!!))

The part where the exclamation mark is stacked is the part that I don't know. The code you want to implement is as follows:

By default, total_point is a set of millions of 100-dimensional intasers. [Example] [1, 2, 3...100] , [-1,2,3...], [12,4,2,...] ...]

dist_cal is a function. Find the Euclidean distance between the internal points. However, if you add dimension, out of the dots, We will only reflect the dimension you put in

import itertools
def dist_cal(points_list, dim):

    result = 0
    for combi in itertools.combinations(points_list, 2):
        tmp = 0
        for i in range(dim):
            tmp += (combi[0][i]-combi[1][i])**2
        result += tmp**0.5

    return result

from 0 to 100 dimensions As we increase the dimension, we want to observe the change in the distance between the points, and the goal is to parallelize it because of the large amount of computation.

Back to the code you want to ask

from multiprocessing import pool
dist_result = []

with Pool() as p:
    dist_result.append(p.map(dist_cal, !!!!(total_point, range(100) !!!!))

Total_point is the default input that does not change for all parallel operations. But it just wants to run 0 to 100 differently, to run this in parallel When you put it in p.map(), you put a function and its factors separately.

When generating these factors,

This is because I don't know how to generate it in the form of (fixed total points, numbers from 1 to 100 that keep changing).

Please.

==============================

def genero(po, N):
    count = 0
    for i in range(N):
        yield po, i

with Pool() as p:
    dist_result.append(p.map(dist_cal, genero(total_point, 10) ))

I'm approaching it, but I don't know if it's the right approach. Why not;;;;

python map

2022-09-22 18:16

1 Answers

If I write it down briefly...

You want me to mix 0-99 numbers, right?

import random

L = list(range(100)) #0~99 is stored
random.shuffle(L) # Mix the elements of L

print(L)
[56, 43, 52, 7, 92, 97, 42, 79, 66, 80, 55, 69, 41, 91, 34, 2, 23, 49, 10, 6, 64, 30, 98, 87, 17, 51, 47, 85, 45, 72, 5, 81, 3, 93, 16, 67, 99, 25, 28, 86, 95, 78, 22, 14, 0, 36, 1, 15, 27, 89, 32, 48, 62, 40, 37, 74, 31, 90, 76, 84, 68, 24, 57, 60, 18, 12, 38, 88, 44, 26, 70, 29, 65, 50, 73, 71, 53, 63, 4, 33, 94, 13, 75, 82, 96, 20, 19, 39, 61, 9, 46, 58, 8, 11, 59, 77, 83, 54, 35, 21]

But when I looked at the code, it was a little wrong. You must use startap because there are more than two arguments.(Version 3.3 or later)

Total_point would have to be equal to 100 times. The sample mixes elements, such as shuffle, but returns a new list.

with Pool() as p:
    dist_result.append(p.starmap(dist_cal, zip([total_point] * 100, random.sample(range(100), 100)))


2022-09-22 18:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.