<jupyter_start><jupyter_text>05 Arrays - Creating and Manipulating 1D & 2D Arrays- Array Operations  1D Numpy Array  |  |  |  Create array<jupyter_code>import numpy as np  
a = np.array([0,1,2,3,4])
print(f'Numpy Array a \n{a}')
print(f'Type of Numpy Array a {type(a)}')
print(f'Elements Type of Numpy Array a {a.dtype}')
print(f'Size of Numpy Array a {a.size}')
print(f'Dimensions of Numpy Array a {a.ndim}')
print(f'Shape of Numpy Array a {a.shape}')<jupyter_output>Numpy Array a 
[0 1 2 3 4]
Type of Numpy Array a <class 'numpy.ndarray'>
Elements Type of Numpy Array a int64
Size of Numpy Array a 5
Dimensions of Numpy Array a 1
Shape of Numpy Array a (5,)<jupyter_text>Manipulate Array  |  |  Indexing<jupyter_code>a[0]
for index,element in enumerate(a):
    print(f'index {index} element {element}')
b=np.array([3.1,11.02,6.2,231.2,5.2])
print(f'Numpy Array b {b}')
print(f'Type of Numpy Array b {type(b)}')
print(f'Elements Type of Numpy Array b {b.dtype}')
print(f'Size of Numpy Array b {b.size}')
print(f'Dimensions of Numpy Array b {b.ndim}')
print(f'Shape of Numpy Array b {b.shape}')
for index,element in enumerate(b):
    print(f'index {index} element {element}')
c=np.array([20,1,2,3,4])
c
c[0]=100
c
c[4]=0
c<jupyter_output><empty_output><jupyter_text>Slice<jupyter_code>d = c[1:4]
d
d.size
c[3:5]=300,400
c<jupyter_output><empty_output><jupyter_text>Array Operations  |  |  |  |  Vector Addition & Subtraction<jupyter_code>u = np.array([1,0])
u
v = np.array([0,1])
v
z = u + v
z
type(z)
z = u-v
z<jupyter_output><empty_output><jupyter_text>Array multiplication with a Scalar<jupyter_code>y = np.array([1,2])
y
z = 2*y
z<jupyter_output><empty_output><jupyter_text>Product of two Numpy arrays  |  |  |<jupyter_code>u = np.array([1,2])
u
v = np.array([3,1])
v<jupyter_output><empty_output><jupyter_text>Hadamard Product<jupyter_code>z = u*v
z<jupyter_output><empty_output><jupyter_text>Dot Product<jupyter_code>u.T
z= np.dot(u,v)
z
z = u@v
z<jupyter_output><empty_output><jupyter_text>Broadcasting  |  Adding Constant to an Numpy Array<jupyter_code>u = np.array([1,2,3,-1])
u
z = u + 1
z<jupyter_output><empty_output><jupyter_text>Universal Functions  |  |  |  |  |<jupyter_code>a = np.array([1,-1,1,-1])
a
mean_a = a.mean()
mean_a
b = np.array([1,-2,3,4,5])
b
max_b = b.max()
max_b
np.pi
x = np.array([0,np.pi/2,np.pi])
x
y = np.sin(x)
y
np.linspace(-2,2,num=5)
np.linspace(-2,2,num=9)<jupyter_output><empty_output><jupyter_text>Plotting   |<jupyter_code>x = np.linspace(0,2*np.pi,100)
x
y = np.sin(x)
y
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(x,y);<jupyter_output><empty_output><jupyter_text>2D Numpy Array  |  |  |  |  Create 2D Numpy Array<jupyter_code>A = np.array([[11,12,13],
             [21,22,23],
             [31,32,33]])
print(f'Numpy Array A \n{A}')
print(f'Type of Numpy Array A {type(A)}')
print(f'Elements Type of Numpy Array A {A.dtype}')
print(f'Size of Numpy Array A {A.size}')
print(f'Dimensions of Numpy Array A {A.ndim}')
print(f'Shape of Numpy Array A {A.shape}')<jupyter_output>Numpy Array A 
[[11 12 13]
 [21 22 23]
 [31 32 33]]
Type of Numpy Array A <class 'numpy.ndarray'>
Elements Type of Numpy Array A int64
Size of Numpy Array A 9
Dimensions of Numpy Array A 2
Shape of Numpy Array A (3, 3)<jupyter_text>Indexing  |  |<jupyter_code>A[0]
A[0][0]
A[1][2]<jupyter_output><empty_output><jupyter_text>Slicing  |<jupyter_code>A[0,0:2]
A[1,:]
A[:,2]
A[0:2,2]<jupyter_output><empty_output><jupyter_text>Addition  |<jupyter_code>X = np.array([[1,0],
              [0,1]])
X
Y = np.array([[2,1],
              [1,2]])
Y
Z = X + Y
Z<jupyter_output><empty_output><jupyter_text>|<jupyter_code>Z = 2*Y
Z<jupyter_output><empty_output><jupyter_text>Matrix Multiplication  |  |  |  Hadamard Matrix Multiplication<jupyter_code>Z = X * Y
Z<jupyter_output><empty_output><jupyter_text>Dot Matrix Multiplication<jupyter_code>Z = X@Y
Z
A = np.array([[0,1,1],
              [1,0,1]])
A
B = np.array([[1,1],
              [1,1],
              [-1,1]])
B
C = A@B
C<jupyter_output><empty_output><jupyter_text>Plotting functions<jupyter_code># Plotting functions Plotvec1,Plotvec2

def Plotvec1(u, z, v):
    
    ax = plt.axes()
    ax.arrow(0, 0, *u, head_width=0.05, color='r', head_length=0.1)
    plt.text(*(u + 0.1), 'u')
    
    ax.arrow(0, 0, *v, head_width=0.05, color='b', head_length=0.1)
    plt.text(*(v + 0.1), 'v')
    ax.arrow(0, 0, *z, head_width=0.05, head_length=0.1)
    plt.text(*(z + 0.1), 'z')
    plt.ylim(-2, 2)
    plt.xlim(-2, 2)
def Plotvec2(a,b):
    ax = plt.axes()
    ax.arrow(0, 0, *a, head_width=0.05, color ='r', head_length=0.1)
    plt.text(*(a + 0.1), 'a')
    ax.arrow(0, 0, *b, head_width=0.05, color ='b', head_length=0.1)
    plt.text(*(b + 0.1), 'b')
    plt.ylim(-2, 2)
    plt.xlim(-2, 2)
u = np.array([1, 0])
v = np.array([0, 1])
z = u + v
Plotvec1(u, z, v)
print(f"The dot product u@z is {u@z}")
a,b = np.array([-1,1]),np.array([1,1])
Plotvec2(a,b)
print(f"The dot product a@b is {a@b}")
a,b = np.array([1,0]),np.array([0,1])
Plotvec2(a,b)
print(f"The dot product a@b is {a@b}")
# The vectors are perpendicular. 
# As a result, the dot product is zero.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18025169409486932, 'fraction_numerical': 0.04743465634075508, 'mean_word_length': 4.670691547749725, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '411922', 'license': 'MIT', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2355, 'n_words': 500, 'path': 'notebooks/05_Arrays.ipynb', 'repo_name': 'aymanibrahim/pyds', 'size': '5165'}