OBJECTIVES
#import libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#define a function
def f(x): return x**2
#plot the function
x = np.linspace(-3, 3, 1000)
plt.plot(x, f(x))
#square roots, trigonometric, exponential, and subplots
fig, ax = plt.subplots(1, 3, figsize = (16, 4))
def s(x): return np.sqrt(x)
def sin(x): return np.sin(x)
def e(x): return np.e**x
ax[0].plot(x, s(x))
ax[1].plot(x, sin(x))
ax[2].plot(x, e(x));
#tables of values
functions = pd.DataFrame({'x': x, 's(x)': s(x), 'sin(x)': sin(x), 'e^x': e(x)})
functions.head()
In mathematics, a combination is a selection of items from a collection, such that the order of selection does not matter (unlike permutations). For example, given three fruits, say an apple, an orange and a pear, there are three combinations of two that can be drawn from this set: an apple and a pear; an apple and an orange; or a pear and an orange. More formally, a k-combination of a set S is a subset of k distinct elements of S. If the set has n elements, the number of k-combinations is equal to the binomial coefficient
which can be written using factorials as whenever , and which is zero when . The set of all k-combinations of a set S is often denoted by .
#import combination function
from scipy.special import comb
#3 choose 2
comb(3, 2)
#3 choose 0, 1, 2, 3
[comb(3, i) for i in [0, 1, 2, 3]]
#build the triangle
for i in range(10):
print([comb(i, j) for j in range(i + 1)])
#row 7
row_seven = [1.0, 7.0, 21.0, 35.0, 35.0, 21.0, 7.0, 1.0]
#plot the row
plt.bar(range(8), row_seven)
#total
sum(row_seven)
#probability of 3 successes
35/128
#binomial distribution
#import
import scipy.stats as stats
b = stats.binom(7, .5)
b.pmf(3)
#example
plt.bar(range(8), b.pmf(range(8)))