Num Py
Num Py
What is NumPy?
NumPy is a general-purpose array-processing package.
It provides a high-performance multidimensional array object and
tools for working with these arrays.
It is the fundamental package for scientific computing
with Python. It is open-source software.
Install Python NumPy
Numpy can be installed for Mac and Linux users via the
following pip command:
pip install numpy
Arrays in NumPy
NumPy’s main object is the homogeneous multidimensional
array.
It is a table of elements (usually numbers), all of the same type,
is rank.
NumPy’s array class is called ndarray. It is also known by the
alias array.
Example:
In this example, we are creating a two-dimensional array that has
the rank of 2 as it has 2 axes.
The first axis(dimension) is of length 2, i.e., the number of rows,
and the second axis(dimension) is of length 3, i.e., the number of
columns. The overall shape of the array can be represented as (2,
3)
import numpy as np
# Creating array object
arr = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
Output:
Array is of type: <class 'numpy.ndarray'>
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64
NumPy Array Creation
There are various ways of Numpy array creation in Python. They
are as follows:
1. Create NumPy Array with List and Tuple
You can create an array from a regular Python list or tuple using
the array() function. The type of the resulting array is deduced
from the type of the elements in the sequences. Let’s see this
implementation:
Python
import numpy as np
newarr = arr.reshape(2, 2, 3)
# An exemplar array
arr = np.array([[-1, 2, 0, 4],
[4, -0.5, 6, 0],
[2.6, 0, 7, 8],
[3, -7, 4, 2.0]])
# Slicing array
temp = arr[:2, ::2]
print ("Array with first 2 rows and alternate"
"columns(0 and 2):\n", temp)
a = np.array([1, 2, 5, 3])
# transpose of array
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]])
a = np.array([[1, 2],
[3, 4]])
b = np.array([[4, 3],
[2, 1]])
# add arrays
print ("Array sum:\n", a + b)
NymPy’s ufuncs
NumPy provides familiar mathematical functions such as sin, cos,
exp, etc. These functions also operate elementwise on an array,
producing an array as output.
Note: All the operations we did above using overloaded
operators can be done using ufuncs like np.add, np.subtract,
np.multiply, np.divide, np.sum, etc.
Python
# Python program to demonstrate
# universal functions in numpy
import numpy as np
# exponential values
a = np.array([0, 1, 2, 3])
print ("Exponent of array elements:", np.exp(a))
a = np.array([[1, 4, 2],
[3, 4, 6],
[0, -1, 5]])
# sorted array
print ("Array elements in sorted order:\n",
np.sort(a, axis = None))
# Creating array
arr = np.array(values, dtype = dtypes)
print ("\nArray sorted by names:\n",
np.sort(arr, order = 'name'))
NumPy Commands
NumPy commands refer to the functions and methods available
in the NumPy library that operate on arrays. Here are a few
commonly used NumPy commands:
np.array(): Create an array.
interval.
np.zeros(), np.ones(), np.full(): Create new arrays filled with
its data.
np.mean(), np.median(), np.std(): Compute the mean,
matrix.
These commands and many others make NumPy a versatile tool
for numerical computing in Python