import random as rd import numpy as np %timeit a = [rd.random() for x in range(1000)] %timeit a = [np.random.random() for x in range(1000)] %timeit a = np.random.random(1000) %%time # This is a terrible way to do this fib = [0, 1] for i in range(10**5): fib.append(fib[-1] + fib[-2]) from scipy.special import jn x = linspace(0, 4*pi) for i in range(6): plot(x, jn(i, x)) import random as rd N = 4 lol = [[rd.random() for c in range(N)] for r in range(N)] # OR lol = [] for r in range(N): row = [] for c in range(N): row.append(rd.random()) lol.append(row) lol lol[:,0] lol[:][0] [x[0] for x in lol] import numpy as np npm = np.random.random((N, N)) npm npm[:,0] npm.sum(0) import numpy import scipy.ndimage import Image class Life(object): def __init__(self, n, p=0.5, mode='wrap'): self.n = n self.mode = mode self.array = numpy.uint8(numpy.random.random((n, n)) < p) self.weights = numpy.array([[1,1,1], [1,10,1], [1,1,1]], dtype=numpy.uint8) def step(self): con = scipy.ndimage.filters.convolve(self.array, self.weights, mode=self.mode) boolean = (con==3) | (con==12) | (con==13) self.array = numpy.int8(boolean) def run(self, N): for _ in range(N): self.step() def draw(self, scale): im = Image.fromarray(numpy.uint8(self.array)*255) z = int(scale*self.n) return im.resize((z,z)) l = Life(50) imshow(l.draw(15)) l.run(10) imshow(l.draw(15)) from scipy import stats import numpy as np x = np.random.random(10) y = np.random.random(10) slope, intercept, r_value, p_value, std_err = stats.linregress(x,y) print "r-squared:", r_value**2 from scipy import stats import numpy as np x = np.random.random(10) y = np.random.random(10) slope, intercept, r_value, p_value, std_err = stats.linregress(x,y) print "r-squared:", r_value**2 import matplotlib.pyplot as plt sizes = 1000* np.random.random(10) colors = np.random.random(10) fit_x = np.linspace(0,1,100) fit_y = slope * xx + intercept plt.scatter(x,y, sizes, colors, alpha=0.5) plt.plot(fit_x, fit_y, '--r') plt.title("Fit line to random junk", fontsize=16) plt.show() import sympy sympy.init_printing(use_latex=True) ##or use_unicode=True in a console x, y = sympy.symbols('x y') sympy.diff(sympy.exp(x**2), x) my_deriv = sympy.Derivative(sympy.exp(x**2), x, x) my_deriv my_deriv.doit() sympy.Integral(sympy.exp(-x**2 - y**2), x, y) from sympy import oo sympy.integrate(sympy.exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo)) sympy.solve([x*y - 7, x + y - 6], [x, y])