6/19/2021

Python Numpy

 # Numpy is imported, seed is set

 import numpy as np

# average

np.mean()

# Specify array of percentiles: percentiles
percentiles = np.array([2.5, 25, 50, 75, 97.5])

# Compute percentiles: ptiles_vers
ptiles_vers = np.percentile(some_array)
 
# Array of differences to mean: differences
differences = versicolor_petal_length - np.mean(some_array)

# Square the differences: diff_sq
diff_sq = differences ** 2

# Compute the mean square difference: variance_explicit
variance_explicit = np.mean(diff_sq)

# Compute the variance using NumPy: variance_np
variance_np = np.var(some_array)
 
# Compute the variance: variance
variance = np.var(some_array)

# Print the square root of the variance
print(np.sqrt(variance))

# Print the standard deviation
print(np.std(some_array))
 
# Compute the covariance matrix: covariance_matrix
covariance_matrix = np.cov(versicolor_petal_length, versicolor_petal_width)

# Extract covariance of length and width of petals: petal_cov
petal_cov = covariance_matrix[0,1]

def pearson_r(x, y):
"""Compute Pearson correlation coefficient between two arrays."""
# Compute correlation matrix: corr_mat
corr_mat = np.corrcoef(x,y)

# Return entry [0,1]
return corr_mat[0,1]
 
random_numbers = np.empty(100000)

# Set the seed
np.random.seed(123)

# Generate and print random float

print(np.random.random(size = 4)) 
print(np.random.rand())
print(np.random.randint(1,7))

# Take 10,000 samples out of the binomial distribution (n=100,p=0.05): n_defaults
n_defaults = np.random.binomial(100, 0.05, size=10000)
 
# Draw 10,000 samples out of Poisson distribution:
samples_poisson = np.random.poisson(10, size=10000) 
 
# Take 10,000 samples out of the normal distribution of mean=100, std=0.5
n_defaults = np.random.binomial(100, 0.5, size=10000)
 
# Draw samples out of an exponential distribution
sample = np.random.exponential(mean, size=1)

np.percentile(bs_slope_reps, [2.5, 97.5]))
 

# Initialize random_walk

random_walk = [0] 

for x in range(100) :

    step = random_walk[-1]

    dice = np.random.randint(1,7)

    if dice <= 2:

        # use max to make sure step can't go below 0

        step = max(0step -1)

    elif dice <= 5:

        step = step + 1

    else:

        step = step + np.random.randint(1,7)

    random_walk.append(step)

print(random_walk)


def draw_bs_reps(data, func, size=1):
"""Draw bootstrap replicates."""
# Initialize array of replicates: bs_replicates
bs_replicates = np.empty(size)
# Generate replicates
for i in range(size):
bs_replicates[i] = bootstrap_replicate_1d(data,func)
return bs_replicates
 
 
def draw_bs_pairs(x, y, func, size=1):
"""Perform pairs bootstrap for a single statistic."""
# Set up array of indices to sample from: inds
inds = np.arange(len(x))
# Initialize replicates: bs_replicates
bs_replicates = np.empty(size)
# Generate replicates
for i in range(size):
bs_inds = np.random.choice(inds,len(inds))
bs_x, bs_y = x[i], y[i]
bs_replicates[i] = func(bs_x, bs_y)
return bs_replicates

def permutation_sample(data1, data2):
"""Generate a permutation sample from two data sets."""
# Concatenate the data sets: data
data = np.concatenate((data1,data2))
# Permute the concatenated array: permuted_data
permuted_data = np.random.permutation(data)
# Split the permuted array into two: perm_sample_1, perm_sample_2
perm_sample_1 = permuted_data[:len(data1)]
perm_sample_2 = permuted_data[len(data1):]
return perm_sample_1, perm_sample_2

def draw_perm_reps(data_1, data_2, func, size=1):
"""Generate multiple permutation replicates."""
# Initialize array of replicates: perm_replicates
perm_replicates = np.empty(size)
for i in range(size):
# Generate permutation sample
perm_sample_1, perm_sample_2 = permutation_sample(data_1,data_2)
# Compute the test statistic
perm_replicates[i] = func(perm_sample_1, perm_sample_2)
return perm_replicates

# Compute mean of all forces: mean_force
mean_force = np.mean(forces_concat)
# Generate shifted arrays
force_a_shifted = force_a - np.mean(force_a) + mean_force
force_b_shifted = force_b - np.mean(force_b) + mean_force
# Compute 10,000 bootstrap replicates from shifted arrays
bs_replicates_a = draw_bs_reps(force_a_shifted,np.mean, 10000)
bs_replicates_b = draw_bs_reps(force_b_shifted, np.mean, 10000)
# Get replicates of difference of means: bs_replicates
bs_replicates = bs_replicates_a-bs_replicates_b
# Compute and print p-value: p
p = np.sum(bs_replicates>=(np.mean(force_a-force_b))) / 10000
print('p-value =', p)

# Construct arrays of data: dems, reps
dems = np.array([True] * 153 + [False] * 91)
reps = np.array([True] * 136 + [False] * 35)
def frac_yea_dems(dems, reps):
"""Compute fraction of Democrat yea votes."""
frac = np.sum(dems) / len(dems)
return frac

# Acquire permutation samples: perm_replicates
perm_replicates = draw_perm_reps(dems,reps, frac_yea_dems, 10000)
# Compute and print p-value: p
p = np.sum(perm_replicates <= 153/244) / len(perm_replicates)
print('p-value =', p)

 # Compute the observed difference in mean inter-no-hitter times: nht_diff_obs

nht_diff_obs = diff_of_means(nht_dead,nht_live)
# Acquire 10,000 permutation replicates of difference in mean no-hitter time: perm_replicates
perm_replicates = draw_perm_reps(nht_dead, nht_live, diff_of_means, size=10000)
# Compute and print the p-value: p
p = np.sum(perm_replicates <= nht_diff_obs)/10000
print('p-val =', p)

没有评论: