<jupyter_start><jupyter_text>Operations on NumPy ArraysThe learning objectives of this section are:* Manipulate arrays    * Reshape arrays    * Stack arrays* Perform operations on arrays    * Perform basic mathematical operations    * Apply built-in functions     * Apply your own functions     * Apply basic linear algebra operations<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Example - 1 (Arithmatric Operations)<jupyter_code>array1 = np.array([10,20,30,40,50])
array2 = np.arange(5)
array1
array2
# Add array1 and array2.
array3 = array1 + array2
array3<jupyter_output><empty_output><jupyter_text>Example - 2<jupyter_code>array4 = np.array([1,2,3,4])
array4 + array1
print (array1.shape)
print (array4.shape)<jupyter_output>(4,)<jupyter_text>Example - 3<jupyter_code>array = np.linspace(1, 10, 5)
array
array*2
array**2<jupyter_output><empty_output><jupyter_text>Stacking Arrays    ```np.hstack()``` and ```n.vstack()```Stacking is done using the ```np.hstack()``` and ```np.vstack()``` methods. For horizontal stacking, the number of rows should be the same, while for vertical stacking, the number of columns should be the same.<jupyter_code># Note that np.hstack(a, b) throws an error - you need to pass the arrays as a list
a = np.array([1, 2, 3])
b = np.array([2, 3, 4])

np.hstack((a,b))
np.vstack((a,b))
np.arange(12)
np.arange(12).reshape(3,4)
array1 = np.arange(12).reshape(3,4) #3x4
array2 = np.arange(20).reshape(5,4) #5x4
print (array1, '\n', array2)
np.vstack((array1,array2))<jupyter_output><empty_output><jupyter_text>Example - 4 (Numpy Built-in functions)<jupyter_code>array1
np.power(array1, 3)
np.arange(9).reshape(3,3)
x = np.array([-2,-1, 0, 1,2])
x
abs(x)
np.absolute(x)<jupyter_output><empty_output><jupyter_text>Example - 5 (Trignometric functions)<jupyter_code>np.pi
theta = np.linspace(0, np.pi, 5)
theta
np.sin(theta)
np.cos(theta)
np.tan(theta)<jupyter_output><empty_output><jupyter_text>Example - 6 (Exponential and logarithmic functions)<jupyter_code>x = [1, 2, 3, 10]
x = np.array(x)
np.exp(x) # e=2.718...
# 2^1, 2^2, 2^3, 2^10
np.exp2(x)
np.power(x,3)
np.log(x)
np.log2(x)
np.log10(x)
np.log<jupyter_output><empty_output><jupyter_text>Example - 7<jupyter_code>x = np.arange(5)
x
y = x * 10
y
y = np.empty(5)
y
np.multiply(x, 12, out=y)
y
y = np.zeros(10)
y
np.power(2, x, out=y[::2])
y<jupyter_output><empty_output><jupyter_text>Example - 8 (Aggregates)<jupyter_code>x = np.arange(1,6)
x
sum(x)
np.add.reduce(x)
np.add.accumulate(x)
np.multiply.accumulate(x)<jupyter_output><empty_output><jupyter_text>Apply Basic Linear Algebra OperationsNumPy provides the ```np.linalg``` package to apply common linear algebra operations, such as:* ```np.linalg.inv```: Inverse of a matrix* ```np.linalg.det```: Determinant of a matrix* ```np.linalg.eig```: Eigenvalues and eigenvectors of a matrix    Also, you can multiple matrices using ```np.dot(a, b)```.<jupyter_code># np.linalg documentation
help(np.linalg)
A = np.array([[6, 1, 1],
              [4, -2, 5],
              [2, 8, 7]])
A<jupyter_output><empty_output><jupyter_text>Rank of a matrix<jupyter_code>np.linalg.matrix_rank(A)<jupyter_output><empty_output><jupyter_text>Trace of matrix A<jupyter_code>np.trace(A)<jupyter_output><empty_output><jupyter_text>Determinant of a matrix<jupyter_code>np.linalg.det(A)<jupyter_output><empty_output><jupyter_text>Inverse of matrix A<jupyter_code>A
np.linalg.inv(A)
B = np.linalg.inv(A)
np.matmul(A,B) #actual matrix multiplication
A * B<jupyter_output><empty_output><jupyter_text>Matrix A raised to power 3<jupyter_code>np.linalg.matrix_power(A,3) # matrix multiplication A A A<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15443383356070942, 'fraction_numerical': 0.03656207366984993, 'mean_word_length': 6.25940594059406, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '54965', 'license': 'MIT', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1491, 'n_words': 307, 'path': 'Teaching Session1_part5.ipynb', 'repo_name': 'anushka-DS/NUMPY', 'size': '3665'}