SlideShare a Scribd company logo
1 of 91
INTRODUCTION TO NUMPY
Object Essentials
NumPy provides two fundamental objects:
1. An N-dimensional Array Object (Ndarray)
2. A universal function object (ufunc).
An N-dimensional array: a homogeneous collection of “items” indexed
using N integers.
Two essential pieces of information that define an N-dimensional array:
1. The Shape Of The Array
2. The kind of item the array is composed of
Data-Type Descriptors
dtype(data-type) object: The layout of the array data
array-scalar: Returned when a single-element of the array is accessed
28-09-2022
# DataType Output: str
x = "Hello World"
# DataType Output: int
x = 50
# DataType Output: float
x = 60.5
# DataType Output: complex
x = 3j
# DataType Output: list
x = ["geeks", "for", "geeks"]
# DataType Output: tuple
x = ("geeks", "for", "geeks")
# DataType Output: bytearray
x = bytearray(4)
# DataType Output: memoryview
x = memoryview(bytes(6))
# DataType Output: NoneType
x = None
# DataType Output: range
x = range(10)
# DataType Output: dict
x = {"name": "Suraj", "age": 24}
# DataType Output: set
x = {"geeks", "for", "geeks"}
# DataType Output: frozenset
x = frozenset({"geeks", "for", "geeks"})
# DataType Output: bool
x = True
# DataType Output: bytes
x = b"Geeks"
Create a NumPy ndarray Object
using the array() function
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
Create a NumPy ndarray Object
Use a tuple to create a NumPy array:
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
print(type(arr))
Dimensions in Arrays
0-D Arrays
The elements in array are 0-D arrays, or Scalars.
Example: Create a 0-D array with value 862
import numpy as np
arr = np.array(862)
print(arr)
Dimensions in Arrays
1-D Arrays
An array that has 1-D arrays as its elements is called uni-
dimensional or 1-D array.
Example: Create a 0-D array with value 1,2,3,4,5
import numpy as np
arr = np.array([1,2,3,4,5])
print(arr)
Dimensions in Arrays
2-D Arrays
An array that has 1-D arrays as its
elements is called two-dimensional or 2-
D array.
Example: Create a 2-D array with values
1,2,3 and 4,5,6
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
print(arr)
Dimensions in Arrays
3-D Arrays
An array that has 2-D arrays as its elements is
called three-dimensional or 3-D array.
Example: Create a 3-D array with 2 D array values
1,2,3 and 4,5,6
import numpy as np
arr = np.array([[[1,2,3],[4,5,6]], [[1,2,3],[4,5,6]]])
print(arr)
Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells us how
many dimensions the array have.
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
Higher Dimensional Arrays
An array can have any number of dimensions.
When the array is created, we can define the
number of dimensions by using the ndmin
argument.
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)
Array Indexing
• Get the first element from a 1D array
• Get third and fourth elements from a 1D array and add them.
• To access elements from 2-D arrays we can use comma separated
integers representing the dimension and the index of the element.
• Access the element on the first row, second column
• Access the element on the 2nd row, 5th column
Array Indexing
• To access elements from 3-D arrays we can use comma
separated integers representing the dimensions and the index
of the element.
• Access the third element of the second array of the first array
• Negative Indexing
• Use negative indexing to access an array from the end.
Numpy Datatypes
Data Types in Python
By default Python have these data types:
Strings - used to represent text data
Integer - used to represent integer numbers.
Float - used to represent real numbers.
Boolean - used to represent true or false.
Complex - used to represent complex numbers.
Data Types in NumPy
NumPy has some extra data types, and refer to data types with
one character
i - integer
b - boolean
u - unsigned integer
f - float
c - complex float
m - timedelta
M - datetime
O - object
S - string
U - unicode string
V - fixed chunk of memory for other type ( void )
Checking the Data Type of an Array
The NumPy array object has a property called dtype that
returns the data type of the array:
Example:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr.dtype)
Creating Arrays With a Defined Data Type
Use array() function to create arrays, this can take an optional
argument: dtype to define the expected data type of the array
elements:
Example:
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='S’)
print(arr)
print(arr.dtype)
Converting Data Type on Existing Arrays
The best way to change the data type of an existing array, is to make a
copy of the array with the astype() method.
The astype() function creates a copy of the array, and allows you to
specify the data type as a parameter.
Example:
import numpy as np
arr = np.array([1.1, 2.1, 3.1])
newarr = arr.astype('i’)
print(newarr)
print(newarr.dtype)
NumPy Array Copy vs View
• The main difference between a copy and a view of an array is that
the copy is a new array, and the view is just a view of the original
array.
• The copy owns the data and any changes made to the copy will not
affect original array, and any changes made to the original array will
not affect the copy.
• The view does not own the data and any changes made to the view
will affect the original array, and any changes made to the original
array will affect the view.
NumPy Array Copy vs View
Example:
COPY
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42
print(arr)
print(x)
NumPy Array Copy vs View
Example:
VIEW
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42
print(arr)
print(x)
Array Slicing
• Taking elements from one given index to another given index.
• We pass slice instead of index like this: [start:end].
• We can also define the step, like this: [start:end:step].
• If we don't pass start it’s considered 0
• If we don't pass end it’s considered length of array in that
dimension
• If we don't pass step it’s considered 1
Array Slicing
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
//Slice elements from index 1 to index 5
print(arr[1:5])
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
//Slice elements from index 4 to the end of the array:
print(arr[4:])
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
//Slice elements from the beginning to index 4 (not included):
print(arr[:4])
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
//Return every other element from index 1 to index 5:
print(arr[1:5:2])
//Return every other element from the entire array:
print(arr[::2])
 Taking array and elements from one given index to another given index.
 We pass slice instead of index like this:[array index, start:end].
 We can also define the step, like this: [array index, start:end:step].
 2 D array has two 1D arrays
 array index 0 for first 1D array
 array index 1 for first 1D array
Slicing 2-D Arrays
Slicing 2-D Arrays
Example:
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
//From the second element, slice elements from index 1 to index 4 (not included):
print(arr[1,1:4])
Example:
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
//From both elements, return index 2:
print(arr[0:2,2])
Example:
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
//From both elements, slice index 1 to index 4 (not included):
print(arr[0:2,1:4])
Array Shape
Shape of an Array
The shape of an array is the number of elements in each dimension.
NumPy arrays have an attribute called shape that returns a tuple with
each index having the number of corresponding elements.
Example:
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
Array Reshaping
• Reshaping means changing the shape of an array.
• The shape of an array is the number of elements in each
dimension.
• By reshaping we can add or remove dimensions or change
number of elements in each dimension.
Reshape From 1-D to 2-D
Example:
Convert the following 1-D array with 12 elements into a 2-D array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
//The outermost dimension will have 4 arrays, each with 3 elements:
print(newarr)
Reshape From 1-D to 3-D
Example:
Convert the following 1-D array with 12 elements into a 3-D array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3,2)
//The outermost dimension will have 2 arrays that contain 3 arrays, each with 2 elements:
print(newarr)
Unknown Dimension
 You are allowed to have one "unknown" dimension.
 Do not specify an exact number for one of the dimensions in
the reshape method.
 Pass -1 as the value, and NumPy will calculate this number for
you.
Unknown Dimension
Example:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(2, 2, -1)
print(newarr)
//Convert 1D array with 8 elements to 3D array with 2x2 elements:
Flattening the arrays
Means converting a multidimensional array into a 1D array.
Use reshape(-1) to do this.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr) newarr = arr.reshape(-1)
//Convert the array into a 1D array:
print(newarr)
Array Iterating
Iterating Arrays
• Iterating means going through elements one by one.
• As we deal with multi-dimensional arrays in numpy, we can do this
using basic for loop of python.
• If we iterate on a 1-D array it will go through each element one by one.
Example:
import numpy as np
arr = np.array([1, 2, 3])
for x in arr:
print(x)
Iterating 2-D Arrays
In a 2-D array it will go through all the rows.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
print(x)
If we iterate on a n-D array it will go through
n-1th dimension one by one.
Iterating 2-D Arrays
To return the actual values, the scalars, we have to iterate the
arrays in each dimension.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
for y in x:
print(y)
Iterating 3-D Arrays
In a 3-D array it will go through all 2D arrays.
Example:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in arr:
print(x)
[[1 2 3]
[4 5 6]]
[[ 7 8 9]
[10 11 12]]
Iterating 3-D Arrays
In a 3-D array it will go through all 2D arrays.
Example:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in arr:
for y in x:
for z in y:
print(z)
1
2
3
4
5
6
7
8
9
10
11
12
Iterating Arrays Using nditer()
Example 1:
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for x in np.nditer(arr):
print(x)
Iterating With Different Step Size
Iterate through every scalar element of the
2D array skipping 1 element:
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for x in np.nditer(arr[:, ::2]):
print(x)
Joining Array
Joining NumPy Arrays
 Joining means putting contents of two or more arrays in a
single array.
 In SQL we join tables based on a key, whereas in NumPy
we join arrays by axes.
 We pass a sequence of arrays that we want to join to the
concatenate() function, along with the axis, If axis is not
explicitly passed, it is taken as 0.
Joining NumPy Arrays
Example: Join two arrays
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)
[1 2 3 4 5 6]
Joining NumPy Arrays
Example: Join two 2-D arrays:
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2))
print(arr)
Joining NumPy Arrays
Example: Join two 2-D arrays along rows (axis=1):
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)
Joining NumPy Arrays
Example: Join two 3-D arrays:
import numpy as np
arr1 = np.array([[[1, 2], [3, 4]],[[1, 2], [3, 4]]])
arr2 = np.array([[[5, 6], [7, 8]],[[5, 6], [7, 8]]])
arr = np.concatenate((arr1, arr2),axis=0)
print(arr)
Joining NumPy Arrays
Example: Join two 3-D arrays:
import numpy as np
arr1 = np.array([[[1, 2], [3, 4]],[[1, 2], [3, 4]]])
arr2 = np.array([[[5, 6], [7, 8]],[[5, 6], [7, 8]]])
arr = np.concatenate((arr1, arr2),axis=1)
print(arr)
Joining NumPy Arrays
Example: Join two 3-D arrays:
import numpy as np
arr1 = np.array([[[1, 2], [3, 4]],[[1, 2], [3, 4]]])
arr2 = np.array([[[5, 6], [7, 8]],[[5, 6], [7, 8]]])
arr = np.concatenate((arr1, arr2),axis=2)
print(arr)
Joining Arrays Using Stack Functions
Stacking is same as concatenation, the only difference is that
stacking is done along a new axis.
We can concatenate two 1-D arrays along the second axis which
would result in putting them one over the other, i.e,. stacking.
We pass a sequence of arrays that we want to join to the stack()
method along with the axis. If axis is not explicitly passed it is taken
as 0 (default).
Joining Arrays Using Stack Functions
Stack Two 1D arrays:
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)
Joining Arrays Using Stack Functions
Stacking Along Rows:
NumPy provides a helper function: hstack() to stack along rows.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2))
print(arr)
Joining Arrays Using Stack Functions
Stacking Along Colum:
NumPy provides a helper function: vstack() to stack along coloum.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)
Joining Arrays Using Stack Functions
Stacking Along Height (depth)
NumPy provides a helper function: dstack() to stack along height,
which is the same as depth.
import numpy as np
arr1 = np.array([[1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.dstack((arr1, arr2))
print(arr)
Universal Functions
 ufuncs are used to implement vectorization in NumPy which is way faster than
iterating over elements.
 They also provide broadcasting and additional methods like reduce, accumulate
etc. that are very helpful for computation.
 ufuncs also take additional arguments, like:
 where boolean array or condition defining where the operations should take
place.
 dtype defining the return type of elements.
 out output array where the return value should be copied.
Vectorization
Converting iterative statements into a vector based operation is called
vectorization.
It is faster as modern CPUs are optimized for such operations.
Add the Elements of Two Lists
list 1: [1, 2, 3, 4]
list 2: [4, 5, 6, 7]
One way of doing it is to iterate over both of the lists and then sum each
elements.
Example
Without ufunc, we can use Python's built-in zip() method:
x = [1, 2, 3, 4]
y = [4, 5, 6, 7,9]
z = []
for i, j in zip(x, y):
z.append(i + j)
print(z)
OUTPUT
[5, 7, 9, 11]
NumPy has a ufunc for this, called add(x, y) that will produce the same
result.
Example
With ufunc, we can use the add() function:
import numpy as np
x = [1, 2, 3, 4]
y = [4, 5, 6, 7]
z = np.add(x, y)
print(z)
OUTPUT
[5, 7, 9, 11]
Create Your Own ufun
1. define a function like normal python program
2. add it to your NumPy ufunc library with the frompyfunc()
method
The frompyfunc() method takes the following arguments:
1. Function name - the name of the function we defined.
2. inputs - the number of input arguments (arrays) to be
passed.
3. outputs - the number of output arrays returned.
import numpy as np
def myadd(x, y):
return x+y
myadd = np.frompyfunc(myadd, 2, 1)
print(myadd([1, 2, 3, 4], [5, 6, 7, 8]))
Output
[6 8 10 12]
import numpy as np
def myadd(x, y):
return x+y+1
myadd = np.frompyfunc(myadd, 2, 1)
print(myadd([1, 2, 3, 4], [5, 6, 7, 8]))
OUTPUT
[7 9 11 13]
Simple Arithmetic
Addition:
The add() function sums the content of two arrays, and return the results in a
new array.
Example:
import numpy as np
arr1 = np.array([10, 11, 12, 13, 14, 15])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.add(arr1, arr2)
print(newarr)
OUTPUT
[30 32 34 36 38 40]
Subtraction:
The subtract() function subtracts the values from one array with the
values from another array, and return the results in a new array.
Example:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.subtract(arr1, arr2)
print(newarr)
[-10 -1 8 17 26 35]
Multiplication:
The multiply() function multiplies the values from one array with
the values from another array, and return the results in a new array.
Example:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.multiply(arr1, arr2)
print(newarr)
[ 200 420 660 920 1200 1500]
Division:
The divide() function divides the values from one array with the
values from another array, and return the results in a new array.
Example:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 10, 8, 2, 33])
newarr = np. divide(arr1, arr2)
print(newarr)
[ 3.33333333 4. 3. 5. 25. 1.81818182]
Simple Arithmetic…
Power: power()
Remainder: remainder() or mod()
Quotient or mod : divmod()
Absolute values: absolute() or abs()
Rounding Decimals
There are primarily five ways of rounding off
decimals in NumPy:
* truncation
* fix
* rounding
* floor
* ceil
Rounding Decimals
Truncation:
Remove the decimals, and return the float number closest to zero. Use
the trunc() and fix() functions.
Eg:
import numpy as np
arr = np.trunc([-3.1666, 3.6667])
print(arr)
Eg:
import numpy as np
arr = np.fix([-3.1666, 3.6667])
print(arr)
[-3. 3.]
[-3. 3.]
Rounding
The around() function increments preceding digit or decimal by 1 if >=5
else do nothing.
Eg:
import numpy as np
arr = np.around([3.1666, 2])
print(arr)
import numpy as np
arr = np.around([3.1666, 2.6, 1.6, 4.3])
print(arr)
[3. 2.]
[3. 3. 2. 4.]
Floor:
The floor() function rounds off decimal to nearest lower integer.
Eg:
import numpy as np
arr = np. floor([-3.1666, 3.6667])
print(arr)
[-4. 3.]
Ceil:
The ceil() function rounds off decimal to nearest upper integer.
Eg:
import numpy as np
arr = np. ceil([-3.1666, 3.6667])
print(arr)
[-3. 4.]
Splitting Array
Splitting Array
• Splitting is reverse operation of Joining.
• Joining merges multiple arrays into one and Splitting breaks one
array into multiple.
• We use array_split() and split() for splitting arrays, we pass it the
array we want to split and the number of splits.
Splitting Array
Example://split a 1D array to 3 parts
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.split(arr, 3)
print(newarr)
[array([1, 2]), array([3, 4]), array([5, 6])]
Example://split a 1D array to 3 parts
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.split(arr, 4)
print(newarr)
Splitting Array
Example://split a 1D array to 3 parts
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np. array_split (arr, 4)
print(newarr)
[array([1, 2]), array([3, 4]), array([5]), array([6])]
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr[0])
print(newarr[1])
print(newarr[2])
[1 2]
[3 4]
[5 6]
Splitting 2-D Arrays
Use the same syntax when splitting 2-D arrays.
Use the array_split() method, pass in the array you want to split
and the number of splits you want to do.
Example:
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
newarr = np.array_split(arr, 3)
print(newarr)
[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]]), array([[ 9, 10],
[11, 12]])]
Splitting 2-D Arrays
In addition, you can specify which axis you want to do the split
around.
The example below also returns three 2-D arrays, but they are
split along the row (axis=1).
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12],
[13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3, axis=1)
print(newarr)
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9],
[10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3, axis=0)
print(newarr)
[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]]), array([[13, 14, 15],
[16, 17, 18]])]
Splitting 2-D Arrays
Use the hsplit() method to split the 2-D array into three 2-D
arrays along rows.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12],
[13, 14, 15], [16, 17, 18]])
newarr = np.hsplit(arr, 3)
print(newarr)
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.vsplit(arr, 3)
print(newarr)
[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]]), array([[13, 14, 15],
[16, 17, 18]])]
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5
, 6]], [[7, 8, 9], [10, 11, 12]],
[[13, 14, 15], [16, 17, 18]]])
np.dsplit(arr,[2,3])
[array([[[ 1, 2],
[ 4, 5]],
[[ 7, 8],
[10, 11]],
[[13, 14],
[16, 17]]]),
array([[[ 3],
[ 6]],
[[ 9],
[12]],
[[15],
[18]]]),
array([], shape=(3, 2, 0), dtype=int64)]
NUMPY-2.pptx
NUMPY-2.pptx

More Related Content

What's hot (20)

NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Data structure
Data structureData structure
Data structure
 
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | Edureka
 
Sets in python
Sets in pythonSets in python
Sets in python
 
NUMPY
NUMPY NUMPY
NUMPY
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Sorting
SortingSorting
Sorting
 
Linked list
Linked listLinked list
Linked list
 
Python Collections
Python CollectionsPython Collections
Python Collections
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 

Similar to NUMPY-2.pptx

NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptxcoolmanbalu123
 
arraycreation.pptx
arraycreation.pptxarraycreation.pptx
arraycreation.pptxsathya930629
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfSmrati Kumar Katiyar
 
CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.pptkdr52121
 
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...DineshThallapelly
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
Numpy_defintion_description_usage_examples.pptx
Numpy_defintion_description_usage_examples.pptxNumpy_defintion_description_usage_examples.pptx
Numpy_defintion_description_usage_examples.pptxVGaneshKarthikeyan
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxDarellMuchoko
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptxMrMaster11
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxprakashvs7
 
Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docxmanohar25689
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...HendraPurnama31
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
Data Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with NData Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with NOllieShoresna
 
python-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxpython-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxAkashgupta517936
 

Similar to NUMPY-2.pptx (20)

NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
 
arraycreation.pptx
arraycreation.pptxarraycreation.pptx
arraycreation.pptx
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdf
 
CAP776Numpy (2).ppt
CAP776Numpy (2).pptCAP776Numpy (2).ppt
CAP776Numpy (2).ppt
 
CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.ppt
 
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
 
CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
Numpy - Array.pdf
Numpy - Array.pdfNumpy - Array.pdf
Numpy - Array.pdf
 
Numpy_defintion_description_usage_examples.pptx
Numpy_defintion_description_usage_examples.pptxNumpy_defintion_description_usage_examples.pptx
Numpy_defintion_description_usage_examples.pptx
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
PPS-UNIT5.ppt
PPS-UNIT5.pptPPS-UNIT5.ppt
PPS-UNIT5.ppt
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
 
Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docx
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Data Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with NData Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with N
 
python-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxpython-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptx
 

Recently uploaded

办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...Boston Institute of Analytics
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxStephen266013
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationshipsccctableauusergroup
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 

Recently uploaded (20)

办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
B2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docxB2 Creative Industry Response Evaluation.docx
B2 Creative Industry Response Evaluation.docx
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships04242024_CCC TUG_Joins and Relationships
04242024_CCC TUG_Joins and Relationships
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 

NUMPY-2.pptx

  • 2. Object Essentials NumPy provides two fundamental objects: 1. An N-dimensional Array Object (Ndarray) 2. A universal function object (ufunc). An N-dimensional array: a homogeneous collection of “items” indexed using N integers. Two essential pieces of information that define an N-dimensional array: 1. The Shape Of The Array 2. The kind of item the array is composed of
  • 3. Data-Type Descriptors dtype(data-type) object: The layout of the array data array-scalar: Returned when a single-element of the array is accessed 28-09-2022
  • 4.
  • 5. # DataType Output: str x = "Hello World" # DataType Output: int x = 50 # DataType Output: float x = 60.5 # DataType Output: complex x = 3j # DataType Output: list x = ["geeks", "for", "geeks"] # DataType Output: tuple x = ("geeks", "for", "geeks") # DataType Output: bytearray x = bytearray(4) # DataType Output: memoryview x = memoryview(bytes(6)) # DataType Output: NoneType x = None # DataType Output: range x = range(10) # DataType Output: dict x = {"name": "Suraj", "age": 24} # DataType Output: set x = {"geeks", "for", "geeks"} # DataType Output: frozenset x = frozenset({"geeks", "for", "geeks"}) # DataType Output: bool x = True # DataType Output: bytes x = b"Geeks"
  • 6. Create a NumPy ndarray Object using the array() function import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type(arr))
  • 7. Create a NumPy ndarray Object Use a tuple to create a NumPy array: import numpy as np arr = np.array((1, 2, 3, 4, 5)) print(arr) print(type(arr))
  • 8. Dimensions in Arrays 0-D Arrays The elements in array are 0-D arrays, or Scalars. Example: Create a 0-D array with value 862 import numpy as np arr = np.array(862) print(arr)
  • 9. Dimensions in Arrays 1-D Arrays An array that has 1-D arrays as its elements is called uni- dimensional or 1-D array. Example: Create a 0-D array with value 1,2,3,4,5 import numpy as np arr = np.array([1,2,3,4,5]) print(arr)
  • 10. Dimensions in Arrays 2-D Arrays An array that has 1-D arrays as its elements is called two-dimensional or 2- D array. Example: Create a 2-D array with values 1,2,3 and 4,5,6 import numpy as np arr = np.array([[1,2,3],[4,5,6]]) print(arr)
  • 11. Dimensions in Arrays 3-D Arrays An array that has 2-D arrays as its elements is called three-dimensional or 3-D array. Example: Create a 3-D array with 2 D array values 1,2,3 and 4,5,6 import numpy as np arr = np.array([[[1,2,3],[4,5,6]], [[1,2,3],[4,5,6]]]) print(arr)
  • 12. Check Number of Dimensions? NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have. import numpy as np a = np.array(42) b = np.array([1, 2, 3, 4, 5]) c = np.array([[1, 2, 3], [4, 5, 6]]) d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(a.ndim) print(b.ndim) print(c.ndim) print(d.ndim)
  • 13. Higher Dimensional Arrays An array can have any number of dimensions. When the array is created, we can define the number of dimensions by using the ndmin argument. import numpy as np arr = np.array([1, 2, 3, 4], ndmin=5) print(arr) print('number of dimensions :', arr.ndim)
  • 14. Array Indexing • Get the first element from a 1D array • Get third and fourth elements from a 1D array and add them. • To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element. • Access the element on the first row, second column • Access the element on the 2nd row, 5th column
  • 15. Array Indexing • To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element. • Access the third element of the second array of the first array • Negative Indexing • Use negative indexing to access an array from the end.
  • 17. Data Types in Python By default Python have these data types: Strings - used to represent text data Integer - used to represent integer numbers. Float - used to represent real numbers. Boolean - used to represent true or false. Complex - used to represent complex numbers.
  • 18. Data Types in NumPy NumPy has some extra data types, and refer to data types with one character i - integer b - boolean u - unsigned integer f - float c - complex float m - timedelta M - datetime O - object S - string U - unicode string V - fixed chunk of memory for other type ( void )
  • 19. Checking the Data Type of an Array The NumPy array object has a property called dtype that returns the data type of the array: Example: import numpy as np arr = np.array([1, 2, 3, 4]) print(arr.dtype)
  • 20. Creating Arrays With a Defined Data Type Use array() function to create arrays, this can take an optional argument: dtype to define the expected data type of the array elements: Example: import numpy as np arr = np.array([1, 2, 3, 4], dtype='S’) print(arr) print(arr.dtype)
  • 21. Converting Data Type on Existing Arrays The best way to change the data type of an existing array, is to make a copy of the array with the astype() method. The astype() function creates a copy of the array, and allows you to specify the data type as a parameter. Example: import numpy as np arr = np.array([1.1, 2.1, 3.1]) newarr = arr.astype('i’) print(newarr) print(newarr.dtype)
  • 22. NumPy Array Copy vs View • The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array. • The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy. • The view does not own the data and any changes made to the view will affect the original array, and any changes made to the original array will affect the view.
  • 23. NumPy Array Copy vs View Example: COPY import numpy as np arr = np.array([1, 2, 3, 4, 5]) x = arr.copy() arr[0] = 42 print(arr) print(x)
  • 24. NumPy Array Copy vs View Example: VIEW import numpy as np arr = np.array([1, 2, 3, 4, 5]) x = arr.view() arr[0] = 42 print(arr) print(x)
  • 25. Array Slicing • Taking elements from one given index to another given index. • We pass slice instead of index like this: [start:end]. • We can also define the step, like this: [start:end:step]. • If we don't pass start it’s considered 0 • If we don't pass end it’s considered length of array in that dimension • If we don't pass step it’s considered 1
  • 26. Array Slicing Example: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) //Slice elements from index 1 to index 5 print(arr[1:5]) Example: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) //Slice elements from index 4 to the end of the array: print(arr[4:])
  • 27. Example: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) //Slice elements from the beginning to index 4 (not included): print(arr[:4]) Example: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[-3:-1])
  • 28. Example: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) //Return every other element from index 1 to index 5: print(arr[1:5:2]) //Return every other element from the entire array: print(arr[::2])
  • 29.  Taking array and elements from one given index to another given index.  We pass slice instead of index like this:[array index, start:end].  We can also define the step, like this: [array index, start:end:step].  2 D array has two 1D arrays  array index 0 for first 1D array  array index 1 for first 1D array Slicing 2-D Arrays
  • 30. Slicing 2-D Arrays Example: import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) //From the second element, slice elements from index 1 to index 4 (not included): print(arr[1,1:4]) Example: import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) //From both elements, return index 2: print(arr[0:2,2])
  • 31. Example: import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) //From both elements, slice index 1 to index 4 (not included): print(arr[0:2,1:4])
  • 32. Array Shape Shape of an Array The shape of an array is the number of elements in each dimension. NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements. Example: import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(arr.shape)
  • 33. Array Reshaping • Reshaping means changing the shape of an array. • The shape of an array is the number of elements in each dimension. • By reshaping we can add or remove dimensions or change number of elements in each dimension.
  • 34. Reshape From 1-D to 2-D Example: Convert the following 1-D array with 12 elements into a 2-D array. import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.reshape(4, 3) //The outermost dimension will have 4 arrays, each with 3 elements: print(newarr)
  • 35. Reshape From 1-D to 3-D Example: Convert the following 1-D array with 12 elements into a 3-D array. import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.reshape(2, 3,2) //The outermost dimension will have 2 arrays that contain 3 arrays, each with 2 elements: print(newarr)
  • 36. Unknown Dimension  You are allowed to have one "unknown" dimension.  Do not specify an exact number for one of the dimensions in the reshape method.  Pass -1 as the value, and NumPy will calculate this number for you.
  • 37. Unknown Dimension Example: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) newarr = arr.reshape(2, 2, -1) print(newarr) //Convert 1D array with 8 elements to 3D array with 2x2 elements:
  • 38. Flattening the arrays Means converting a multidimensional array into a 1D array. Use reshape(-1) to do this. Example: import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) newarr = arr.reshape(-1) //Convert the array into a 1D array: print(newarr)
  • 40. Iterating Arrays • Iterating means going through elements one by one. • As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python. • If we iterate on a 1-D array it will go through each element one by one. Example: import numpy as np arr = np.array([1, 2, 3]) for x in arr: print(x)
  • 41. Iterating 2-D Arrays In a 2-D array it will go through all the rows. Example: import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) for x in arr: print(x) If we iterate on a n-D array it will go through n-1th dimension one by one.
  • 42. Iterating 2-D Arrays To return the actual values, the scalars, we have to iterate the arrays in each dimension. Example: import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) for x in arr: for y in x: print(y)
  • 43. Iterating 3-D Arrays In a 3-D array it will go through all 2D arrays. Example: import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: print(x) [[1 2 3] [4 5 6]] [[ 7 8 9] [10 11 12]]
  • 44. Iterating 3-D Arrays In a 3-D array it will go through all 2D arrays. Example: import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: for y in x: for z in y: print(z) 1 2 3 4 5 6 7 8 9 10 11 12
  • 45. Iterating Arrays Using nditer() Example 1: import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) for x in np.nditer(arr): print(x)
  • 46. Iterating With Different Step Size Iterate through every scalar element of the 2D array skipping 1 element: import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) for x in np.nditer(arr[:, ::2]): print(x)
  • 48. Joining NumPy Arrays  Joining means putting contents of two or more arrays in a single array.  In SQL we join tables based on a key, whereas in NumPy we join arrays by axes.  We pass a sequence of arrays that we want to join to the concatenate() function, along with the axis, If axis is not explicitly passed, it is taken as 0.
  • 49. Joining NumPy Arrays Example: Join two arrays import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.concatenate((arr1, arr2)) print(arr) [1 2 3 4 5 6]
  • 50. Joining NumPy Arrays Example: Join two 2-D arrays: import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) arr = np.concatenate((arr1, arr2)) print(arr)
  • 51. Joining NumPy Arrays Example: Join two 2-D arrays along rows (axis=1): import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) arr = np.concatenate((arr1, arr2), axis=1) print(arr)
  • 52. Joining NumPy Arrays Example: Join two 3-D arrays: import numpy as np arr1 = np.array([[[1, 2], [3, 4]],[[1, 2], [3, 4]]]) arr2 = np.array([[[5, 6], [7, 8]],[[5, 6], [7, 8]]]) arr = np.concatenate((arr1, arr2),axis=0) print(arr)
  • 53. Joining NumPy Arrays Example: Join two 3-D arrays: import numpy as np arr1 = np.array([[[1, 2], [3, 4]],[[1, 2], [3, 4]]]) arr2 = np.array([[[5, 6], [7, 8]],[[5, 6], [7, 8]]]) arr = np.concatenate((arr1, arr2),axis=1) print(arr)
  • 54. Joining NumPy Arrays Example: Join two 3-D arrays: import numpy as np arr1 = np.array([[[1, 2], [3, 4]],[[1, 2], [3, 4]]]) arr2 = np.array([[[5, 6], [7, 8]],[[5, 6], [7, 8]]]) arr = np.concatenate((arr1, arr2),axis=2) print(arr)
  • 55. Joining Arrays Using Stack Functions Stacking is same as concatenation, the only difference is that stacking is done along a new axis. We can concatenate two 1-D arrays along the second axis which would result in putting them one over the other, i.e,. stacking. We pass a sequence of arrays that we want to join to the stack() method along with the axis. If axis is not explicitly passed it is taken as 0 (default).
  • 56. Joining Arrays Using Stack Functions Stack Two 1D arrays: 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)
  • 57. Joining Arrays Using Stack Functions Stacking Along Rows: NumPy provides a helper function: hstack() to stack along rows. import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.hstack((arr1, arr2)) print(arr)
  • 58. Joining Arrays Using Stack Functions Stacking Along Colum: NumPy provides a helper function: vstack() to stack along coloum. import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.vstack((arr1, arr2)) print(arr)
  • 59. Joining Arrays Using Stack Functions Stacking Along Height (depth) NumPy provides a helper function: dstack() to stack along height, which is the same as depth. import numpy as np arr1 = np.array([[1, 2, 3]) arr2 = np.array([4, 5, 6]) arr = np.dstack((arr1, arr2)) print(arr)
  • 61.  ufuncs are used to implement vectorization in NumPy which is way faster than iterating over elements.  They also provide broadcasting and additional methods like reduce, accumulate etc. that are very helpful for computation.  ufuncs also take additional arguments, like:  where boolean array or condition defining where the operations should take place.  dtype defining the return type of elements.  out output array where the return value should be copied.
  • 62. Vectorization Converting iterative statements into a vector based operation is called vectorization. It is faster as modern CPUs are optimized for such operations. Add the Elements of Two Lists list 1: [1, 2, 3, 4] list 2: [4, 5, 6, 7] One way of doing it is to iterate over both of the lists and then sum each elements.
  • 63. Example Without ufunc, we can use Python's built-in zip() method: x = [1, 2, 3, 4] y = [4, 5, 6, 7,9] z = [] for i, j in zip(x, y): z.append(i + j) print(z) OUTPUT [5, 7, 9, 11]
  • 64. NumPy has a ufunc for this, called add(x, y) that will produce the same result. Example With ufunc, we can use the add() function: import numpy as np x = [1, 2, 3, 4] y = [4, 5, 6, 7] z = np.add(x, y) print(z) OUTPUT [5, 7, 9, 11]
  • 65. Create Your Own ufun 1. define a function like normal python program 2. add it to your NumPy ufunc library with the frompyfunc() method The frompyfunc() method takes the following arguments: 1. Function name - the name of the function we defined. 2. inputs - the number of input arguments (arrays) to be passed. 3. outputs - the number of output arrays returned.
  • 66. import numpy as np def myadd(x, y): return x+y myadd = np.frompyfunc(myadd, 2, 1) print(myadd([1, 2, 3, 4], [5, 6, 7, 8])) Output [6 8 10 12]
  • 67. import numpy as np def myadd(x, y): return x+y+1 myadd = np.frompyfunc(myadd, 2, 1) print(myadd([1, 2, 3, 4], [5, 6, 7, 8])) OUTPUT [7 9 11 13]
  • 68. Simple Arithmetic Addition: The add() function sums the content of two arrays, and return the results in a new array. Example: import numpy as np arr1 = np.array([10, 11, 12, 13, 14, 15]) arr2 = np.array([20, 21, 22, 23, 24, 25]) newarr = np.add(arr1, arr2) print(newarr) OUTPUT [30 32 34 36 38 40]
  • 69. Subtraction: The subtract() function subtracts the values from one array with the values from another array, and return the results in a new array. Example: import numpy as np arr1 = np.array([10, 20, 30, 40, 50, 60]) arr2 = np.array([20, 21, 22, 23, 24, 25]) newarr = np.subtract(arr1, arr2) print(newarr) [-10 -1 8 17 26 35]
  • 70. Multiplication: The multiply() function multiplies the values from one array with the values from another array, and return the results in a new array. Example: import numpy as np arr1 = np.array([10, 20, 30, 40, 50, 60]) arr2 = np.array([20, 21, 22, 23, 24, 25]) newarr = np.multiply(arr1, arr2) print(newarr) [ 200 420 660 920 1200 1500]
  • 71. Division: The divide() function divides the values from one array with the values from another array, and return the results in a new array. Example: import numpy as np arr1 = np.array([10, 20, 30, 40, 50, 60]) arr2 = np.array([3, 5, 10, 8, 2, 33]) newarr = np. divide(arr1, arr2) print(newarr) [ 3.33333333 4. 3. 5. 25. 1.81818182]
  • 72. Simple Arithmetic… Power: power() Remainder: remainder() or mod() Quotient or mod : divmod() Absolute values: absolute() or abs()
  • 73. Rounding Decimals There are primarily five ways of rounding off decimals in NumPy: * truncation * fix * rounding * floor * ceil
  • 74. Rounding Decimals Truncation: Remove the decimals, and return the float number closest to zero. Use the trunc() and fix() functions. Eg: import numpy as np arr = np.trunc([-3.1666, 3.6667]) print(arr) Eg: import numpy as np arr = np.fix([-3.1666, 3.6667]) print(arr) [-3. 3.] [-3. 3.]
  • 75. Rounding The around() function increments preceding digit or decimal by 1 if >=5 else do nothing. Eg: import numpy as np arr = np.around([3.1666, 2]) print(arr) import numpy as np arr = np.around([3.1666, 2.6, 1.6, 4.3]) print(arr) [3. 2.] [3. 3. 2. 4.]
  • 76. Floor: The floor() function rounds off decimal to nearest lower integer. Eg: import numpy as np arr = np. floor([-3.1666, 3.6667]) print(arr) [-4. 3.]
  • 77. Ceil: The ceil() function rounds off decimal to nearest upper integer. Eg: import numpy as np arr = np. ceil([-3.1666, 3.6667]) print(arr) [-3. 4.]
  • 79. Splitting Array • Splitting is reverse operation of Joining. • Joining merges multiple arrays into one and Splitting breaks one array into multiple. • We use array_split() and split() for splitting arrays, we pass it the array we want to split and the number of splits.
  • 80. Splitting Array Example://split a 1D array to 3 parts import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) newarr = np.split(arr, 3) print(newarr) [array([1, 2]), array([3, 4]), array([5, 6])]
  • 81. Example://split a 1D array to 3 parts import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) newarr = np.split(arr, 4) print(newarr)
  • 82. Splitting Array Example://split a 1D array to 3 parts import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) newarr = np. array_split (arr, 4) print(newarr) [array([1, 2]), array([3, 4]), array([5]), array([6])]
  • 83. import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) newarr = np.array_split(arr, 3) print(newarr[0]) print(newarr[1]) print(newarr[2]) [1 2] [3 4] [5 6]
  • 84. Splitting 2-D Arrays Use the same syntax when splitting 2-D arrays. Use the array_split() method, pass in the array you want to split and the number of splits you want to do. Example: import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]) newarr = np.array_split(arr, 3) print(newarr) [array([[1, 2], [3, 4]]), array([[5, 6], [7, 8]]), array([[ 9, 10], [11, 12]])]
  • 85. Splitting 2-D Arrays In addition, you can specify which axis you want to do the split around. The example below also returns three 2-D arrays, but they are split along the row (axis=1). Example: import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]) newarr = np.array_split(arr, 3, axis=1) print(newarr) [array([[ 1], [ 4], [ 7], [10], [13], [16]]), array([[ 2], [ 5], [ 8], [11], [14], [17]]), array([[ 3], [ 6], [ 9], [12], [15], [18]])]
  • 86. import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]) newarr = np.array_split(arr, 3, axis=0) print(newarr) [array([[1, 2, 3], [4, 5, 6]]), array([[ 7, 8, 9], [10, 11, 12]]), array([[13, 14, 15], [16, 17, 18]])]
  • 87. Splitting 2-D Arrays Use the hsplit() method to split the 2-D array into three 2-D arrays along rows. Example: import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]) newarr = np.hsplit(arr, 3) print(newarr) [array([[ 1], [ 4], [ 7], [10], [13], [16]]), array([[ 2], [ 5], [ 8], [11], [14], [17]]), array([[ 3], [ 6], [ 9], [12], [15], [18]])]
  • 88. import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]]) newarr = np.vsplit(arr, 3) print(newarr) [array([[1, 2, 3], [4, 5, 6]]), array([[ 7, 8, 9], [10, 11, 12]]), array([[13, 14, 15], [16, 17, 18]])]
  • 89. import numpy as np arr = np.array([[[1, 2, 3], [4, 5 , 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]) np.dsplit(arr,[2,3]) [array([[[ 1, 2], [ 4, 5]], [[ 7, 8], [10, 11]], [[13, 14], [16, 17]]]), array([[[ 3], [ 6]], [[ 9], [12]], [[15], [18]]]), array([], shape=(3, 2, 0), dtype=int64)]