Numpy
Numpy
There are the following advantages of using NumPy for data analysis.
----------------------------------
NumPy - Ndarray Object
The most important object defined in NumPy is an N-dimensional
array type called ndarray. It describes the collection of items
of the same type. Items in the collection can be accessed using
a zero-based index.
numpy.array
It creates an ndarray from any object exposing array interface,
or from any method that returns an array.
Any object exposing the array interface method returns an array, or any (nested)
sequence.
2
dtype
6
ndmin
output:
[1, 2, 3]
----------------------
# more than one dimensions
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a)
output:
[[1 2]
[3 4]]
----------------------------
# minimum dimensions
import numpy as np
a = np.array([1, 2, 3,4,5], ndmin = 2)
print(a)
output:
[[1 2 3 4 5]]
----------------------
# dtype parameter
import numpy as np
a = np.array([1, 2, 3], dtype = complex)
print(a)
output:
[1.+0.j 2.+0.j 3.+0.j]
--------------------------------
NumPy - Data Types
NumPy supports a much greater variety of numerical types than Python
does. The following table shows different scalar data types defined
in NumPy.
1
bool_
2
int_
3
intc
4
intp
Integer used for indexing (same as C ssize_t; normally either int32 or int64)
5
int8
6
int16
7
int32
9
uint8
10
uint16
11
uint32
14
float16
16
float64
17
complex_
Complex number, represented by two 32-bit floats (real and imaginary components)
19
complex128
Complex number, represented by two 64-bit floats (real and imaginary components)
----------------------------
# using array-scalar type
import numpy as np
dt = np.dtype(np.int32)
print(dt)
output:
int32
-----------------
#int8, int16, int32, int64 can be replaced by equivalent string
'i1', 'i2','i4', etc.
import numpy as np
dt = np.dtype('i4')
print(dt)
output:
int32
-------------------
Use of structured data type. Here, the field name and the corresponding
scalar data type is to be declared.
output:
[('age', 'i1')]
-------------------------------
# now apply it to ndarray object
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print(a)
output:
[(10,) (20,) (30,)]
--------------------------------
# field name can be used to access content of age column
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print(a['age'])
output:
[10 20 30]
---------------------
Define a structured data type called student with a string field 'name',
an integer field 'age' and a float field 'marks'. This dtype is applied
to ndarray object.
--------------------------------
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
print(student)
output:
[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]
----------------------------------
import numpy as np
output:
[(b'abc', 21, 50.) (b'xyz', 18, 75.)]
-----------------------------
Each built-in data type has a character code that uniquely identifies it.
'b' − boolean
'f' − floating-point
'm' − timedelta
'M' − datetime
'U' − Unicode
output:
[[1 2 3]
[4 5 6]]
(2, 3)
--------------------
# this resizes the ndarray
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a)
a.shape = (3,2)
print(a)
output:
[[1 2 3]
[4 5 6]]
[[1 2]
[3 4]
[5 6]]
---------------------
output:
[[1 2 3]
[4 5 6]]
[[1 2]
[3 4]
[5 6]]
--------------------------------
ndarray.ndim
This array attribute returns the number of array dimensions.
# an array of evenly spaced numbers
import numpy as np
a = np.arange(24)
print(a)
output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
--------------------------------
# this is one dimensional array
import numpy as np
a = np.arange(24)
print(a)
print(a.ndim)
output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
1
------------------------------
numpy.itemsize
This array attribute returns the length of each element of array in bytes.
-------------------
# dtype of array is int8
import numpy as np
x = np.array([1,2,3,4,5], dtype = np.int8)
print(x)
print(x.itemsize)
output:
[1 2 3 4 5]
1
---------------------
# dtype of array is now float32 (4 bytes)
import numpy as np
x = np.array([1,2,3,4,5], dtype = np.float32)
print(x)
print(x.itemsize)
output:
[1. 2. 3. 4. 5.]
4
----------------------------
NumPy - Array Creation Routines
numpy.empty
It creates an uninitialized array of specified shape and dtype. It uses the
following constructor −
1
Shape
3
Order
'C' for C-style row-major array F' for FORTRAN style column-major array
--------------------------
import numpy as np
x = np.empty([3,2], dtype = int)
print(x)
output:
[[6357058 6946921]
[6029429 7340097]
[4456560 7602273]]
-----------------------------------
numpy.zeros
Returns a new array of specified size, filled with zeros.
1
Shape
2
Dtype
3
Order
'C' for C-style row-major array, 'F' for FORTRAN style column-major array
------------------------------------------
# array of five zeros. Default dtype is float
import numpy as np
x = np.zeros(5)
print(x)
output:
[0. 0. 0. 0. 0.]
----------------------------
import numpy as np
x = np.zeros((5,), dtype = np.int8)
print(x)
output:
[0 0 0 0 0]
--------------------------------
# custom type
import numpy as np
x = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])
print(x)
output:
[[(0, 0) (0, 0)]
[(0, 0) (0, 0)]]
--------------------------
numpy.ones
Returns a new array of specified size and type, filled with ones.
2
Dtype
3
Order
'C' for C-style row-major array, 'F' for FORTRAN style column-major array
---------------------------------
# array of five ones. Default dtype is float
import numpy as np
x = np.ones(5)
print(x)
output:
[1. 1. 1. 1. 1.]
---------------------------------------
import numpy as np
x = np.ones([2,2], dtype = int)
print(x)
output:
[[1 1]
[1 1]]
-----------------
numpy.fromiter
This function builds an ndarray object from any iterable object. A new
one-dimensional array is returned by this function.
numpy.fromiter(iterable, dtype, count = -1)
1
iterable
2
dtype
3
count
The number of items to be read from iterator. Default is -1 which means all data to
be read
---------------------
# obtain iterator object from list
import numpy as np
list = range(5)
it = iter(list)
output:
[0. 1. 2. 3. 4.]
--------------------------------------
NumPy - Array From Numerical Ranges
numpy.arange
This function returns an ndarray object containing evenly spaced values
within a given range. The format of the function is as follows −
2
stop
3
step
4
dtype
Data type of resulting ndarray. If not given, data type of input is used
-------------------------------
import numpy as np
x = np.arange(5)
print(x)
output:
[0 1 2 3 4]
----------------------------
import numpy as np
# dtype set
x = np.arange(5, dtype = float)
print(x)
output:
[0. 1. 2. 3. 4.]
---------------------
# start and stop parameters set
import numpy as np
x = np.arange(10,20,2)
print(x)
output:
[10 12 14 16 18]
-----------------------------
numpy.linspace
This function is similar to arange() function. In this function,
instead of step size, the number of evenly spaced values between
the interval is specified.
numpy.linspace(start, stop, num, endpoint, retstep, dtype)
1
start
2
stop
The end value of the sequence, included in the sequence if endpoint set to true
3
num
4
endpoint
5
retstep
6
dtype
output:
[10. 12. 14. 16. 18.]
---------------------
# find retstep value
import numpy as np
x = np.linspace(1,2,5, retstep = True)
print(x)
output:
(array([1. , 1.25, 1.5 , 1.75, 2. ]), 0.25)
-------------------------------------------
NumPy - Indexing & Slicing
Contents of ndarray object can be accessed and modified by indexing
or slicing, just like Python's in-built container objects.
output:
[0 1 2 3 4 5 6 7 8 9]
[2 4 6]
-----------------
Slicing parameters separated by a colon : (start:stop:step) directly
to the ndarray object.
import numpy as np
a = np.arange(10)
print(a)
b = a[2:7:2]
print(b)
output:
[0 1 2 3 4 5 6 7 8 9]
[2 4 6]
----------------------
# slice single item
import numpy as np
a = np.arange(10)
print(a)
b = a[5]
print(b)
output:
[0 1 2 3 4 5 6 7 8 9]
5
-----------------------------
# slice items between indexes
import numpy as np
a = np.arange(10)
print(a)
print(a[2:5])
output:
[0 1 2 3 4 5 6 7 8 9]
[2 3 4]
--------------------------
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print(a)
# slice items starting from index
print('Now we will slice the array from the index a[1:]')
print(a[1:])
output:
[[1 2 3]
[3 4 5]
[4 5 6]]
Now we will slice the array from the index a[1:]
[[3 4 5]
[4 5 6]]
----------------------------------
Slicing can also include ellipsis (…) to make a selection tuple of
the same length as the dimension of an array. If ellipsis is used
at the row position, it will return an ndarray comprising of items in rows.
# array to begin with
----------------------------
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print('Our array is:')
print(a)
# this returns array of items in the second column
print('The items in the second column are:')
print(a[..., 1])
print('\n')
output:
Integer Indexing
This mechanism helps in selecting any arbitrary item in an array
based on its Ndimensional index. Each integer array represents the
number of indexes into that dimension. When the index consists of
as many integer arrays as the dimensions of the target ndarray,
it becomes straightforward.
------------------
import numpy as np
x = np.array([[1, 2], [3, 4], [5, 6]])
print(x)
y = x[[0,1,2], [0,1,0]]
print(y)
output:
[[1 2]
[3 4]
[5 6]]
[1 4 5]
The selection includes elements at (0,0), (1,1) and (2,0) from the
first array.
-----------------------------------
import numpy as np
x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
print('Our array is:')
print(x)
print('\n')
output:
Our array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
output:
Our array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
output:
[ 10 40 90 160]
Size in each dimension of the output shape is maximum of the input sizes
in that dimension. An input can be used in calculation, if its size in a
particular dimension matches the output size or its value is exactly 1.
print('Second array:')
print(b)
output:
First array:
[[ 0. 0. 0.]
[10. 10. 10.]
[20. 20. 20.]
[30. 30. 30.]]
Second array:
[1. 2. 3.]
First Array + Second Array
[[ 1. 2. 3.]
[11. 12. 13.]
[21. 22. 23.]
[31. 32. 33.]]
--------------------------------
NumPy - Iterating Over Array
NumPy package contains an iterator object numpy.nditer. It is an efficient
multidimensional iterator object using which it is possible to iterate
over an array. Each element of an array is visited using Python’s standard
Iterator interface.
---------------
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
output:
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55
---------------------------------------
Iterating over the transpose of the above array.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
output:
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Transpose of the original array is:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55
------------------------------------------
Iteration Order
If the same elements are stored using F-style order, the iterator chooses the more
efficient way of iterating
over an array.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
output:
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Transpose of the original array is:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
Sorted in C-style order:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
0 20 40 5 25 45 10 30 50 15 35 55 Sorted in F-style order:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
0 5 10 15 20 25 30 35 40 45 50 55
-------------------------------
NumPy - Array Manipulation
Several routines are available in NumPy package for manipulation of elements in
ndarray object.
--------------
Changing Shape:
numpy.reshape
This function gives a new shape to an array without changing the data. It accepts
the following parameters −
numpy.reshape(arr, newshape, order')
import numpy as np
a = np.arange(8)
print('The original array:')
print(a)
b = a.reshape(4,2)
print('The modified array:')
print(b)
output:
The original array:
[0 1 2 3 4 5 6 7]
The modified array:
[[0 1]
[2 3]
[4 5]
[6 7]]
--------------
numpy.ndarray.flat
This function returns a 1-D iterator over the array. It behaves similar to Python's
built-in iterator.
import numpy as np
a = np.arange(8).reshape(2,4)
print('The original array:')
print(a)
output:
The original array:
[[0 1 2 3]
[4 5 6 7]]
After applying the flat function:
5
-----------------
numpy.ndarray.flatten
This function returns a copy of an array collapsed into one dimension. The function
takes the following parameters.
ndarray.flatten(order)
import numpy as np
a = np.arange(8).reshape(2,4)
output:
The original array is:
[[0 1 2 3]
[4 5 6 7]]
The flattened array is:
[0 1 2 3 4 5 6 7]
The flattened array in F-style ordering:
[0 4 1 5 2 6 3 7]
--------------------
Transpose Operations
numpy.transpose
This function permutes the dimension of the given array. It returns a view
wherever possible. The function takes the following parameters.
-----------------------------
numpy.transpose(arr)
import numpy as np
a = np.arange(12).reshape(3,4)
Output:
The original array is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
The transposed array is:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
----------------------------
numpy.ndarray.T
This function belongs to ndarray class. It behaves similar to numpy.transpose.
import numpy as np
a = np.arange(12).reshape(3,4)
output:
The original array is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Array after applying the function:
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
---------------------
Joining Arrays
numpy.concatenate
Concatenation refers to joining. This function is used to join two or more arrays
of the same shape along a
specified axis. The function takes the following parameters.
print('First array:')
print(a)
b = np.array([[5,6],[7,8]])
print('Second array:')
print(b)
output:
First array:
[[1 2]
[3 4]]
Second array:
[[5 6]
[7 8]]
Joining the two arrays along axis 0:
[[1 2]
[3 4]
[5 6]
[7 8]]
Joining the two arrays along axis 1:
[[1 2 5 6]
[3 4 7 8]]
-----------------------------
numpy.stack
This function joins the sequence of arrays along a new axis.
numpy.stack(arrays, axis)
------------------
import numpy as np
a = np.array([[1,2],[3,4]])
print('First Array:')
print(a)
b = np.array([[5,6],[7,8]])
print('Second Array:')
print(b)
output:
First Array:
[[1 2]
[3 4]]
Second Array:
[[5 6]
[7 8]]
Stack the two arrays along axis 0:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Stack the two arrays along axis 1:
[[[1 2]
[5 6]]
[[3 4]
[7 8]]]
------------------------------------
numpy.hstack
Variants of numpy.stack function to stack so as to make a single array
horizontally.
import numpy as np
a = np.array([[1,2],[3,4]])
print('First array:')
print(a)
b = np.array([[5,6],[7,8]])
print('Second array:')
print(b)
print('Horizontal stacking:')
c = np.hstack((a,b))
print(c)
output:
First array:
[[1 2]
[3 4]]
Second array:
[[5 6]
[7 8]]
Horizontal stacking:
[[1 2 5 6]
[3 4 7 8]]
---------------------
numpy.vstack
Variants of numpy.stack function to stack so as to make a single array
vertically.
import numpy as np
a = np.array([[1,2],[3,4]])
print('First array:')
print(a)
b = np.array([[5,6],[7,8]])
print('Second array:')
print(b)
print('Vertical stacking:')
c = np.vstack((a,b))
print(c)
output:
First array:
[[1 2]
[3 4]]
Second array:
[[5 6]
[7 8]]
Vertical stacking:
[[1 2]
[3 4]
[5 6]
[7 8]]
-------------------------
NumPy - Array Manipulation
Splitting Arrays
This function divides the array into subarrays along a specified axis.
The function takes three parameters.
2
indices_or_sections
3
axis
Default is 0
---------------------------
import numpy as np
a = np.arange(9)
print('First array:')
print(a)
print('Split the array in 3 equal-sized subarrays:')
b = np.split(a,3)
print(b)
output:
First array:
[0 1 2 3 4 5 6 7 8]
Split the array in 3 equal-sized subarrays:
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
Split the array at positions indicated in 1-D array:
[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]
----------------------
numpy.hsplit
The numpy.hsplit is a special case of split() function where axis is 1
indicating a horizontal
split regardless of the dimension of the input array.
import numpy as np
a = np.arange(16).reshape(4,4)
print('First array:')
print(a)
print('Horizontal splitting:')
b = np.hsplit(a,2)
print(b)
output:
First array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Horizontal splitting:
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]
--------------
numpy.vsplit
numpy.vsplit is a special case of split() function where axis is 1
indicating a vertical split regardless of the dimension of the
input array. The following example makes this clear.
import numpy as np
a = np.arange(16).reshape(4,4)
print('First array:')
print(a)
print('Vertical splitting:')
b = np.vsplit(a,2)
print(b)
Output:
First array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Vertical splitting:
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]
-----------------------
Adding / Removing Elements
numpy.resize
This function returns a new array with the specified size. If the new
size is greater than the original, the repeated copies of entries in
the original are contained. The function takes the following parameters.
numpy.resize(arr, shape)
1
arr
2
shape
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print('First array:')
print(a)
b = np.resize(a, (3,2))
print('Second array:')
print(b)
output:
First array:
[[1 2 3]
[4 5 6]]
The shape of first array:
(2, 3)
Second array:
[[1 2]
[3 4]
[5 6]]
The shape of second array:
(3, 2)
Resize the second array:
[[1 2 3]
[4 5 6]
[1 2 3]]
----------------
numpy.append
This function adds values at the end of an input array. The append
operation is not inplace, a new array is allocated. Also the dimensions
of the input arrays must match otherwise ValueError will be generated.
Input array
2
values
3
axis
print('First array:')
print(a)
b = np.array([7, 8, 9])
print('Second array:')
print(b)
print('Append elements to array:')
print(np.append(a, b))
output:
First array:
[[1 2 3]
[4 5 6]]
Second array:
[7 8 9]
Append elements to array:
[1 2 3 4 5 6 7 8 9]
Append elements along axis 0:
[[1 2 3]
[4 5 6]
[7 8 9]]
Append elements along axis 1:
[[1 2 3 5 5 5]
[4 5 6 7 8 9]]
------------------
numpy.insert
This function inserts values in the input array along the given axis
and before the given index. If the type of values is converted to be
inserted, it is different from the input array. Insertion is not done
in place and the function returns a new array. Also, if the axis is not
mentioned, the input array is flattened.
Input array
2
pos
3
values
4
axis
The axis along which to insert. If not given, the input array is flattened
-----------------------------------
import numpy as np
a = np.array([[1,2],[3,4],[5,6]])
print('First array:')
print(a)
print('Axis parameter not passed. The input array is flattened before insertion.')
print(np.insert(a, 3, [11, 12]))
print('Axis parameter passed. The values array is broadcast to match input array.')
output:
First array:
[[1 2]
[3 4]
[5 6]]
Axis parameter not passed. The input array is flattened before insertion.
[ 1 2 3 11 12 4 5 6]
Axis parameter passed. The values array is broadcast to match input array.
Broadcast along axis 0:
[[ 1 2]
[11 11]
[ 3 4]
[ 5 6]]
Broadcast along axis 1:
[[ 1 11 2]
[ 3 11 4]
[ 5 11 6]]
---------------------------------
numpy.delete
This function returns a new array with the specified subarray deleted from
the input array. As in case of insert() function, if the axis parameter
is not used, the input array is flattened.
The function takes the following parameters −
Input array
2
obj
3
axis
The axis along which to delete the given subarray. If not given, arr is flattened
-----------------------------------
import numpy as np
a = np.arange(12).reshape(3,4)
print('First array:')
print(a)
print('Column 2 deleted:')
print(np.delete(a, 1, axis=1))
Output:
First array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Array flattened before delete operation as axis not used:
[ 0 1 2 3 4 6 7 8 9 10 11]
Column 2 deleted:
[[ 0 2 3]
[ 4 6 7]
[ 8 10 11]]
---------------
numpy.unique
This function returns an array of unique elements in the input array.
The function can be able to return a tuple of array of unique vales and
an array of associated indices. Nature of the indices depend upon the
type of return parameter in the function call.
2
return_index
3
return_inverse
If True, returns the indices of unique array, which can be used to reconstruct the
input array
4
return_counts
If True, returns the number of times the element in unique array appears in the
original array
--------------------------------
import numpy as np
a = np.array([5,2,6,2,7,5,6,8,2,9])
print('First array:')
print(a)
print('\n')
print('Indices are:')
print(indices)
print('\n')
output:
First array:
[5 2 6 2 7 5 6 8 2 9]
Indices are:
[1 0 2 0 3 1 2 4 0 5]
-----------------
NumPy - String Functions
standard string functions in Python's built-in library.
numpy.char.add()
This function performs elementwise string concatenation.
----------------------
import numpy as np
print('Concatenation example:')
print(np.char.add(['hello', 'hi'], [' abc', ' xyz']))
output:
Concatenate two strings:
['hello xyz']
Concatenation example:
['hello abc' 'hi xyz']
------------------------------
numpy.char.multiply()
This function performs multiple concatenation.
import numpy as np
output:
Hello Hello Hello
---------------------------
numpy.char.center()
This function returns an array of the required width so that the input string is
centered and padded on the
left and right with fillchar.
-------------------------
import numpy as np
# np.char.center(arr, width,fillchar)
print(np.char.center('hello', 20, fillchar='*'))
output:
*******hello********
-----------------------------
numpy.char.capitalize()
This function returns the copy of the string with the first letter
capitalized.
----------------------------
import numpy as np
print(np.char.capitalize('hello world'))
output:
Hello world
-------------------------------
numpy.char.title()
This function returns a title cased version of the input string with the
first letter of each word capitalized.
--------------------------
import numpy as np
print(np.char.title('hello how are you?'))
output:
Hello How Are You?
-----------------------------
numpy.char.lower()
This function returns an array with elements converted to lowercase. It calls
str.lower for each element.
-----------------------
import numpy as np
print(np.char.lower(['HELLO', 'WORLD']))
print(np.char.lower('HELLO'))
output:
['hello' 'world']
hello
-----------------------
numpy.char.upper()
This function calls str.upper function on each element in an array to return the
uppercase array elements.
-------------------------------------
import numpy as np
print(np.char.upper('hello'))
print(np.char.upper(['hello', 'world']))
output:
HELLO
['HELLO' 'WORLD']
------------------------------------------
numpy.char.split()
This function returns a list of words in the input string. By default, a whitespace
is used as a separator.
Otherwise the specified separator character is used to spilt the string.
------------------------------
import numpy as np
print(np.char.split('hello how are you?'))
print(np.char.split('India, is my ,country', sep=','))
output:
['hello', 'how', 'are', 'you?']
['India', ' is my ', 'country']
----------------------------------------
numpy.char.strip()
This function returns a copy of array with elements stripped of the
specified characters leading and/or trailing in it.
---------------------------
import numpy as np
print(np.char.strip('ashok arora', 'a'))
print(np.char.strip(['arora', 'admin', 'java'], 'a'))
output:
shok aror
['ror' 'dmin' 'jav']
----------------
numpy.char.join()
This method returns a string in which the individual characters are joined by
separator character specified.
------------------------------
import numpy as np
print(np.char.join(':', 'dmy'))
print(np.char.join([':', '-'], ['dmy', 'ymd']))
output:
d:m:y
['d:m:y' 'y-m-d']
-----------------------
numpy.char.replace()
This function returns a new copy of the input string in which all
occurrences of the sequence of characters is
replaced by another given sequence
--------------------------------
import numpy as np
print(np.char.replace('He is a good boy', 'is', 'was'))
output:
He was a good boy
-----------------------------------------------------
NumPy - Mathematical Functions:
output:
Sine of different angles:
[0. 0.5 0.70710678 0.8660254 1. ]
print('Inverse of cos:')
inv = np.arccos(cos)
print(inv)
print('\n')
print('In degrees:')
print(np.degrees(inv))
print('\n')
print('Tan function:')
tan = np.tan(a*np.pi/180)
print(tan)
print('\n')
print('Inverse of tan:')
inv = np.arctan(tan)
print(inv)
print('\n')
print('In degrees:')
print(np.degrees(inv))
output:
Array containing sine values:
[0. 0.5 0.70710678 0.8660254 1. ]
Inverse of cos:
[0. 0.52359878 0.78539816 1.04719755 1.57079633]
In degrees:
[ 0. 30. 45. 60. 90.]
Tan function:
[0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
Inverse of tan:
[0. 0.52359878 0.78539816 1.04719755 1.57079633]
In degrees:
[ 0. 30. 45. 60. 90.]
------------------------------
Functions for Rounding
numpy.around()
This is a function that returns the value rounded to the desired precision.
The function takes the following parameters.
numpy.around(a,decimals)
1
a
Input data
2
decimals
print('After rounding:')
print(np.around(a))
print(np.around(a, decimals=1))
output:
Original array:
[ 1. 5.55 123. 0.567 25.532]
After rounding:
[ 1. 6. 123. 1. 26.]
[ 1. 5.6 123. 0.6 25.5]
-------------------------------------------
numpy.ceil()
The ceil() function returns the ceiling of an input value, i.e. the ceil of
the scalar x is the smallest integer i,
such that i >= x.
---------------------------------
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
output:
The given array:
[-1.7 1.5 -0.2 0.6 10. ]
The modified array:
[-1. 2. -0. 1. 10.]
-------------------------------------
numpy.floor()
This function returns the largest integer not greater than the input parameter.
The floor of the scalar x is the largest integer i, such that i <= x. Note
that in Python, flooring always is rounded away from 0.
----------------------------
import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])
print('The given array:')
print(a)
print('\n')
print('The modified array:')
print(np.floor(a))
output:
The given array:
[-1.7 1.5 -0.2 0.6 10. ]
-------------------------------------
NumPy - Arithmetic Operations
Input arrays for performing arithmetic operations such as add(), subtract(),
multiply(), and divide() must be either of the same shape or should conform
to array broadcasting rules.
---------------------------------------
import numpy as np
a = np.arange(9, dtype = np.float_).reshape(3,3)
print('First array:')
print(a)
print('\n')
print('Second array:')
b = np.array([10,10,10])
print(b)
print('\n')
output:
First array:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
Second array:
[10 10 10]
-------------------------------------
numpy.reciprocal()
This function returns the reciprocal of argument, element-wise. For elements
with absolute values larger than 1, the result is always 0 because of the
way in which Python handles integer division. For integer 0, an overflow
warning is issued.
---------------------
import numpy as np
a = np.array([0.25, 1.33, 1, 0, 100])
print('Our array is:')
print(a)
print('\n')
print('After applying reciprocal function:')
print(np.reciprocal(a))
print('\n')
output:
Our array is:
[ 0.25 1.33 1. 0. 100. ]
--------------------------
numpy.power()
This function treats elements in the first input array as base and returns
it raised to the power of the corresponding element in the second input array.
---------------------------------
import numpy as np
a = np.array([10,100,1000])
print('Our array is:')
print(a)
print('\n')
print('Applying power function:')
print(np.power(a, 2))
print('\n')
print('Second array:')
b = np.array([1,2,3])
print(b)
print('\n')
output:
Our array is:
[ 10 100 1000]
Second array:
[1 2 3]
print('First array:')
print(a)
print('\n')
print('Second array:')
print(b)
print('\n')
output:
First array:
[10 20 30]
Second array:
[3 5 7]
numpy.real() − returns the real part of the complex data type argument.
numpy.imag() − returns the imaginary part of the complex data type argument.
numpy.angle() − returns the angle of the complex argument. The function has
degree parameter. If true, the angle in the degree is returned, otherwise
the angle is in radians.
---------------------------------------
import numpy as np
a = np.array([-5.6j, 0.2j, 11. , 1+1j])
output:
Our array is:
[-0.-5.6j 0.+0.2j 11.+0.j 1.+1.j ]
---------------------------------
NumPy - Statistical Functions
NumPy has quite a few useful statistical functions for finding minimum,
maximum, percentile standard deviation and variance, etc. from the given
elements in the array. The functions are explained as follows −
numpy.amin() and numpy.amax()
These functions return the minimum and the maximum from the elements in the
given array along the specified axis.
--------------------------
import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print('Our array is:')
print(a)
print('\n')
print('Applying amin() function:')
print(np.amin(a, 1))
print('\n')
output:
Our array is:
[[3 7 5]
[8 4 3]
[2 4 9]]
--------------
numpy.ptp()
The numpy.ptp() function returns the range (maximum-minimum) of values along
an axis.
numpy.ptp()function plays an important role in statistics by finding out
Range of given numbers. Range = max value – min value.
-------------------------------------------
import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print('Our array is:')
print(a)
print('\n')
output:
Our array is:
[[3 7 5]
[8 4 3]
[2 4 9]]
----------------------
numpy.percentile()
Percentile (or a centile) is a measure used in statistics indicating the value
below which a given percentage of observations in a group of observations fall.
The function numpy. percentile() takes the following arguments.
numpy.percentile(a, q, axis)
1
a
Input array
2
q
3
axis
output:
Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]
-----------------------
numpy.median()
Median is defined as the value separating the higher half of a data sample
from the lower half.
----------------------------
import numpy as np
a = np.array([[30, 65, 70], [80, 95, 10], [50, 90, 60]])
print('Our array is:')
print(a)
print('\n')
output:
Our array is:
[[30 65 70]
[80 95 10]
[50 90 60]]
output:
Our array is:
[[1 2 3]
[3 4 5]
[4 5 6]]
------------------------
numpy.average()
Weighted average is an average resulting from the multiplication of each
component by a factor reflecting its importance. The numpy.average()
function computes the weighted average of elements in an array according
to their respective weight given in another array. The function can have
an axis parameter. If the axis is not specified, the array is flattened.
import numpy as np
a = np.array([1,2,3,4])
output:
Our array is:
[1 2 3 4]
Sum of weights
(2.0, 10.0)
--------------------------------
In a multi-dimensional array, the axis for computation can be specified.
import numpy as np
a = np.arange(6).reshape(3,2)
print('Our array is:')
print(a)
print('\n')
print('Modified array:')
wt = np.array([3,5])
print(np.average(a, axis=1, weights=wt))
print('\n')
print('Modified array:')
print(np.average(a, axis=1, weights=wt, returned=True))
output:
Our array is:
[[0 1]
[2 3]
[4 5]]
Modified array:
[0.625 2.625 4.625]
Modified array:
(array([0.625, 2.625, 4.625]), array([8., 8., 8.]))
-----------------------------------
Standard Deviation
Standard deviation is the square root of the average of squared deviations from
mean.
------------------------------------
import numpy as np
print(np.std([1, 2, 3, 4]))
output:
1.118033988749895
-------------------------------------
Variance
Variance is the average of squared deviations, i.e., mean(abs(x - x.mean())**2). In
other words,
the standard deviation is the square root of variance.
-----------------------------------
import numpy as np
print(np.var([1, 2, 3, 4]))
output:
1.25
----------------------------------------------------
NumPy - Sort, Search & Counting Functions
A variety of sorting related functions are available in NumPy. These sorting
functions implement different sorting algorithms, each of them characterized
by the speed of execution, worst case performance, the workspace required
and the stability of algorithms.
‘quicksort’
‘mergesort’
‘heapsort’
-----------------
numpy.sort()
The sort() function returns a sorted copy of the input array. It has the
following parameters −
1
a
Array to be sorted
2
axis
The axis along which the array is to be sorted. If none, the array is flattened,
sorting on the last axis
3
kind
Default is quicksort
4
order
print('Order by name:')
print(np.sort(a, order='name'))
output:
Our array is:
[[3 7]
[9 1]]
Applying sort() function:
[[3 7]
[1 9]]
Sort along axis 0:
[[3 1]
[9 7]]
Our array is:
[(b'raju', 21) (b'anil', 25) (b'ravi', 17) (b'amar', 27)]
Order by name:
[(b'amar', 27) (b'anil', 25) (b'raju', 21) (b'ravi', 17)]
--------------------
numpy.argmax() and numpy.argmin()
These two functions return the indices of maximum and minimum elements
respectively along the given axis.
-------------------
import numpy as np
a = np.array([[30, 40, 70], [80, 20, 10], [50, 90, 60]])
print('Our array is:')
print(a)
print('\n')
print('Applying argmax() function:')
print(np.argmax(a))
print('\n')
print('Flattened array:')
print(a.flatten()[minindex])
print('\n')
print('Flattened array along axis 0:')
minindex = np.argmin(a, axis=0)
print(minindex)
print('\n')
output:
Our array is:
[[30 40 70]
[80 20 10]
[50 90 60]]
Flattened array:
10
--------------
numpy.nonzero()
The numpy.nonzero() function returns the indices
of non-zero elements in the input array.
---------------------------------
import numpy as np
a = np.array([[30,40,0],[0,20,10],[50,0,60]])
print('Our array is:')
print(a)
print('\n')
print('Applying nonzero() function:')
print(np.nonzero(a))
Output:
ur array is:
[[30 40 0]
[ 0 20 10]
[50 0 60]]
Output:
Our array is:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
Indices of elements > 3
(array([1, 1, 2, 2, 2], dtype=int64), array([1, 2, 0, 1, 2], dtype=int64))
Use these indices to get elements satisfying the condition
[4. 5. 6. 7. 8.]
----------------------------
numpy.extract()
The extract() function returns the elements
satisfying any condition.
----------------------------------
import numpy as np
x = np.arange(9.).reshape(3, 3)
print('Our array is:')
print(x)
# define a condition
condition = np.mod(x,2) == 0
Output:
Our array is:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
Element-wise value of condition
[[ True False True]
[False True False]
[ True False True]]
Extract elements using condition
[0. 2. 4. 6. 8.]
-------------------------------------
NumPy - Matrix Library
NumPy package contains a Matrix library numpy.matlib. This module has
functions that return matrices instead of ndarray
objects.
--------------------------------
matlib.empty()
The matlib.empty() function returns a new matrix without initializing
the entries. The function takes the following parameters.
1
shape
2
Dtype
3
order
C or F
--------------------
import numpy.matlib
import numpy as np
print(np.matlib.empty((2, 2)))
# filled with random data
Output:
[[0. 0.]
[0. 0.]]
-----------------
numpy.matlib.zeros()
This function returns the matrix filled with zeros.
import numpy.matlib
import numpy as np
print(np.matlib.zeros((2,2)) )
output:
[[0. 0.]
[0. 0.]]
-----------
numpy.matlib.ones()
This function returns the matrix filled with 1s.
------------------------------
import numpy.matlib
import numpy as np
print (np.matlib.ones((2,2)))
Output:
[[1. 1.]
[1. 1.]]
-----------------------------------
numpy.matlib.eye()
This function returns a matrix with 1 along the diagonal elements
and the zeros elsewhere. The function takes the following parameters.
numpy.matlib.eye(n, M,k, dtype)
1
n
2
M
3
k
Index of diagonal
4
dtype
Output:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]
-----------------------
numpy.matlib.identity()
The numpy.matlib.identity() function returns the Identity matrix of
the given size. An identity matrix is a square matrix with all
diagonal elements as 1.
------------------------------
import numpy.matlib
import numpy as np
print (np.matlib.identity(5, dtype = float))
Output:
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
-------------------------
numpy.matlib.rand()
The numpy.matlib.rand() function returns a matrix of the given size
filled with random values.
--------------------------------------
import numpy.matlib
import numpy as np
print(np.matlib.rand(3,3))
Output:
[[0.85187087 0.60435697 0.44439556]
[0.29060045 0.15376698 0.03306542]
[0.0488378 0.99729703 0.87463283]]
----------------------------
NumPy - Linear Algebra
NumPy package contains numpy.linalg module that
provides all the functionality
required for linear algebra.
numpy.dot()
This function returns the dot product of two arrays. For 2-D vectors, it is the
equivalent
to matrix multiplication. For 1-D arrays, it is the inner product of the vectors.
-----------------------------
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
print("Array a")
print(a)
print("Array b")
print(b)
print("Dot product")
print(np.dot(a,b))
Output:
Array a
[[1 2]
[3 4]]
Array b
[[11 12]
[13 14]]
Dot product
[[37 40]
[85 92]]
---------------------------------
numpy.vdot()
This function returns the dot product of the two vectors. If the first argument is
complex, then its conjugate is used for calculation. If the argument id is multi-
dimensional
array, it is flattened.
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
print("Array a:")
print(a)
print("Array b:")
print(b)
print("V dot product:")
print(np.vdot(a,b))
#Note − 1*11 + 2*12 + 3*13 + 4*14 = 130
Output:
Array a:
[[1 2]
[3 4]]
Array b:
[[11 12]
[13 14]]
V dot product:
130
------------------------------------------
numpy.inner()
This function returns the inner product of vectors for 1-D arrays. For higher
dimensions, it returns the sum product over the last axes.
--------------------------
import numpy as np
print (np.inner(np.array([1,2,3]),np.array([0,1,0])) )
# Equates to 1*0+2*1+3*0
Output:
2
-------------------------
numpy.matmul()
The numpy.matmul() function returns the matrix product of two arrays. While
it returns
a normal product for 2-D arrays
# For 2-D array, it is matrix multiplication
--------------------
import numpy as np
a = [[1,0],[0,1]]
b = [[4,1],[2,2]]
print("Matrix a")
print(a)
print("Matrix b")
print(b)
print("Result:")
print (np.matmul(a,b))
Output:
Matrix a
[[1, 0], [0, 1]]
Matrix b
[[4, 1], [2, 2]]
Result:
[[4 1]
[2 2]]
-----------------------------
NumPy - Determinant
Determinant is a very useful value in linear algebra. It calculated from the
diagonal
elements of a square matrix. For a 2x2 matrix, it is simply the subtraction of the
product of the top left and bottom right element from the product of other two.
In other words, for a matrix [[a,b], [c,d]], the determinant is computed as ‘ad-
bc’.
The larger square matrices are considered to be a combination of 2x2 matrices.
The numpy.linalg.det() function calculates the determinant of the input matrix.
-----------------------------------------
import numpy as np
a = np.array([[1,2], [3,4]])
print (np.linalg.det(a))
Output:
-2.0000000000000004
----------------------------------
numpy.linalg.solve()
The numpy.linalg.solve() function gives the solution of linear equations in the
matrix form.
3x + 4y + 5z = 9
y + 2x+3z = 8
2x + 4y +5z = 7
------------------
# Program to show the working of solve()
import numpy as np
Output:
Array A is:
[[3 4 5]
[1 2 3]
[2 4 5]]
Array B is :
[9 8 7]
Answer of the equation is :
[ 2. -10.5 9. ]
True
---------------------------------
numpy.linalg.inv()
We use numpy.linalg.inv() function to calculate the
inverse of a matrix. The inverse of a matrix is such
that if it is multiplied by the original matrix, it
results in identity matrix.
---------------------------
import numpy as np
x = np.array([[1,2],[3,4]])
y = np.linalg.inv(x)
print(x)
print(y)
print(np.dot(x, y))
output:
[[1 2]
[3 4]]
[[-2. 1. ]
[ 1.5 -0.5]]
[[1.00000000e+00 1.11022302e-16]
[0.00000000e+00 1.00000000e+00]]
---------------------------------