<jupyter_start><jupyter_text>NumPy Basic Operations<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse

# https://www.oreilly.com/library/view/machine-learning-with/9781491989371/ch01.html<jupyter_output><empty_output><jupyter_text>Applying Operations to Elements NumPy’s vectorizeNumPy’s vectorize class converts a function into a function that can apply to all elements in an array or slice of an array. It’s worth noting that vectorize is essentially a for loop over the elements and does not increase performance<jupyter_code># Create matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# Create function that adds 100 to something
add_100 = lambda i: i + 100

# Create vectorized function
vectorized_add_100 = np.vectorize(add_100)

print(vectorized_add_100(matrix))<jupyter_output>[[101 102 103]
 [104 105 106]
 [107 108 109]]<jupyter_text>NumPy arrays allow us to perform operations between arrays even if their dimensions are not the same (a process called broadcasting). For example, we can create a much simpler version of our solution using broadcasting:<jupyter_code># Add 100 to all elements as before
matrix = matrix + 100
print(matrix)<jupyter_output>[[101 102 103]
 [104 105 106]
 [107 108 109]]<jupyter_text>Maximum and Minimum ValueYou need to find the maximum or minimum value in an array.<jupyter_code># Return maximum element
print(np.max(matrix))

# Return minimum element
print(np.min(matrix))<jupyter_output>109
101<jupyter_text>Often we want to know the maximum and minimum value in an array or subset of an array. This can be accomplished with the max and min methods. Using the axis parameter we can also apply the operation along a certain axis:**axis=0 means column and axis=1 means row**<jupyter_code># Find maximum element in each column
print(np.max(matrix, axis=0))

# Find maximum element in each row
print(np.max(matrix, axis=1))<jupyter_output>[107 108 109]
[103 106 109]<jupyter_text>Average, Variance, and Standard DeviationUse NumPy’s **mean, var, and std**. Just like with max and min, we can easily get descriptive statistics about the whole matrix or do calculations along a single axis.<jupyter_code>print(matrix)
# Return mean
print(np.mean(matrix))

# return variance
print(np.var(matrix))

# return standard deviation
print(np.std(matrix))

# Find the mean value in each column
print(np.mean(matrix, axis=0))<jupyter_output>[[101 102 103]
 [104 105 106]
 [107 108 109]]
105.0
6.666666666666667
2.581988897471611
[104. 105. 106.]<jupyter_text>Reshaping ArraysReshape allows us to restructure an array so that we maintain the same data but it is organized as a different number of rows and columns. The only requirement is that **the shape of the original and new matrix contain the same number of elements (i.e., the same size).**<jupyter_code># Create 4x3 matrix
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9],
                   [10, 11, 12]])

# Reshape matrix into 2x6 matrix
matrix=matrix.reshape(2, 6)
print(matrix)

# Agein Reshape
matrix=matrix.reshape(4, 3)
print(matrix)<jupyter_output>[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]<jupyter_text>One useful argument in reshape is -1, which effectively means “as many as needed,” so reshape(-1, 1) means one row and as many columns as needed.Similarly, if we provide one integer, reshape will return a 1D array of that length:<jupyter_code>print(matrix.reshape(1, -1))

print(matrix.reshape(12))<jupyter_output>[[ 1  2  3  4  5  6  7  8  9 10 11 12]]
[ 1  2  3  4  5  6  7  8  9 10 11 12]<jupyter_text>Flattening a Matrixflatten is a simple method to transform a matrix into a one-dimensional array. Alternatively, we can use reshape to create a row vector:<jupyter_code>print(matrix.flatten()) # flatten
print(matrix.reshape(1, -1)) # row vector<jupyter_output>[ 1  2  3  4  5  6  7  8  9 10 11 12]
[[ 1  2  3  4  5  6  7  8  9 10 11 12]]
{'fraction_non_alphanumeric': 0.08897149938042132, 'fraction_numerical': 0.08079306071871127, 'mean_word_length': 4.221216041397154, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '481007', 'license': 'MIT', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1300, 'n_words': 531, 'path': 'Libraries/Numpy/numpy_operations.ipynb', 'repo_name': 'sharif-42/All_About_DS', 'size': '4035'}