<jupyter_start><jupyter_text>import numpy as np   Numpy library<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
a = np.random.choice(np.arange(0,10),10000)
plt.hist(a,bins=np.arange(0,11,1))

plt.hist(a,bins=np.arange(0,11,1),density=True)
#  Use Bins 1/2 wide - What does plot meean? 
plt.hist(a,bins=np.arange(0,11,0.5),density=True)













import numpy as np
import matplotlib.pyplot as plt

a = np.arange(0,10,0.1)
y = np.sin(a)
plt.plot(a,y)
# Data as sampling from an unseen population
# Choose at random from 1 through 10 
import numpy as np
import matplotlib.pyplot as plt
a = np.random.choice(np.arange(0,10),100)
a = np.random.random(100)*10.0
a



# Normal Data 

a = np.random.normal(10,2,10)
plt.hist(a,bins=np.arange(5,16,1),density=True)
#plt.scatter(np.arange(0,10),a)


    









plt.hist(a,bins=np.arange(5,16,0.1), density=True)









plt.hist(a,bins=np.arange(5,16,1))


import numpy as np
import matplotlib.pyplot as plt
a = np.random.normal(0,1,20)
plt.hist(b, bins=np.arange(0,20,1), density = True)<jupyter_output><empty_output><jupyter_text>Exercise - Produce a 10x10 checkerboard of 1s and 0s<jupyter_code>import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap as lc
Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
print(Z)
sns.heatmap(Z, annot=True,linewidths=5,cbar=False)
import seaborn as sns
sns.heatmap(Z, annot=True,linewidths=5,cbar=False)



import numpy as np
import scipy as scipy
from scipy.stats import norm

plt.style.use('ggplot')
fig, ax = plt.subplots()
x= np.arange(34,40,0.01)
y = np.random.normal(x)
lines = ax.plot(x, norm.pdf(x,loc=37,scale=1))
ax.set_ylim(0,0.45) # range

ax.set_xlabel('x',fontsize=20) # set x label
ax.set_ylabel('pdf(x)',fontsize=20,rotation=90) # set y label
ax.xaxis.set_label_coords(0.55, -0.05) # x label coordinate
ax.yaxis.set_label_coords(-0.1, 0.5) # y label coordinate
px=np.arange(36,37,0.1)
plt.fill_between(px,norm.pdf(px,loc=37,scale=1),color='r',alpha=0.5)
plt.show()






a = np.random.normal(10,1,20)
a<jupyter_output><empty_output><jupyter_text>Calculate the mean and subtract the mean from each data value$$<jupyter_code>from matplotlib import collections as matcoll
Npoints = 20
x = np.arange(0,Npoints)
y = np.random.normal(loc=10, scale=2, size=Npoints )
lines = []
for i in range(Npoints):
    pair=[(x[i],0), (x[i], y[i])]
    lines.append(pair)
linecoll = matcoll.LineCollection(lines)
fig, ax = plt.subplots()
ax.add_collection(linecoll)

plt.scatter(x,y, marker='o', color='blue')
plt.xticks(x)
plt.ylim(0,40)
plt.show()
ylim=(0,10)<jupyter_output><empty_output><jupyter_text>Numpy 2D Arrays<jupyter_code>import numpy as np
# Numpy 2_D Arrays
a = [0,1,2]
b = [3,4,5]
c = [6,7,8]
z = [a, 
     b, 
     c]

a = np.arange(0,9)
z = a.reshape(3,3)
z

z[2,2]
z[0:3:2,0:3:2]<jupyter_output><empty_output><jupyter_text>Numpy Arrays   Slices point back to the original array<jupyter_code>import numpy as np

a = np.ones(10)
a[:] = 2
b = a[0:2]   # View of a NOT a copy
b[:] = 3
b = a.copy()
b[:] = 6
print(id(a), id(b))

mybool = a < 3
c = a[a < 3]     # Conditionals 
c<jupyter_output>140350370072496 140350375711808<jupyter_text>generate log normal dist and sample<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
mu, sigma = 3., 1. # mean and standard deviation
s = np.random.lognormal(mu, sigma, 1000)
plt.hist(s,bins=20)
means = []
N = 100
a = [5,20,50]
fig, axs = plt.subplots(3)
fig.suptitle('Sampling of a Log-Normal Distribution')
fig.tight_layout()
for k,N in enumerate(a):
    for i in np.arange(1000):
        means.append(np.random.choice(s,N).sum()/N)
    axs[k].hist(means,bins=20)
   

mu, sigma = 50., 1. # mean and standard deviation
s = np.random.normal(mu, sigma, 1000)
s = np.sort(s)
means = []
plt.plot(range(0,1000), (s - mu)/np.sqrt(sigma),'.')
plt.title('Z-Score')
N = 100
a = [5,20,50]
fig, axs = plt.subplots(3)
fig.suptitle('Sampling of a Normal Distribution')
fig.tight_layout()
for k,N in enumerate(a):
    for i in np.arange(1000):
        means.append(np.random.choice(s,N).sum()/N)
    axs[k].hist(var,bins=20)<jupyter_output><empty_output><jupyter_text>$$\sigma_{mean}^{2} =  \frac{\sigma_{pop}^{2}} {N}$$where N is sample size$$\sigma_{mean} =  \frac{\sigma_{pop}} {\sqrt{N}}$$<jupyter_code>import scipy.stats as stats

s = np.random.randint(1,1000,1000)

sampArr = np.arange(2,3,1)
skip = 2
fig, axes = plt.subplots(nrows=np.int((1+len(sampArr))/skip), ncols=skip)
fig.suptitle('Sampling Mean of a Uniform Distribution')
fig.set_size_inches(10, 10)
fig.tight_layout()
Nloop = 1000

for k,S in enumerate(sampArr):   # choose number S in a sample
    means = []    # fill this with Nloop samples
    for i in np.arange(Nloop):
        mean = np.random.choice(s,S).mean()
        means.append(mean)
    df = pd.DataFrame(means)
    right = k%skip
    row = np.int(k/skip)
    
    df.plot(ax=axes[row,right], kind = "kde")
    axes[row,right].set_title(f'sample= {S}')


import pandas as pd
y = np.array([1,2,4,4,4,2,5,5,1])
df = pd.DataFrame(y)
df.plot()
df.plot(kind = "kde")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16943521594684385, 'fraction_numerical': 0.057455540355677154, 'mean_word_length': 4.6866666666666665, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '376933', 'license': 'MIT', 'n_tokens_mistral': 2386, 'n_tokens_neox': 2182, 'n_words': 452, 'path': 'NumpyIntro.ipynb', 'repo_name': 'johntango/jupyterCourse100', 'size': '5117'}