0% found this document useful (0 votes)
26 views26 pages

Understanding NumPy Ndarray Basics

The document provides an overview of NumPy's Ndarray, highlighting its role in handling n-dimensional arrays in Python, including features like efficient data manipulation, matrix operations, and reshaping. It details the creation of Ndarrays, data types, and various functions for array operations such as slicing, reshaping, and performing arithmetic operations. Additionally, it covers NumPy's data types and structured data types, emphasizing their importance in data handling and manipulation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views26 pages

Understanding NumPy Ndarray Basics

The document provides an overview of NumPy's Ndarray, highlighting its role in handling n-dimensional arrays in Python, including features like efficient data manipulation, matrix operations, and reshaping. It details the creation of Ndarrays, data types, and various functions for array operations such as slicing, reshaping, and performing arithmetic operations. Additionally, it covers NumPy's data types and structured data types, emphasizing their importance in data handling and manipulation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

NumPy Ndarray

Mr. Ullas S.
Assistant Professor (Sr. Gr.)
CSE, ASC, Amrita Vishwa Vidyapeetham,
Bengaluru
NumPy Ndarray
• In Python, the standard library for NDArrays is called NumPy.
However, there is no equivalent standard library in Java. One offering
for Java developers interested in working with NDArrays is AWS's
Deep Java Library (DJL)
• NumPy provides a convenient and efficient way to handle the vast
amount of data.
• NumPy is also very convenient with Matrix multiplication and data
reshaping.
• NumPy is fast which makes it reasonable to work with a large set of
data
Ndarray
• Ndarray is the n-dimensional array object defined in the numpy which
stores the collection of the similar type of elements
• The ndarray object can be accessed by using the 0 based indexing.
Each element of the Array object contains the same size in the
memory.
• The ndarray object can be created by using the array routine of the
numpy module. For this purpose, we need to import the numpy.
• >>> import numpy as mp
• >>> a = [Link]
• >>> print(a)
• >>> [Link](object, dtype = None, copy = True, order = None, su
bok = False, ndmin = 0
SN Parameter Description
1 object It represents the collection object. It can be a list, tuple, dictionary, set, etc.
2 dtype We can change the data type of the array elements by changing this option
to the specified type. The default is none.

3 copy It is optional. By default, it is true which means the object is copied.


4 order There can be 3 possible values assigned to this option. It can be C (column
order), R (row order), or A (any)

5 subok The returned array will be base class array by default. We can change this
to make the subclasses passes through by setting this option to true.

6 ndmin It represents the minimum dimensions of the resultant array.


To create an array using the list
• >>> a = [Link]([1, 2, 3])
• >>>print(a)
[1 2 4]
• To create a multi-dimensional array object, use the following syntax.
>>> a = [Link]([[1, 2, 3], [4, 5, 6]])
>>>print(a)
[ [1 2 4]
[4 5 6]]
>>>
Data Type - Changing
• To change the data type of the array elements, mention the name of
the data type along with the collection.
>>> a = [Link]([1, 3, 5, 7], complex)
>>>print a
[1.+0.j 3.+0.j. 7.+0.j]
• The ndim function can be used to find the dimensions of the array.
Finding the dimensions of the Array
• The ndim function can be used to find the dimensions of the array.
>>> import numpy as np
>>> arr = [Link]([[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 23]])
>>> print([Link])
2

>>> print (arr)


[ [ 1 2 3 4]
[ 4 5. 6 7]
[ 9 10 11 23]
Finding the size of each array
element
import numpy as np
a = [Link]([[1,2,3]])
print("Each item contains",[Link],"bytes")

Output:
Each item contains 8 bytes.

print("Each item is of the type",[Link])


Output:
Each item is of the type int64
Finding the shape and size of the
array
import numpy as np
a = [Link]([[1,2,3,4,5,6,7]])
print("Array Size:",[Link])
print("Shape:",[Link])

Output:
Array Size: 7
Shape: (1, 7)
Reshaping the array objects
• numpy can reshape the array by changing the number of rows and
columns of the multi-dimensional array.
import numpy as np
Output:
a = [Link]([[1,2],[3,4],[5,6]]) printing the original array..
print("printing the original array..") [ [1 2]
print(a) [3 4]
a=[Link](2,3) [5 6]]
printing the reshaped array..
print("printing the reshaped array..") [[1 2 3]
print(a) [4 5 6]]
Slicing in the Array
• Slicing in the NumPy array is the way to extract a range of elements from an
array.
import numpy as np
a = [Link]([[1,2],[3,4],[5,6]])
print(a[0,1])
print(a[2,0])

Output:
2
5
Linspace
• The linspace() function returns the evenly spaced values over the
given interval.
import numpy as np
a=[Link](5,15,10) #prints 10 values which are evenly spaced over t
he given interval 5-15
print(a)
[ 5. 6.11111111 7.22222222 8.33333333 9.44444444 10.55555556
11.66666667 12.77777778 13.88888889 15. ]
Methods - numpy
• The NumPy provides the max(), min(), and sum() functions which are used to find
the maximum, minimum, and sum of the array elements respectively.
import numpy as np
a = [Link]([1,2,3,10,15,4])
print("The array:",a)
print("The maximum element:",[Link]())
print("The minimum element:",[Link]())
print("The sum of the elements:",[Link]())
Output:
The array: [ 1 2 3 10 15 4] The maximum element: 15 The minimum element: 1 The
sum of the elements: 35
NumPy Array Axis
• A NumPy multi-dimensional array is represented by the axis wher:
• axis-0 represents the columns and
• axis-1 represents the rows.
NumPy Array Axis – Contd.
Example
import numpy as np
a = [Link]([[1,2,30],[10,15,4]])
print("The array:",a)
print("The maximum elements of columns:",[Link](axis = 0))
print("The minimum element of rows",[Link](axis = 1))
print("The sum of all rows",[Link](axis = 1))
Output:
The array: [[1 2 30]
[10 15 4]]
The maximum elements of columns: [10 15 30]
The minimum element of rows [1 4]
The sum of all rows [33 29]
Finding square root and standard
deviation
• The sqrt() and std() functions associated with the numpy array are used to find the square
root and standard deviation of the array elements respectively.
• Standard deviation means how much each element of the array varies from the mean value
of the numpy array.
import numpy as np
a = [Link]([[1,2,30],[10,15,4]])
print([Link](a))
print([Link](a))
Output:
[[1. 1.41421356 5.47722558]
[3.16227766 3.87298335 2. ]]
10.044346115546242
Arithmetic operations on the array
• The numpy module allows us to perform the arithmetic operations on
multi-dimensional arrays directly.
import numpy as np
a = [Link]([[1,2,30],[10,15,4]])
b = [Link]([[1,2,3],[12, 19, 29]])
print("Sum of array a and b\n",a+b)
print("Product of array a and b\n",a*b)
print("Division of array a and b\n",a/b)
Array Concatenation
• The numpy provides us with the vertical stacking and
horizontal stacking which allows us to concatenate two
multi-dimensional arrays vertically or horizontally.
import numpy as np
a = [Link]([[1,2,30],[10,15,4]])
b = [Link]([[1,2,3],[12, 19, 29]])
print("Arrays vertically concatenated\n",[Link]((a,b)));
print("Arrays horizontally concatenated\
n",[Link]((a,b)))
NumPy Datatypes
SN Data type Description
1 bool_ It represents the boolean value indicating true or false. It
is stored as a byte.
2 int_ It is the default type of integer. It is identical to long type
in C that contains 64 bit or 32-bit integer.
3 intc It is similar to the C integer (c int) as it represents 32 or
64-bit int.
4 intp It represents the integers which are used for indexing.
5 int8 It is the 8-bit integer identical to a byte. The range of the
value is -128 to 127.
NumPy Datatypes – Contd.
6 int16 It is the 2-byte (16-bit) integer. The range is -
32768 to 32767.
7 int32 It is the 4-byte (32-bit) integer. The range is -
2147483648 to 2147483647.
8 int64 It is the 8-byte (64-bit) integer. The range is -
9223372036854775808 to
9223372036854775807.

9 uint8 It is the 1-byte (8-bit) unsigned integer.


10 uint16 It is the 2-byte (16-bit) unsigned integer.
NumPy Datatypes – Contd.
11 uint32 It is the 4-byte (32-bit) unsigned integer.
12 uint64 It is the 8 bytes (64-bit) unsigned integer.
13 float_ It is identical to float64.
14 float16 It is the half-precision float. 5 bits are reserved for
the exponent. 10 bits are reserved for mantissa,
and 1 bit is reserved for the sign.

15 float32 It is a single precision float. 8 bits are reserved


for the exponent, 23 bits are reserved for
mantissa, and 1 bit is reserved for the sign.
NumPy Datatypes – Contd.
16 float64 It is the double precision float. 11 bits are
reserved for the exponent, 52 bits are reserved
for mantissa, 1 bit is used for the sign.

17 complex_ It is identical to complex128.


18 complex64 It is used to represent the complex number
where real and imaginary part shares 32 bits
each.
19 complex12 It is used to represent the complex number
8 where real and imaginary part shares 64 bits
each.
NumPy dtype
• All the items of a numpy array are data type objects also known as numpy
dtypes.
• A data type object implements the fixed size of memory corresponding to
an array.
[Link](object, align, copy)

• Object: It represents the object which is to be converted to the data type.


• Align: It can be set to any boolean value. If true, then it adds extra padding
to make it equivalent to a C struct.
• Copy: It creates another copy of the dtype object.
Example
import numpy as np
d = [Link](np.int32)
print(d)
Output:
int32

import numpy as np
d = np.int32(i4)
print(d)
Output:
int32
Creating a Structured data type
• a map-like (dictionary) data type which contains the mapping
between the values
import numpy as np
d=[Link]([('salary',[Link])])
arr = [Link]([(10000.12,),(20000.50,)],dtype=d)
print(arr['salary'])
Output:
[(10000.12,) (20000.5 ,)]
Reference
• [Link]

You might also like