import time
import numpy as np
from multiprocessing import Pool
from concurrent.futures import ProcessPoolExecutor
deff1(x):
a, b = x
return a * b
deff2(a, b):
return a * b
defmain():
p1 = Pool(4)
p2 = ProcessPoolExecutor(4)
a, b = np.random.rand(2, 10**2, 10**2)
alst = [a+x for x inrange(1000)]
blst = [b+x for x inrange(1000)]
t1 = time.time()
result1 = p1.map(f1, zip(alst, blst))
t2 = time.time()
print("Pool:{:.6f}".format(t2 - t1))
t1 = time.time()
result2 = list(p2.map(f2, alst, blst))
t2 = time.time()
print("ProcessPoolExecutor:{:.6f}".format(t2 - t1))
print(np.array_equal(result1, result2))
if __name__ == "__main__":
main()
# 結果""" =>Pool:0.586397ProcessPoolExecutor:1.137316True"""
import time
import numpy as np
from multiprocessing import Pool
from concurrent.futures import ProcessPoolExecutor
classF:
deff1(self, x):
a, b = x
return a * b
deff2(self, a, b):
return a * b
defmain():
p1 = Pool(4)
p2 = ProcessPoolExecutor(4)
a, b = np.random.rand(2, 10**2, 10**2)
alst = [a+x for x inrange(1000)]
blst = [b+x for x inrange(1000)]
f = F()
t1 = time.time()
result1 = p1.map(f.f1, zip(alst, blst))
t2 = time.time()
print("Pool:{:.6f}".format(t2 - t1))
t1 = time.time()
result2 = list(p2.map(f.f2, alst, blst, chunksize=128))
t2 = time.time()
print("ProcessPoolExecutor:{:.6f}".format(t2 - t1))
print(np.array_equal(result1, result2))
if __name__ == "__main__":
main()
import time
import numpy as np
from multiprocessing import Pool
from concurrent.futures import ProcessPoolExecutor
deff1(x):
a, b = x
return a * b
deff2(a, b):
return a * b
p1 = Pool(4)
p2 = ProcessPoolExecutor(4)
a, b = np.random.rand(2, 10**2, 10**2)
alst = [a+x for x inrange(1000)]
blst = [b+x for x inrange(1000)]
t1 = time.time()
result1 = p1.map(f1, zip(alst, blst))
t2 = time.time()
print("Pool:{:.6f}".format(t2 - t1))
t1 = time.time()
result2 = list(p2.map(f2, alst, blst, chunksize=128))
t2 = time.time()
print("ProcessPoolExecutor:{:.6f}".format(t2 - t1))
print(np.array_equal(result1, result2))
>>> "hoge".find("fuga")
-1
>>> "hoge".index("fuga")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
Returns a condensed distance matrix Y. For each and (where ),where m is the number of original observations. The metric dist(u=X[i], v=X[j]) is computed and stored in entry ij.
from collections import defaultdict
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import linkage, fcluster
defclustering_fcluster():
X, y = gen_data()
S = pdist(X)
Z = linkage(S, method="average")
result = fcluster(Z, t=4, criterion="maxclust")
d = defaultdict(list)
for i, r inenumerate(result):
d[r].append(i)
for k, v in d.items():
print(k, v)
if __name__ == "__main__":
clustering_fcluster()
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import linkage, dendrogram
defclustering_dendrogram():
X, y = gen_data()
S = pdist(X)
Z = linkage(S, method="average")
dendrogram(Z)
plt.savefig("dendro1.png")
if __name__ == "__main__":
clustering_dendrogram()
from scipy.spatial.distance import pdist
from scipy.cluster.hierarchy import linkage, dendrogram
defclustering_dendrogram2():
X, y = gen_data()
S = pdist(X)
Z = linkage(S, method="average")
dendrogram(Z, color_threshold=31)
plt.savefig("dendro2.png")
if __name__ == "__main__":
clustering_dendrogram2()
>>> d1 = {x:[x] for x inrange(0, 3)}
>>> d2 = {x:[x] for x inrange(1, 4)}
>>> {k:d1[k] if k in d1 else d2[k] for k in d1.keys() | d2.keys()}
{0: [0], 1: [1], 2: [2], 3: [3]}