Foo
This commit is contained in:
BIN
codes/partition/__pycache__/variance.cpython-39.pyc
Normal file
BIN
codes/partition/__pycache__/variance.cpython-39.pyc
Normal file
Binary file not shown.
@ -4,6 +4,7 @@ from collections import defaultdict
|
||||
import hashlib
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import variance
|
||||
|
||||
# int(ipaddress.IPv4Address("192.168.0.1"))
|
||||
|
||||
@ -26,65 +27,74 @@ def partition(crawlers, ips):
|
||||
|
||||
|
||||
def main():
|
||||
ips = load_ips('./ips.csv')
|
||||
# ips = load_ips('./ips.csv')
|
||||
ips = load_ips('./random.csv')
|
||||
c2 = ['c0', 'c1']
|
||||
c4 = ['c0', 'c1', 'c2', 'c3']
|
||||
c6 = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5']
|
||||
c10 = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']
|
||||
c100 = []
|
||||
for i in range(100):
|
||||
c100.append(f'c{i:02d}')
|
||||
|
||||
for cs in [c2, c4, c6, c10]:
|
||||
# for cs in [c2, c4, c6, c10]:
|
||||
for cs in [c100]:
|
||||
print(f'{len(cs)} crawlers')
|
||||
|
||||
part = partition(cs, ips)
|
||||
|
||||
ind = np.arange(len(cs))
|
||||
fig, ax = plt.subplots()
|
||||
crawlers = sorted(cs)
|
||||
|
||||
ax.invert_yaxis()
|
||||
ax.set_ylabel('Crawlers')
|
||||
ax.set_xlabel('Peers')
|
||||
ax.set_title(f'IP Partitioning for {len(cs)} crawlers')
|
||||
ax.set_yticks(ind)
|
||||
|
||||
# ax.set_xticks(ind) #, labels=cs)
|
||||
|
||||
ns = []
|
||||
|
||||
for c, i in sorted(part.items(), key=lambda kv: kv[0]):
|
||||
print(f'\t{c}: {len(i)}')
|
||||
ns.append(len(i))
|
||||
|
||||
# p1 = ax.bar(ind, ns, 0.35)
|
||||
hbars = ax.barh(ind, ns)
|
||||
ax.bar_label(hbars, label_type='center')
|
||||
# plt.show()
|
||||
plt.savefig(f'./ip_part_c{len(cs):02}.png')
|
||||
counts = [len(v) for k, v in part.items()]
|
||||
variance.variance(counts)
|
||||
# for c, i in sorted(part.items(), key=lambda kv: kv[0]):
|
||||
|
||||
|
||||
# p1 = ax.bar(ind, i, 0.35, label=c)
|
||||
# ind = np.arange(len(cs))
|
||||
# fig, ax = plt.subplots()
|
||||
# crawlers = sorted(cs)
|
||||
|
||||
# ax.invert_yaxis()
|
||||
# ax.set_ylabel('Crawlers')
|
||||
# ax.set_xlabel('Peers')
|
||||
# ax.set_title(f'IP Partitioning for {len(cs)} crawlers')
|
||||
# ax.set_yticks(ind)
|
||||
|
||||
# # ax.set_xticks(ind) #, labels=cs)
|
||||
|
||||
# ns = []
|
||||
|
||||
# for c, i in sorted(part.items(), key=lambda kv: kv[0]):
|
||||
# print(f'\t{c}: {len(i)}')
|
||||
# ns.append(len(i))
|
||||
|
||||
# # p1 = ax.bar(ind, ns, 0.35)
|
||||
# hbars = ax.barh(ind, ns)
|
||||
# ax.bar_label(hbars, label_type='center')
|
||||
# # plt.show()
|
||||
# plt.savefig(f'./ip_part_c{len(cs):02}.png')
|
||||
|
||||
|
||||
# people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
|
||||
# # p1 = ax.bar(ind, i, 0.35, label=c)
|
||||
|
||||
|
||||
# # people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
|
||||
|
||||
|
||||
|
||||
# hbars = ax.barh(y_pos, performance, xerr=error, align='center')
|
||||
# ax.set_yticks(y_pos, labels=people)
|
||||
# ax.invert_yaxis() # labels read top-to-bottom
|
||||
# ax.set_xlabel('Performance')
|
||||
# ax.set_title('How fast do you want to go today?')
|
||||
# # hbars = ax.barh(y_pos, performance, xerr=error, align='center')
|
||||
# # ax.set_yticks(y_pos, labels=people)
|
||||
# # ax.invert_yaxis() # labels read top-to-bottom
|
||||
# # ax.set_xlabel('Performance')
|
||||
# # ax.set_title('How fast do you want to go today?')
|
||||
|
||||
# # Label with specially formatted floats
|
||||
# ax.bar_label(hbars, fmt='%.2f')
|
||||
# ax.set_xlim(right=15) # adjust xlim to fit labels
|
||||
# # # Label with specially formatted floats
|
||||
# # ax.bar_label(hbars, fmt='%.2f')
|
||||
# # ax.set_xlim(right=15) # adjust xlim to fit labels
|
||||
|
||||
# plt.show()
|
||||
# # plt.show()
|
||||
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
# print()
|
||||
# print()
|
||||
# print()
|
||||
# print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
51
codes/partition/variance.py
Normal file
51
codes/partition/variance.py
Normal file
@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from math import sqrt
|
||||
# from itertools import islice
|
||||
|
||||
def variance(xs):
|
||||
n = len(xs)
|
||||
mu = sum(xs) / n
|
||||
# mu = 265.8
|
||||
# print(sum(map(lambda x: (x - mu)**2, xs)))
|
||||
s = sum(map(lambda x: (x - mu)**2, xs))
|
||||
var = s / n
|
||||
deriv = sqrt(var)
|
||||
# var = sqrt( / len(xs))
|
||||
|
||||
print('\\begin{align*}')
|
||||
print(f' n &= {n} \\\\')
|
||||
print(f' \\mu &= \\frac{{{sum(xs)}}}{{n}} = {mu:.1f} \\\\')
|
||||
print(f' s &= \\sum\\limits_{{i=1}}^{{n}} {{(x_i - \\mu)}}^2 \\\\')
|
||||
# print [mylist[i:i+4] ]
|
||||
first = True
|
||||
for i in range(0, n, 3):
|
||||
symbol = '=' if first else '+'
|
||||
l = xs[i:i+3]
|
||||
a = ' + '.join(map(lambda x: f'{{({x} - {mu:.1f})}}^2', l))
|
||||
first = False
|
||||
print(f' &{symbol} {a} \\\\')
|
||||
|
||||
print(f' &= {s:.1f} \\\\')
|
||||
|
||||
# a = ' + '.join(map(lambda x: f'({x} - {mu})^2', xs))
|
||||
print(f' \\sigma^2 &= \\frac{{s}}{{n}} = {var:.1f} \\\\')
|
||||
print(f' \\sigma &= \\sqrt{{\\sigma^2}} = {deriv:.1f}')
|
||||
print('\\end{align*}')
|
||||
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
print()
|
||||
|
||||
# \mu &= 1595 / 4 = 398.75 \\
|
||||
# \sigma &= \sqrt{\frac{(403 - 398.75)^2 + (369 - 398.75)^2 + (405 - 398.75)^2 + (418 - 398.75)^2}{4}} = 10.5
|
||||
|
||||
def main():
|
||||
# for xs in [[808, 787], [403, 369, 405, 418], [258, 273, 257, 264, 293, 250], [140, 175, 186, 166, 159, 152, 172, 148, 151, 146]]:
|
||||
# for xs in [[258, 273, 257, 264, 293, 250]]:
|
||||
for xs in [[499322, 500678], [249504, 250451, 249818, 250227], [166430, 166861, 166269, 166937, 166623, 166880], [100424, 99650, 99307, 100305, 99403, 100562, 100277, 99875, 99911, 100286]]:
|
||||
variance(xs)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user