%pylab inline %load_ext sympyprinting import numpy as np a = np.array([23, 29, 20, 32, 23, 21, 33, 25]) print a.sum() a.size mean = a.sum(dtype=float) / a.size print mean print np.mean(a) assert mean == np.mean(a) print a.sort() print np.median(a) np.bincount(a).argmax() np.angle( c = np.array([3, 2, 7, 9, 5, 1, 2]) print np.median(c) c = np.array([5, 6, 6, 2, 9]) print np.mean(c) c = np.array([2, 5, 10, 9, 2, 9, 4, 9]) np.bincount(c).argmax() c = np.array([9, 7, 6, 6, 6, 8, 8, 4, 6, 2]) print np.mean(c) c = np.array([10, 3, 2, 5, 1, 8, 1, 9, 7]) print np.median(c) c = np.array([8, 6, 5, 9, 10, 1, 2, 4, 10]) np.mean(c) c = np.array([6, 2, 2, 5, 1, 2, 8, 8]) np.bincount(c).argmax() c = np.array([4, 7, 1, 9, 8, 6, 1]) np.median(c) b = (81 + 81 + 81 + 81 + 91) / 5 b c = np.array([85, 77, 94, 88, 91]) np.mean(c) c = np.array([82, 82, 82, 82, 100, 100]) np.mean(c) c = np.array([83, 98, 80, 81, 91, 95]) np.mean(c) c = np.array([82, 82, 82, 90]) np.mean(c) c = np.array([92, 88, 86, 95, 97, 76]) np.mean(c) c = np.array([85, 85, 85, 100, 100]) np.mean(c) c = np.array([84, 84, 84, 96]) np.mean(c) d = np.array([14, 6, 3, 2, 4, 15, 11, 8, 1, 7, 2, 1, 3, 4, 10, 22, 20], dtype=float) boxplot(d, vert=False, ) d = np.array([3, 5, 6, 6, 7, 7, 7, 7, 7, 8, 8, 9, 9, 10, 11], dtype=float) boxplot(d, vert=False) sample = [1, 3, 5, 7, 14] population_mean = sum(sample)/len(sample) print population_mean def population_variance(population): mean = np.mean(population) pv = sum([(float(i) - mean)**2 for i in population]) / len(population) return pv population_variance(sample) # Hours of television sample = [1.5, 4, 1, 2.5, 2, 1] print 'mean: %s' % np.mean(sample) print 'variance: %s' % population_variance(sample) def unbiased_variance(sample): mean = np.mean(sample) pv = sum([(float(i) - mean)**2 for i in sample]) / (len(sample) - 1) return pv print 'sample variance: %s' % unbiased_variance(sample) sample = [8,10,16,2,23,4] print '%.2f years old' % np.mean(sample) print '%.2f years^2' % population_variance(sample) sample = [30,17,1,3,11] print '%.2f years old' % np.mean(sample) print '%.2f years^2' % unbiased_variance(sample) sample = [1, 2, 3, 8, 7] from IPython.display import display, Math, Latex from sympy.printing.python import python import sympy as sym display(Math(latex('\mu = %.2f' % np.mean(sample)))) display(Math(latex('\sigma^2 = %.2f' % population_variance(sample)))) display(Math(latex('s^2_{n-1} = %.2f' % ( unbiased_variance(sample))))) math.sqrt(7.76) sample = [30,17,1,3,11] mean = np.mean([float(i) for i in sample]) print '%.2f' % mean v = unbiased_variance([float(i) for i in sample]) print '%.2f' % v print '%.2f' % math.sqrt(v) sample = [8,10,16,2,23,4] mean = np.mean([float(i) for i in sample]) print '%.2f' % mean v = variance([float(i) for i in sample]) print '%.2f' % v print '%.2f' % math.sqrt(v) display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')) from sympy.printing.python import python import sympy as sym x, y, z = sym.symbols("x y z") print latex(Rational(3,2)*pi + exp(I*x) / (x**2 + y)) display(Math(latex('%s^2' % sym.Symbol("s_n"))))