<jupyter_start><jupyter_text>NumPy NumPy is the fundamental package for scientific computing with Python. It contains among other things:a powerful N-dimensional array objectsophisticated (broadcasting) functionstools for integrating C/C++ and Fortran codeuseful linear algebra, Fourier transform, and random number capabilitiesBesides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.NumPy is licensed under the BSD license, enabling reuse with few restrictions.![numpy.png](attachment:numpy.png) Importing numpy module<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Creating an one axes or one dimensional array<jupyter_code>a = np.array([1,4,5])
a<jupyter_output><empty_output><jupyter_text>Creating two axes or two dimensional array<jupyter_code>a = np.array([(1,3,5),(5,7,8)])
a<jupyter_output><empty_output><jupyter_text>Creating three axes or three dimensional array<jupyter_code>a = np.array([(1,5,7),(4,6,8),(9,7,8)])
a<jupyter_output><empty_output><jupyter_text>Creating an array of zeros using zeros method which takes rows and column as argument.<jupyter_code>a = np.zeros((3,4))
a<jupyter_output><empty_output><jupyter_text>Creating array with ones or creating an identity matrix using ones method which takes row and columns as argument.<jupyter_code>a = np.ones((3,4))
a<jupyter_output><empty_output><jupyter_text>Creating an array with random elements.<jupyter_code>a = np.empty((2,4))
a<jupyter_output><empty_output><jupyter_text>We can also declare the data type of the elemnts into the array by passing a parameter dtype in the methods,The possible values of dtype are complex, int16 and many more.The dafault value is int8.<jupyter_code>a = np.array(([1,5,6],[7,8,9],[4,6,2]), dtype=complex)
a<jupyter_output><empty_output><jupyter_text>We can also generate an sequence of array using arange method which is analogus to range mehtod of pyhton.arange method takes three argument which are start, stop and step.<jupyter_code>a = np.arange(10,50,5)
a<jupyter_output><empty_output><jupyter_text>arange method can also take the number of elements in the array and generate an random array.<jupyter_code>a  =np.arange(50)
a<jupyter_output><empty_output><jupyter_text>We can also generate a sequence of number using linspace method which takes start,stop and number of values as argument.<jupyter_code># It generates number 100 number from 0 to 10
a = np.linspace(0,10,100)
a<jupyter_output><empty_output><jupyter_text>Another example of linspace method is:<jupyter_code># Generates an array of 100 values in between 0 and 2Pi
a = np.linspace(0, 2*3.14159265, 100)

# Generate an array of 100 values of sin
f = np.sin(a)
f<jupyter_output><empty_output><jupyter_text>When you print an array, NumPy displays it in a similar way to nested lists, but with the following layout:* the last axis is printed from left to right,* the second-to-last is printed from top to bottom,* the rest are also printed from top to bottom, with each slice separated from the next by an empty line.Reshape method is used to change the shape of the array.Reshape method can either take no. of rows and column as an argument or it can also take no. of rows and columns as well as number of matrices to produce.***RESHAPE METHOD CREATES A NEW MODIFIED ARRAY*****Resize method can be use to modify the original array**<jupyter_code># Creates an matrix
a = np.arange(24)

# Reshapes the matrix as 3 rows and 8 columns
b = a.reshape(3,8)
b

# Reshaped the matrix b into 2 matices of 2 rows and 6 columns
c = a.reshape(2,6,2)
c<jupyter_output><empty_output><jupyter_text>BASIC OPERATIONS ON NUMPY ARRAY  Multipling an whole array with an scalar value.<jupyter_code>a = np.array([1,2,4,9,2])
a = a*2<jupyter_output><empty_output><jupyter_text>Adding an scalar value to the whole array<jupyter_code>a = np.array([1,5,6,7,4,7,9,0])
a = a+5
a<jupyter_output><empty_output><jupyter_text>Subtracting one array from another array.For this both arrays must be of same order i.e same number of rows and columns are must.The elemnts get subtracted from each other according to their position.Ex- The element at first positon in an array a get subtracte from the elemnt at first poistion in array b respectively.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

c = a-b
c<jupyter_output><empty_output><jupyter_text>Adding one array to another array.For this both arrays must be of same order i.e same number of rows and columns are must.The elemnts get added to each other according to their position.Ex- The element at first positon in an array a get added to the elemnt at first poistion in array b respectively.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

c = a+b
c<jupyter_output><empty_output><jupyter_text>Dividing an array by another array<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

c = a/b
c<jupyter_output><empty_output><jupyter_text>Multiplying one array to another array Elementwise.For this both arrays must be of same order i.e same number of rows and columns are must. The elemnts get multiplied to each other according to their position. Ex- The element at first positon in an array a get multiplied to the elemnt at first poistion in array b respectively.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

c = a*b
c<jupyter_output><empty_output><jupyter_text>Dot Product of two matrices.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

c = a.dot(b)
c<jupyter_output><empty_output><jupyter_text>Matrix product of matrices.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

c = a@b
c<jupyter_output><empty_output><jupyter_text>We can also compare a value of our choice with all the elements in the arrray.Numpy would return an array consiting of bolean values.If the condition provided by us holds correct it would return True else False.<jupyter_code>a = np.array([1,2,6,8,3,9,16,83,90,23,5,86,73])
a>30<jupyter_output><empty_output><jupyter_text>Computing sum of all the elements in the array.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
a.sum()<jupyter_output><empty_output><jupyter_text>Computing minimum of the array.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
a.min()<jupyter_output><empty_output><jupyter_text>Computing the maximum of the array.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
a.max()<jupyter_output><empty_output><jupyter_text>Computing sum along  row or column.<jupyter_code>a = np.array([(1,5,7,98,76),(4,6,34,67,8),(9,7,54,8,28)])
a

# Sum of each row
a.sum(axis=1)
# Sum of each column
a.sum(axis=0)<jupyter_output><empty_output><jupyter_text>Computing minimum along row or column.<jupyter_code>a = np.array([(1,5,7,98,76),(4,6,34,67,8),(9,7,54,8,28)])
a
# Minimum of each row
a.min(axis=1)
# Minimum of each column
a.sum(axis=0)<jupyter_output><empty_output><jupyter_text>Computing maximum along row or cloumn.<jupyter_code>a = np.array([(1,5,7,98,76),(4,6,34,67,8),(9,7,54,8,28)])
a
# Maximum of each row
a.max(axis=1)
# Maximum of each column
a.max(axis=0)<jupyter_output><empty_output><jupyter_text>Transpose of an array<jupyter_code>a = np.arange(25).reshape(5,5)
a
a.T<jupyter_output><empty_output><jupyter_text>Universal functions in Numpy.Numpy provides some basic math functions and these are called universal functions(ufunc).<jupyter_code>a = np.array([(1,5,7,98,76),(4,6,34,67,8),(9,7,54,8,28)])
b = np.array([(1,6,87,938,46),(64,86,334,7,8),(19,37,74,58,298)])
# Exponent function
np.exp(a)
# Square root function
np.sqrt(a)
# Sine function
np.sin(a)
# Cosine function
np.cos(a)
# Tangent function
np.tan(a)
# Inverse sine function
np.arcsin(a)
# Inverse cos function
np.arccos(a)
# Inverse tan function
np.arctan(a)
# Hyperbolic sine function
np.sinh(a)
# Hyperbolic Cosine function
np.cosh(a)
#Hyperbolic Tangent function
np.tanh(a)
# Inverse hyperbolic sine function
np.arcsinh(a)
# Inverse hyperbolic Cosine function
np.arccosh(a)
# Inverse hyperbolic Tangent funciton
np.arctanh(a)
# Log base 2 of array
np.log2(a)
# LCM of arrays
np.lcm(a,b)
# GCD of arrays
np.gcd(a,b)
# Resiprocal of arrays
np.reciprocal(a)
# Square of array elementwise
np.square(a)<jupyter_output><empty_output><jupyter_text>Heaviside function can also be calculated.The Heaviside step function, or the unit step function, usually denoted by H or θ, is a discontinuous function, named after Oliver Heaviside, whose value is zero for negative arguments and one for positive arguments.https://en.wikipedia.org/wiki/Heaviside_step_function<jupyter_code># Conjugate of a complex
a = np.arange(20, dtype=complex)
print(a)

a = np.conjugate(a)
print(a)
# Heaviside step function

np.heaviside(a,b)<jupyter_output><empty_output><jupyter_text>Logical Operations  Logical AND<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])
np.logical_and(a,b)<jupyter_output><empty_output><jupyter_text>Logical OR<jupyter_code>np.logical_or(a,b)<jupyter_output><empty_output><jupyter_text>Logical XOR<jupyter_code>np.logical_xor(a,b)<jupyter_output><empty_output><jupyter_text>Logical NOT<jupyter_code>np.logical_not(a,b)<jupyter_output><empty_output><jupyter_text>Floating functionsChecks the array and return boolean value accordingly.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])<jupyter_output><empty_output><jupyter_text>Checking finitness of the array<jupyter_code># Checking finitness
np.isfinite(a)
# Checking infinitness
np.isinf(a)<jupyter_output><empty_output><jupyter_text>Checking Null value(NaN)<jupyter_code>np.isnan(a)<jupyter_output><empty_output><jupyter_text>Checking Time value(NaT)<jupyter_code>np.isnat(a)<jupyter_output><empty_output><jupyter_text>Getting the absolute value<jupyter_code>np.fabs(a)<jupyter_output><empty_output><jupyter_text>Distance between nearest pointsReturn the distance between x and the nearest adjacent number.<jupyter_code>np.spacing(a)<jupyter_output><empty_output><jupyter_text>Decomposing into mantissaDecompose the elements of x into mantissa and twos exponent<jupyter_code>np.frexp(a)<jupyter_output><empty_output><jupyter_text>Ceiling and florring function<jupyter_code>np.floor(a)
np.ceil(a)<jupyter_output><empty_output><jupyter_text>Truncated value of the elementsReturn the truncated value of the input, element-wise.Truncating a Number. A method of approximating a decimal number by dropping all decimal places past a certain point without rounding. For example, 3.14159265... can be truncated to 3.1415. Note: If 3.14159265...were rounded to the same decimal place, the approximation would be 3.1416https://en.wikipedia.org/wiki/Truncation<jupyter_code>np.trunc(a)<jupyter_output><empty_output><jupyter_text>Changing the sign<jupyter_code>a = np.array([3,5,7,3,9,12,+16])
b = np.array([-8,-9,2,31,6,-9,32])
# Changes the sign of array a with signs of the array b
np.copysign(a,b)<jupyter_output><empty_output><jupyter_text>Bitwise Funcitons These function all require integer arguments and they manipulate the bit-pattern of those arguments.  Bitwiswe AND<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

np.bitwise_and(a,b)<jupyter_output><empty_output><jupyter_text>Bitwise OR<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

np.bitwise_or(a,b)<jupyter_output><empty_output><jupyter_text>Bitwise XOR<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

np.bitwise_xor(a,b)<jupyter_output><empty_output><jupyter_text>Bitwise NOT<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

np.bitwise_not(a,b)<jupyter_output><empty_output><jupyter_text>Invert<jupyter_code>a = np.array([3,5,7,3,9,12,16])
np.invert(a)<jupyter_output><empty_output><jupyter_text>Left Shift<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

np.left_shift(a,b)<jupyter_output><empty_output><jupyter_text>Right Shift<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

np.right_shift(a,b)<jupyter_output><empty_output><jupyter_text>Indexing And Slicing Getting element by its index in array.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
# Getting element from the one axes array
a[3]
b = np.arange(24)
b.reshape(4,6)
print(b)
# Getting the elements from the multi axes array
b[3:4]<jupyter_output><empty_output><jupyter_text>Slicing the elements.It takes argument start, stop and step and returns output from start to stop-1 th index.<jupyter_code>a[0:4]<jupyter_output><empty_output><jupyter_text>Slicing the elements by uttering some values.<jupyter_code>a[0:1:4]<jupyter_output><empty_output><jupyter_text>Reversing the array<jupyter_code>a[::-1]<jupyter_output><empty_output><jupyter_text>Printing the values line by line of an array.<jupyter_code>a = np.array([3,5,7,3,9,12,16])
for element in range(len(a)):
    print(a[element])<jupyter_output><empty_output><jupyter_text>Iterating over rows in multiaxes array<jupyter_code>a = np.arange(10000)
a = a.reshape(100,100)
a
for row in a:
    print(row)<jupyter_output><empty_output><jupyter_text>To get the individual elements printed we use **flat** method.<jupyter_code>for element in a.flat:
    print(element)<jupyter_output><empty_output><jupyter_text>Returning pair of array index and values using **ndenumerator method**.<jupyter_code>for index, value in np.ndenumerate(a):
    print(index, value)<jupyter_output><empty_output><jupyter_text>Stacking multiple arrays  Vertically over each other<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

# Stacks a over the b vertically
np.vstack((a,b))<jupyter_output><empty_output><jupyter_text>Horizontaly side by side<jupyter_code>a = np.array([3,5,7,3,9,12,16])
b = np.array([8,9,2,31,6,9,32])

# Stackss a on the right of b
np.hstack((a,b))<jupyter_output><empty_output><jupyter_text>Spliting arrays in multiple stacks  Spliting Horizontaly<jupyter_code>a = np.array([3,5,7,3,9,12,16,18])

# Splits he array into the no. of equal parts provided as argument horizontally.
np.hsplit(a,2)<jupyter_output><empty_output><jupyter_text>Spliting Vertically<jupyter_code>a = np.arange(36).reshape(6,6)
# Splits the array into the no. of equal parts provided as argument vertically.
np.vsplit(a,2)<jupyter_output><empty_output><jupyter_text>Checking whether two arrays are sameIt checks whether arrays are same or not on the basis of their id's not on basis of the values<jupyter_code>a = np.arange(36).reshape(6,6)
b = np.arange(36).reshape(6,6)

b is a<jupyter_output><empty_output><jupyter_text>Copy method to copy the array and all data associated with it<jupyter_code>a = np.arange(36).reshape(6,6)

np.copy(a)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12538575070441432, 'fraction_numerical': 0.04655843284583389, 'mean_word_length': 7.417278373800113, 'pattern_counts': {'":': 0, '<': 265, '<?xml version=': 0, '>': 266, 'https://': 2, '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': '66', 'dir': 'jupyter-structured-clean-dedup', 'id': '384142', 'license': 'MIT', 'n_tokens_mistral': 5988, 'n_tokens_neox': 5655, 'n_words': 1357, 'path': 'python/Numpy Methods.ipynb', 'repo_name': 'SafvanMelethil/HacktoberFest_2021', 'size': '14906'}