import numpy as np
import matplotlib.pyplot as plt
Run to view results
import random
A, B = 10, 20
random.uniform(A,B)
Run to view results
A = 10
B = 20
m = 3
n = 4
# using just the Python standard library random
# YOUR CODE HERE
s = np.array([[random.uniform(A, B) for _ in range(n)] for _ in range(m)])
print(s)
# using numpy
# YOUR CODE HERE
s = np.random.uniform(A, B, size=(m, n))
print(s)
Run to view results
random.gauss(5,1)
Run to view results
mu, sigma = 2, 0.3 # mean and standard deviation
s = np.random.normal(mu, sigma, 10000)
print('s.shape:', s.shape)
print(s[:10])
Run to view results
# Is the mean equal to mu?
abs(mu - np.mean(s))
Run to view results
# is the standard deviation sigma?
abs(sigma - np.std(s, ddof=1))
Run to view results
abs(sigma - np.std(s))
Run to view results
s = np.random.normal(mu, sigma, 10000)
bins = 30
count, bins, ignored = plt.hist(s, bins, density=True)
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
linewidth=2, color='r')
Run to view results
s = np.random.normal(3, 2.5, size=(2, 4))
print('s.shape:', s.shape)
print(s)
Run to view results
def randv2n(n1,mu1,sigma1,n2,mu2,sigma2):
array = np.array([random.gauss(mu1,sigma1) for _ in range(n1)] + [random.gauss(mu2,sigma2) for _ in range(n2)])
np.random.shuffle(array)
return array
print(randv2n(3,-10,1,4,10,1))
Run to view results
rv = randv2n(30,-10,1,40,10,1)
print(rv)
# plot the histogram of the vector rv
# YOUR CODE HERE
_ = plt.hist(rv)
Run to view results
def randn2d(n, mu1, sigma1, mu2, sigma2, draw):
# generate a random array with 2 columns
# n[i] is the number of rows from cluster i
# the first coordinate of elements from cluster i are generated from N(mu1[i],sigma1[i])
# the second coordinate of elements from cluster i are generated from N(mu2[i],sigma2[i])
# the retured array has shuffled rows
# YOUR CODE HERE
raise NotImplementedError()
res = randn2d([10,4,32], [1, 5, 20], [.3, .3, 2.1], [1, 11, 9], [.3, 0.1, 0.7], draw="yes")
Run to view results
def selectk(x, k):
assert len(x) >= k
rows = np.random.choice(x.shape[0], k, replace=False)
return x[rows]
pass
Run to view results
x = np.random.normal(3, 2.5, size=(10,3))
print(x)
Run to view results
selected = selectk(x,4)
print(selected)
# 3D scatter plot
# YOUR CODE HERE
raise NotImplementedError()
Run to view results
from sklearn.model_selection import train_test_split
train, test = train_test_split(x)
print("train:\n", train)
print("test:\n", test)
Run to view results
train, test = train_test_split(x, random_state=12345)
print("train:\n", train)
print("test:\n", test)
Run to view results
train, test = train_test_split(x, test_size=0.5, random_state=12345)
print("train:\n", train)
print("test:\n", test)
Run to view results
Run to view results