<jupyter_start><jupyter_text>Elementwise Operations  1. Basic Operations  with scalars<jupyter_code>a = np.array([1,2,3,4])

a + 1
a ** 2<jupyter_output><empty_output><jupyter_text>All arithmetic operates elementwise<jupyter_code>b = np.ones(4) + 1

a - b
a * b
# matrix multiplication
c = np.diag([1,2,3,4])

print(c*c)
print('*******************')
print(c.dot(c))<jupyter_output>[[ 1  0  0  0]
 [ 0  4  0  0]
 [ 0  0  9  0]
 [ 0  0  0 16]]
*******************
[[ 1  0  0  0]
 [ 0  4  0  0]
 [ 0  0  9  0]
 [ 0  0  0 16]]<jupyter_text>comparisons<jupyter_code>a = np.array([1,2,3,4])
b = np.array([5,2,2,4])
a == b
a > b
# arr wise comparisons
a = np.array([1,2,3,4])
b = np.array([5,2,2,4])
c = np.array([1,2,3,4])

np.array_equal(a,b)
np.array_equal(a,c)<jupyter_output><empty_output><jupyter_text>Logical Operations<jupyter_code>a = np.array([1,1,0,0], dtype=bool)
b = np.array([1,0,1,0], dtype=bool)

np.logical_or(a,b)
np.logical_and(a,b)<jupyter_output><empty_output><jupyter_text>Transcendental functions<jupyter_code>a = np.arange(5)

np.sin(a)
np.log(a)
np.exp(a)<jupyter_output><empty_output><jupyter_text>Shape Mismatch<jupyter_code>a = np.arange(4)

a + np.array([1,2])<jupyter_output><empty_output><jupyter_text>Basic Reductions  computing sums<jupyter_code>x = np.array([1,2,3,4])
np.sum(x)
# sum by rows and by columns
x = np.array([[1,1],[2,2]])
x
x.sum(axis = 0) # columns first dimension
x.sum(axis = 1)  # rows (second dimension)<jupyter_output><empty_output><jupyter_text>other reductions<jupyter_code>x = np.array([1,3,2])
x.min()
x.max()
x.argmin()  # index of minimum element
x.argmax() # index of max element<jupyter_output><empty_output><jupyter_text>Logical Operations<jupyter_code>np.all([True,True,False])  # est whether all array elements along a given axis evaluate to True.
np.any([True,True,False])
# can be used for arr comparisons
a = np.zeros((50,50))
np.any(a!= 0)
np.all(a == a)
a = np.array([1,2,3,2])
b = np.array([2,2,3,2])
c = np.array([6,4,4,5])
((a <= b) & (b <= c)).all()<jupyter_output><empty_output><jupyter_text>Statistics<jupyter_code>x = np.array([1,2,3,1])
y = np.array([[1,2,3],[5,6,1]])
x.mean()
np.median(x)
np.median(y , axis = 1) # last axis
x.std()   # full population standard dev<jupyter_output><empty_output><jupyter_text>Example: Data in populations.txt describes the populations of hares and lynxes (and carrots) in northern Canada during 20 years.<jupyter_code># load data into numpy arr obj
data = np.loadtxt('http://scipy-lectures.org/_downloads/populations.txt')
data
year , hares, lynxes , carrots = data.T  # column to variables
print(year)
# the mean population over time
populations = data [:, 1:]
populations
 # sample standard deviation
populations.std(axis = 0)
#which species has the highest population each year?
np.argmax(populations,axis=1)<jupyter_output><empty_output><jupyter_text>Broadcasting Basic operations on numpy arrays (addition, etc.) are elementwiseThis works on arrays of the same size. Nevertheless, It’s also possible to do operations on arrays of different sizes if NumPy can transform these arrays so that they all have the same size: this conversion is called broadcasting.The image below gives an example of broadcasting:<jupyter_code>a = np.tile(np.arange(0,40,10),(3,1))
print(a)

print('***************')
a = a.T
print(a)
b = np.array([0,1,2])
b
a + b
a = np.arange(0,40,10)
a.shape
a = a[: , np.newaxis]  # adds a new axis -> 2D array
a.shape
a 
a + b<jupyter_output><empty_output><jupyter_text>Array Shape Manipulation  Flattening<jupyter_code>a = np.array([[1,2,3],[4,5,6]])
a.ravel() #Return a contiguous flattened array. A 1-D array, containing the elements of the input, is returned. A copy is made only if needed.
a.T # transpose
a.T.ravel()<jupyter_output><empty_output><jupyter_text>Reshaping<jupyter_code>print(a.shape)
print(a)
b = a.ravel()
print(b)
b = b.reshape((2,3))
b
b[0,0] = 100
a<jupyter_output><empty_output><jupyter_text>Note and Beware: reshape may also return a copy!:<jupyter_code>a = np.zeros((3,2))
b = a.T.reshape(3*2)
b[0] = 50
a<jupyter_output><empty_output><jupyter_text>Adding a Dimension Indexing with the np.newaxis object allows us to add an axis to an arraynewaxis is used to increase the dimension of the existing array by one more dimension, when used once. Thus,1D array will become 2D array2D array will become 3D array3D array will become 4D array and so on<jupyter_code>z = np.array([1,2,3])
z
z[:,np.newaxis]<jupyter_output><empty_output><jupyter_text>Dimension Shuffling<jupyter_code>a = np.arange(4*3*2).reshape(4,3,2)
a.shape
a
a[0,2,1]<jupyter_output><empty_output><jupyter_text>Resizing<jupyter_code>a = np.arange(4)
a.resize((8,))
a<jupyter_output><empty_output><jupyter_text>However, it must not be referred to somewhere else:<jupyter_code>b = a
a.resize((4,))<jupyter_output><empty_output><jupyter_text>Sorting Data<jupyter_code># sorting along an axis
a = np.array([[5,4,6],[2,3,2]])
b = np.sort(a,axis=1)
b
# in place sort
a.sort(axis=1)
a
# sorting with fancy indexing
a = np.array([4,3,1,2])
j = np.argsort(a)
j
a[j]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16369339345226427, 'fraction_numerical': 0.03822779847088806, 'mean_word_length': 6.04696132596685, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '637723', 'license': 'MIT', 'n_tokens_mistral': 2160, 'n_tokens_neox': 2080, 'n_words': 477, 'path': 'Machine Learning and Artificial Intelligence/01. Fundamentals-of-Programming/04.Python-for-Data-Science_ Numpy/02.Numerical-operations-on-numpy.ipynb', 'repo_name': 'pramit026/Machine-Learning-and-Artificial-Intelligence', 'size': '5101'}