What is the Ndarray Object in NumPy | Python NumPy | Part-2 | Tipszon

Installation of NumPy and Introduction to Ndarray

In previous part , I discussed about NumPy introduction and reason about NumPy fast behaviour , now I am going to discuss about installation of NumPy and core of the NumPy which is Ndarray.

Let's delve into the details.

Installation of NumPy

  • The only prerequisite for NumPy is Python itself means you have to install Python on your PC.
  • But If you don't have Python yet then you can use Anaconda distribution which includes Python, NumPy, and other commonly used packages for scientific computing and data science.
  • NumPy can be installed with conda, with pipOn windows
  • for this just open your Command prompt and use following anyone command according to your system.

CONDA

  • If you use conda, you can install it with:
conda install numpy

PIP

  • If you use pip, you can install it with:
 pip install numpy

NumPy- Ndarray Object

  • At the core of the NumPy package, is the ndarray object. 
  • This encapsulates n-dimensional arrays of homogeneous data types, with many operations being performed in compiled code for performance. 
  • There are several important differences between NumPy arrays and the standard Python sequences:

  1. NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original.
  2. The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory. The exception: one can have arrays of (Python, including NumPy) objects, thereby allowing for arrays of different sized elements.
  3. NumPy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efficiently and with less code than is possible using Python’s built-in sequences.

  • In other words, in order to efficiently use much (perhaps even most) of today’s scientific/mathematical Python-based software, just knowing how to use Python’s built-in sequence types is insufficient - one also needs to know how to use NumPy arrays.

NumPys array class is called ndarray. It is also known by the alias array. Note that numpy.array is not the same as the Standard Python Library class array.array, which only handles one-dimensional arrays and offers less functionality.

ARRAY CREATION

There are several ways to create arrays.

  • For example, 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.
>>> import numpy as np
>>> a = np.array([2,3,4])
>>> aarray([2, 3, 4])

The type of the array can also be explicitly specified at creation time:
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )
>>> carray([[ 1.+0.j, 2.+0.j], 
                    [ 3.+0.j, 4.+0.j]])
>>> aarray([2, 3, 4]) 
  • The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.
>>> np.zeros( (3,4) )
array([[ 0., 0., 0., 0.], 
           [ 0., 0., 0., 0.], 
           [ 0., 0., 0., 0.]])
>>> np.ones( (2,3,4), dtype=np.int16 )            # dtype can also be specified
array([[[ 1, 1, 1, 1], 
            [ 1, 1, 1, 1], 
            [ 1, 1, 1, 1]], 
            [[ 1, 1, 1, 1], 
            [ 1, 1, 1, 1], 
            [ 1, 1, 1, 1]]], dtype=int16)
>>> np.empty( (2,3) )                              # uninitialized, output may vary array
([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260], 
[ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])) 
  • To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.In  this 10 is starting number , 30 is last number and rember this number will not be include in result and 5 is the difference between any two numbers.
>>> np.arange( 10, 30, 5 )
array([10, 15, 20, 25]) 
>>> np.arange( 0, 2, 0.3 )                               # it accepts float argumentsarray
([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
  • When arange is used with floating point arguments, it is generally not possible to predict the number of elements obtained, due to the finite floating point precision.
  • For this reason, it is usually better to use the function linspace that receives as an argument the number of elements
>>> from numpy import pi
>>> np.linspace( 0, 2, 9 )                                       # 9 numbers from 0 to 2
array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
>>> x = np.linspace( 0, 2*pi, 100 )                        # useful to evaluate function at lots of points
>>> f = np.sin(x)

Creation of Identity Matrix 

  • np.eye function is used to create identity matrix.
d = np.eye(2)            # Create a 2x2 identity matrix
print(d)
[[1. 0.]
[0. 1.]]
 

Creation of array of random numbers

  •  np.random.rand function is used to create array with random numbers
s = np.random.rand(5)
array([0.73361698, 0.22411148, 0.1100682 , 0.38994898, 0.3913664 ])

Creation of array of same number [Constant]

  • np.full function is used to create array of constant number.
c = np.full((2,2), 7)         # Create a constant array
print(c)                            # Prints 
[[ 7. 7.] 
[ 7. 7.]]


Post a Comment

0 Comments