<jupyter_start><jupyter_text>args and kwargs<jupyter_code>def summation(*args):
    if len(args) is not 0:
        temp = 0
        for arg in args:
            temp += arg
    else:
        return 0
    return temp
def printName(**kwargs):
    f_name = kwargs["name_f"]
    l_name = kwargs["name_l"]
    print(f_name,l_name)
printName(name_f = "ashish", name_l = "yadav")<jupyter_output><NAME><jupyter_text>Lists are passed by reference<jupyter_code>def update(n, x):
    n = 2
    x.append(4)
    print('Update: ', n, x)
def main():
    n = 1 
    x = [0,1,2,3]
    print('Main: ', n, x)
    update(n,x)
    print('Main: ', n, x)
main()
def increment(n):
   n += 1
   return n

n = 1
while n < 10:
   n = increment(n)
print(n)
class MyList(list):
    def remove_min(self):
        self.remove(min(self))
    def remove_max(self):
        self.remove(max(self))
        
x = [1,2,3,4,5,6,7,8,9]
y = MyList(x)
dir(y)
y.remove_min()
y
y.remove_max()
y<jupyter_output><empty_output><jupyter_text>Numpy  Basic ops<jupyter_code>import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 2, 5])
A = np.array([x, y])
A
zero_vector = np.zeros(5, dtype=int)
zero_matrix = np.zeros((3, 4))
one_matrix = np.ones((3, 4))
empty_matrix = np.empty((3, 4))
zero_vector
# zero_matrix
# one_matrix
# empty_matrix 
# Taking transpose of a matrix
A.transpose()
A.transpose().transpose()
a = np.random.rand(4,)  # -------- Equivalent to a vector or a row matrix
b = np.random.rand(4,1) # -------- Equivalent to a column matrix
print(a)
print(b)<jupyter_output>[0.5084765  0.55040343 0.6016749  0.25926459]
[[0.57549929]
 [0.79482957]
 [0.39467057]
 [0.5996772 ]]<jupyter_text>Slicing in Numpy arrays<jupyter_code>x[2]
# stop index is not included
x[0:3]
x[:]
z = x + y
z
A[:,0]
A[:, 0]
A[0,:]
# array([[1, 2, 3], ------- First row, index = 0
#        [4, 2, 5]]) ------- Second row, index = 1
#         0  1  2     ------- Column Indices

A[0,2]
# Column matrix shape = (row, )
# Sum of A's first and second column
(A[:,1]+A[:,0]).shape<jupyter_output><empty_output><jupyter_text>Building numpy arrays<jupyter_code>x = np.linspace(1,50,10)
x
y = np.logspace(np.log10(10),np.log10(100),10)
y
if (np.any(x>0.9)):
    print("X has element greater than 0.9")
else:
    print("X has no element greater than 0.9")
if (np.any(x>=0.1)):
    print("X's all elements are greater than or equal to 0.1")
else:
    print("X has at-least one elements less than 0.1")
x.shape
x.size
x = 20
not np.any([x%i == 0 for i in range(2, x)])
z = [x%i == 0 for i in range(1,20)]
z<jupyter_output><empty_output><jupyter_text>Matplotlib and PyPlot  Basic PyPlot<jupyter_code>import matplotlib.pyplot as plt
plt.plot([1,4,9,16,25,36,49,45,25,16,9,4])
plt.plot([1,4,9,16,25,36,49,45,25,16,9,4]);
x = np.linspace(0,100,20)
y = x ** 2.0
plt.plot(x,y)
plt.show()
plt.plot(x,y,"ro-",linewidth=1,)
plt.show()
x1 = np.linspace(0,1.5,20)
y1 = x1 ** 2.0
y2 = x1 ** 1.5
y3 = x1 ** 3.0
plt.plot(x1,y1,"bo-",linewidth=3,markersize=10)
plt.plot(x1,y3,"go--",linewidth=3,markersize=10)
plt.plot(x1,y2,"rs-",linewidth=1,markersize=8)
plt.show()
x1 = np.linspace(0,1,20)
y1 = x1 ** 2.0
y2 = x1 ** 1.5
y3 = x1 ** 3.0
plt.plot(x1,y1,"bo-",linewidth=3,markersize=10)
plt.plot(x1,y3,"go--",linewidth=3,markersize=10)
plt.plot(x1,y2,"rs-",linewidth=1,markersize=8)
plt.show()
x1 = np.linspace(1, 1.5, 20)
y1 = x1 ** 2.0
y2 = x1 ** 1.5
y3 = x1 ** 3.0
plt.plot(x1, y1, "bo-", linewidth=3, markersize=10, label="first")
plt.plot(x1, y3, "go--", linewidth=3, markersize=10, label="second")
plt.plot(x1, y2, "rs-", linewidth=1, markersize=8)
plt.legend(loc="upper left")
plt.xlabel("X")
plt.ylabel("Y")
# plt.show()
# plt.axis()
plt.savefig("myfig.jpg")
plt.plot([0,1,2],[0,1,4],"rd-");
plt.grid(True)
x1 = np.logspace(-1, 0, 20)
y1 = x1 ** 2.0
y2 = x1 ** 1.5
y3 = x1 ** 3.0
plt.semilogx(x1, y1, "bo-", linewidth=3, markersize=10, label="first", alpha=0.65) 
# alpha is used to reduce the sharpness of the color
plt.semilogy(x1, y3, "gd--", linewidth=3, markersize=10, label="second", alpha=0.65)
plt.loglog(x1, y2, "rs-", linewidth=1, markersize=8, label="third", alpha=0.65)
plt.legend(loc="upper left")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Demonstration")
plt.show()
x = np.logspace(0,1,10)
y = x**2
plt.loglog(x,y,"bo-");<jupyter_output><empty_output><jupyter_text>Histograms<jupyter_code>x = np.random.normal(size=1000)
plt.hist(x, density=True, cumulative=True, bins=30, alpha=0.75, facecolor="g");
plt.grid(True)
plt.title("Histogram: Cumulative and Normed")
np.random.seed(19680801)

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)

# the histogram of the data
n, bins, patches = plt.hist(x, 50, density=True, facecolor='g', alpha=0.75)


plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.xlim(40, 160)
plt.ylim(0, 0.03)
plt.grid(True)
plt.show()
x = np.random.gamma(2, 3, 100000)
plt.figure()
plt.subplot(221)
plt.hist(x, bins=30)
plt.grid(True)
plt.subplot(222)
plt.hist(x, density=True, bins=30)
plt.grid(True)
plt.subplot(223)
plt.hist(x, facecolor='g', alpha=0.75, density=True, cumulative=True, bins=30)
plt.grid(True)
plt.subplot(224)
plt.hist(x, facecolor='g', alpha=0.75, density=True,
         cumulative=True, histtype="step", bins=30)
plt.xlim(0,30)
plt.grid(True)
plt.show()

# Other syntax for subplot is :- plt.subplot(2,2,1)
# i.e. first subplot of the figure<jupyter_output><empty_output><jupyter_text>Randomness and Time  Simulating Randomness 1. simulating a process of rolling a die, and getting an outcome<jupyter_code>import random
random.choice([1,2,3,4,5,6,7,8,9,10])
random.choice(range(1,7))
random.choice(range(1,7))
random.choice(range(1,7))<jupyter_output><empty_output><jupyter_text>**Possible error while using range()**<jupyter_code>random.choice([range(1,7)])
list([1,2,3,4])
random.choice(list([1,2,3,4]))
import random

random.choice([(0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)])<jupyter_output><empty_output><jupyter_text>**Simulating a process of first chosing one of the three  dice and then rolling it, getting an outcome. Three dice have 6,8,10 faces respectively**<jupyter_code>random.choice(random.choice([range(1,7), range(1,9), range(1,11)]))
sum(random.sample(range(20),10))<jupyter_output><empty_output><jupyter_text>**sum of 10 random integers between 0 and 9**<jupyter_code>sum(random.choice(range(10)) for i in range(10))<jupyter_output><empty_output><jupyter_text>Examples<jupyter_code>import matplotlib.pyplot as plt
Ys = []
for rep in range(100000):
    y = 0
    for i in range(10):
        x = random.choice(range(1,7))
        y += x
    Ys.append(y)
plt.hist(Ys, bins=10, facecolor="g", alpha=0.75, density=True)
plt.grid(True)
plt.title("Y = Sum(Xi)")
x1 = []
x2 = []
x3 = []
x4 = []
for i in range(100):
    x1.append(random.choice(range(1,7)))
for i in range(1000):
    x2.append(random.choice(range(1,7)))
for i in range(10000):
    x3.append(random.choice(range(1,7)))
for i in range(1000000):
    x4.append(random.choice(range(1,7)))
max(x4)
plt.figure()
plt.subplot(221)
plt.title("N = 10")
plt.hist(x1, facecolor="g", alpha=0.75, bins=np.linspace(0.5,6.5,7), density=True)
plt.grid(True)
plt.subplot(222)
plt.title("N = 1000")
plt.hist(x2, bins=np.linspace(0.5,6.5,7), density=True)
plt.grid(True)
plt.subplot(223)
plt.hist(x3, facecolor="r", alpha=0.75, bins=np.linspace(0.5,6.5,7), density=True)
plt.grid(True)
plt.subplot(224)
plt.hist(x4, facecolor="b", alpha=0.75, bins=np.linspace(0.5,6.5,7), density=True)
plt.grid(True)
random.choice(list((1,2,3,4)))<jupyter_output><empty_output><jupyter_text>Randomness using Numpy<jupyter_code>x = np.random.random(10)
plt.plot(x)
plt.title("Uniform Std Distribution")
plt.show()
mu = 0
sd = 1
size = (5, 3)
y = np.random.normal(mu, sd, 5)
z = np.random.normal(mu, sd, size)
print(y,"\n")
print(z)
import time
low = 1
high = 7
size = (3, 2)
# generates a table with 100 rows and 10 col, equivalent to previous example
# Row = Yi
# Col = Xi
# Sum of col = Y
table = np.random.randint(low, high, size)
start_time = time.clock()
Y1 = np.sum(np.random.randint(1, 7, (100,10)), axis=1)
Y2 = np.sum(np.random.randint(1, 7, (1000, 10)), axis=1)
Y3 = np.sum(np.random.randint(1, 7, (10000, 10)), axis=1)
Y4 = np.sum(np.random.randint(1, 7, (1000000, 10)), axis=1)
print(Y4.shape)
plt.figure()
plt.subplot(221)
plt.hist(Y1, density=True)
# plt.grid(True)
plt.subplot(222)
plt.hist(Y2, density=True)
# plt.grid(True)
plt.subplot(223)
plt.hist(Y3, density=True)
# plt.grid(True)
plt.subplot(224)
plt.hist(Y4, density=True)
# plt.grid(True)
plt.show()
end_time = time.clock()
print(end_time-start_time)
low = 1
high = 7
size = (3, 2)
# generates a table with 100 rows and 10 col, equivalent to previous example
# Row = Yi
# Col = Xi
# Sum of col = Y
table = np.random.randint(low, high, size)
print(table)
np.sum(table, axis=0) # Sum of elements of a column
# axis = 1 => Sum of all elements of a row
np.random.random((5,2,3))
start_time = time.clock()
stop_time = time.clock()
stop_time - start_time<jupyter_output>/usr/lib/python3/dist-packages/ipykernel_launcher.py:1: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
  """Entry point for launching an IPython kernel.
/usr/lib/python3/dist-packages/ipykernel_launcher.py:2: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead<jupyter_text>Random Walks<jupyter_code>delta_x = np.random.normal(0,1,(2,5))
X_0 = np.zeros((2,1))
X = np.concatenate((X_0, np.cumsum(delta_x, axis=1)), axis=1)
plt.plot(X[0], X[1], "go-", alpha=0.75)
plt.savefig("rw2.pdf")
delta_x = np.random.normal(0,1, (2,5))
X = np.cumsum(delta_x, axis=1)
plt.plot(X[0], X[1], "bo-", alpha=0.75)
plt.savefig("rw1.pdf")
delta_x = np.random.normal(0,1,(2,100))
X_0 = np.zeros((2,1))
X = np.concatenate((X_0, np.cumsum(delta_x, axis=1)), axis=1)
plt.plot(X[0], X[1], "go-", alpha=0.75)
plt.savefig("rw3.pdf")
delta_x = np.random.normal(0,1,(2,1000))
X_0 = np.zeros((2,1))
X = np.concatenate((X_0, np.cumsum(delta_x, axis=1)), axis=1)
plt.plot(X[0], X[1], "go-", alpha=0.75)
plt.savefig("rw4.pdf")
delta_x = np.random.normal(0,1,(2,10000))
X_0 = np.zeros((2,1))
X = np.concatenate((X_0, np.cumsum(delta_x, axis=1)), axis=1)
plt.plot(X[0], X[1], "go-", alpha=0.75)
plt.savefig("rw5.pdf")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19715621719629736, 'fraction_numerical': 0.07720202309380667, 'mean_word_length': 4.927601809954751, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '404874', 'license': 'MIT', 'n_tokens_mistral': 5012, 'n_tokens_neox': 4556, 'n_words': 952, 'path': 'Py_Tutorials.ipynb', 'repo_name': 'ashudva/Case_Studies', 'size': '10485'}