Enable Dark Mode!
how-to-install-numpy-library-in-python.jpg
By: Chethana Ramachandran

How To Install Numpy Library in Python

Technical

NumPy is a Python package that is used for array processing. NumPy stands for Numeric Python. It supports the processing and computation of multidimensional array elements.
For the efficient calculation of arrays and matrices, NumPy adds a powerful data structure to Python, and it supplies a boundless library of high-level mathematical functions.
NumPy provides N-dimensional array types. It can be used for different mathematical operations on arrays.

Installation

The pip command is used to install the numpy package. Run the below command in the 

terminal.
> pip install numpy
If you don’t have pip on your system, you should first install Pip.

Import NumPy

To access the NumPy, we should import the numpy into our Python code. For that, we can use the command.
> import numpy as np
The np is used as an alias for the numpy.
Creating an array using numpy

To create an array using numpy, we can use the function np.array().

Example:
>>> import numpy as np
>>> a = np.array([1,2,3,4])
>>> print(a)
Output:
[1 2 3]
Ndarray
The most important feature of numpy is Ndarray. It is an N-dimensional array that stores a collection of similar types of elements.
Example:
>>> import numpy as np
>>> a = np.array( [1,2,3],[4,5,6] )
>>> print( “Type:”, type(a))
Output:
Type: <class 'numpy.ndarray'>

NumPy DataTypes

Like Python, NumPy supports different types of data types.  A list of scalar data types is demonstrated in the following table:
Sl.NoData TypeDescription
1bool_Stores true or false of boolean value as a byte.
2int_Default type of integer. It is similar to C long which contains 64-bit or 32-bit integers.
3intcSimilar to the C integer (c int) and it represents 32-bit or 64-bit int.
4intpThis integer represents the indexing
5int8Represents an 8-bit integer. The range is -128 to 127
6int16Represents a 16-bit (2 bytes) integer. The range is -32768 to 32767.
7int32Represents a 32-bit (4-byte) integer. The range is -2147483648 to 2147483647.
8int64Represents 64-bit (8-byte) integers. The range is -9223372036854775808 to 9223372036854775807.
9uint8Represents a 1-byte (8-bit) unsigned integer.
10uint16Represents a 2-byte (16-bit) unsigned integer.
11uint32Represents a 4-byte (32-bit) unsigned integer.
12uint64Represents 8-byte (64-bit) unsigned integer.
13float_Identical to float64
14float16Represents half-precision float. 5 bits are used for the exponent. 10 bits were used for the mantissa, and 1 bit for the sign.
15float32Represents single-precision float. 8 bits are used for the exponent. 23 bits were used for the mantissa, and 1 bit for the sign.
16float64Represents double-precision float. 11 bits used for the exponent. 52 bits used for mantissa, and 1 bit for the sign.
17complex_Identical to complex128
18complex64Represents the complex number, where the real and imaginary part shares 32 bits.
19complex128Represents the complex number, where the real and imaginary part shares 64 bits.

NumPy dtype

The data type of the NumPy array is obtained by using the object known as dtype.
Syntax:
numpy.dtype(object,align,copy)
Example:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr.dtype)
Output:
Int64
We can represent the Numpy data types with characters like i,b,u,S etc.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='i4')
print(arr)
print(arr.dtype)
Output:
[1 2 3 4]
int32

NumPy Array indexing

Array indexing in numpy is similar to accessing an array element. The indexing of the numpy array is started with 0, which means the first element index is 0, the second element index is 1,  and so on.
Example:
>>> import numpy as np
>>> a = np.array([1,2,3,4])
>>> print(a[3])
Output:
4

NumPy Array shape

NumPy arrays have the attribute called shape. It will return the number of elements in each dimension in a tuple.
Example:
>>>import numpy as np
>>>ar = np.array([[1, 2, 3], [5, 6, 7]])
>>>print(ar.shape)
Output:
(2,3)

NumPy Array Reshaping

In NumPy, which has one dimensional, two dimensional and three-dimensional arrays, we can change the shape of the array by using reshape.
Example:
>>>import numpy as np
>>>ar1 = np.array([1, 2, 4, 8, 5, 6, 7, 3, 9, 10, 11, 12])
>>>newarr = ar1.reshape(4, 3)
>>>print(newarr)
Output:
[[1    2    4]
 [8    5    6]
 [7    3    9]
 [10 11 12]]

NumPy Array Slicing

NumPy array slicing means the extraction of a portion of an array from the original array. 
It can be demonstrated by the syntax: [start: stop: step]
The start, stop, step are the parameters passed to the array; start indicates the start index of the array to be sliced, the default index value is 0; stop indicates the length of the array to be sliced, the step will consider the default value is 1 if we don't pass any value.
Example:
>>>import numpy as np
>>>arr = np.array([3, 5, 7, 8, 11, 14, 18])
>>>arr2 = arr[1:6]
>>>print(arr2) 
Output:
[ 5  7  8 11 14]

NumPy Array Joining

NumPy provides different kinds of functions to combine the arrays. 
numpy.concatenate
numpy.stack
By using the concatenate function, it will join two or more arrays along a specified axis. The default axis value is 0.
Example:
import numpy as np
array_1 = np.array([1, 2])
array_2 = np.array([3, 4])
array_new = np.concatenate((array_1, array_2))
print(array_new)
Output:
[1 2 3 4]
By using the stack function, it will join two or more arrays along a new axis. 
Example:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)
Output:
[[1 4]
 [2 5]
 [3 6]]

NumPy Copy and View

In NumPy, some functions return a copy of an array or some return the view while executing a function, when the contents are physically stored in another location, it is called copy. The view will return a new array that looks like the exact location data of the original array.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.view() //declare the view
y = arr.copy() //declare the copy
arr[0] = 16
print(arr)
print(x)
print(y)
Output:
[16  2  3  4  5]
[16  2  3  4  5]
[1 2 3 4 5]
Overall Numpy arrays are faster than Python lists. And it will take less memory to process.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message