Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
NumPy in Python
Dr.G.Jasmine Beulah
Assistant Professor, Dept Computer Science,
Kristu Jayanti College, Bengaluru.
• NumPy (Numerical Python) is an open source Python library that’s
used in almost every field of science and engineering.
• It’s the universal standard for working with numerical data in Python,
and it’s at the core of the scientific Python and PyData ecosystems.
• The NumPy library contains multidimensional array and matrix data
structures.
• It provides ndarray, a homogeneous n-dimensional array object, with
methods to efficiently operate on it. NumPy can be used to perform a
wide variety of mathematical operations on arrays.
• It adds powerful data structures to Python that guarantee efficient
calculations with arrays and matrices and it supplies an enormous
library of high-level mathematical functions that operate on these
arrays and matrices.
How to import NumPy
• To access NumPy and its functions import it in your Python code like
this:
• import numpy as np
What’s the difference between a Python list and a
NumPy array?
• NumPy gives you an enormous range of fast and efficient ways of
creating arrays and manipulating numerical data inside them.
• While a Python list can contain different data types within a single list,
all of the elements in a NumPy array should be homogeneous.
• The mathematical operations that are meant to be performed on
arrays would be extremely inefficient if the arrays weren’t
homogeneous.
Why use NumPy?
• NumPy arrays are faster and more compact than Python lists.
• An array consumes less memory and is convenient to use.
• NumPy uses much less memory to store data and it provides a
mechanism of specifying the data types.
• This allows the code to be optimized even further.
What is an array?
• An array is a central data structure of the NumPy library.
• An array is a grid of values and it contains information about the raw
data, how to locate an element, and how to interpret an element.
• It has a grid of elements that can be indexed in various ways.
• The elements are all of the same type, referred to as the array dtype.
• An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers.
• The rank of the array is the number of dimensions.
• The shape of the array is a tuple of integers giving the size of the array along each dimension.
• One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional
data.
For example:
a = np.array([1, 2, 3, 4, 5, 6])
or:
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
• We can access the elements in the array using square brackets. When
you’re accessing elements, remember that indexing in NumPy starts
at 0. That means that if you want to access the first element in your
array, you’ll be accessing element “0”.
print(a[0])
[1 2 3 4]
How to create a basic array
• To create a NumPy array, you can use the function np.array().
import numpy as np
a = np.array([1, 2, 3])
create an array filled with 0’s:
np.zeros(2)
array([0., 0.])
An array filled with 1’s:
np.ones(2)
array([1., 1.])
Create a NumPy ndarray Object
• NumPy is used to work with arrays. The array object in NumPy is called
ndarray.
• We can create a NumPy ndarray object by using the array() function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
o/p:arr is numpy.ndarray type
Use a tuple to create a NumPy array:
• To create an ndarray, we can pass a list, tuple or any array-like object
into the array() method, and it will be converted into an ndarray:
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
o/p: [1 2 3 4 5]
Dimensions in Arrays
• A dimension in arrays is one level of array depth (nested arrays).
• nested array: are arrays that have arrays as their elements.
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Example
Create a 0-D array with value 42
import numpy as np
arr = np.array(42)
print(arr)
1-D Arrays
• An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
• These are the most common and basic arrays.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
2-D Arrays
• An array that has 1-D arrays as its elements is called a 2-D array.
• These are often used to represent matrix or 2nd order tensors.
• NumPy has a whole sub module dedicated towards matrix operations called numpy.mat
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
3-D arrays
• n array that has 2-D arrays (matrices) as its elements is called 3-D array.
• These are often used to represent a 3rd order tensor.
• Create a 3-D array with two 2-D arrays, both containing two arrays with the
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)
o/p: [[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many
dimensions the array have.
Example
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)
• Creating an array from a sequence of elements, you can easily create an array
filled with 0’s:
np.zeros(2)
An array filled with 1’s:
np.ones(2)
# Create an empty array with 2 elements
np.empty(2)
#create an array with a range of elements:
np.arange(4)
#an array that contains a range of evenly spaced intervals. To do this, you will
specify the first number, last number, and the step size.
np.arange(2, 9, 2)
Use the Numpy Linspace Function
• The NumPy linspace function (sometimes called np.linspace) is a tool
in Python for creating numeric sequences.
np.linspace(start = 0, stop = 100, num = 5)
NumPy.pptx
Syntax:
np.linspace(0, 10, num=5)
NumPy.pptx
Specifying your data type
While the default data type is floating point (np.float64), you
can explicitly specify which data type you want using the dtype
keyword.
Adding, removing, and sorting elements
arr = np.array([2, 1, 5, 3, 7, 4, 6, 8])
np.sort(arr)
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
#concatenate with np.concatenate()
np.concatenate((a, b))
o/p:
array([1, 2, 3, 4, 5, 6, 7, 8])
#start with these arrays:
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
np.concatenate((x, y), axis=0)
o/p:
array([[1, 2],
[3, 4],
[5, 6]])

More Related Content

NumPy.pptx

  • 1. NumPy in Python Dr.G.Jasmine Beulah Assistant Professor, Dept Computer Science, Kristu Jayanti College, Bengaluru.
  • 2. • NumPy (Numerical Python) is an open source Python library that’s used in almost every field of science and engineering. • It’s the universal standard for working with numerical data in Python, and it’s at the core of the scientific Python and PyData ecosystems.
  • 3. • The NumPy library contains multidimensional array and matrix data structures. • It provides ndarray, a homogeneous n-dimensional array object, with methods to efficiently operate on it. NumPy can be used to perform a wide variety of mathematical operations on arrays. • It adds powerful data structures to Python that guarantee efficient calculations with arrays and matrices and it supplies an enormous library of high-level mathematical functions that operate on these arrays and matrices.
  • 4. How to import NumPy • To access NumPy and its functions import it in your Python code like this: • import numpy as np
  • 5. What’s the difference between a Python list and a NumPy array? • NumPy gives you an enormous range of fast and efficient ways of creating arrays and manipulating numerical data inside them. • While a Python list can contain different data types within a single list, all of the elements in a NumPy array should be homogeneous. • The mathematical operations that are meant to be performed on arrays would be extremely inefficient if the arrays weren’t homogeneous.
  • 6. Why use NumPy? • NumPy arrays are faster and more compact than Python lists. • An array consumes less memory and is convenient to use. • NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. • This allows the code to be optimized even further.
  • 7. What is an array? • An array is a central data structure of the NumPy library. • An array is a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element. • It has a grid of elements that can be indexed in various ways. • The elements are all of the same type, referred to as the array dtype.
  • 8. • An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers. • The rank of the array is the number of dimensions. • The shape of the array is a tuple of integers giving the size of the array along each dimension. • One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional data. For example: a = np.array([1, 2, 3, 4, 5, 6]) or: a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
  • 9. • We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array, you’ll be accessing element “0”. print(a[0]) [1 2 3 4]
  • 10. How to create a basic array • To create a NumPy array, you can use the function np.array(). import numpy as np a = np.array([1, 2, 3])
  • 11. create an array filled with 0’s: np.zeros(2) array([0., 0.]) An array filled with 1’s: np.ones(2) array([1., 1.])
  • 12. Create a NumPy ndarray Object • NumPy is used to work with arrays. The array object in NumPy is called ndarray. • We can create a NumPy ndarray object by using the array() function. import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type(arr)) o/p:arr is numpy.ndarray type
  • 13. Use a tuple to create a NumPy array: • To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray: import numpy as np arr = np.array((1, 2, 3, 4, 5)) print(arr) o/p: [1 2 3 4 5]
  • 14. Dimensions in Arrays • A dimension in arrays is one level of array depth (nested arrays). • nested array: are arrays that have arrays as their elements. 0-D Arrays 0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array. Example Create a 0-D array with value 42 import numpy as np arr = np.array(42) print(arr)
  • 15. 1-D Arrays • An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. • These are the most common and basic arrays. Example Create a 1-D array containing the values 1,2,3,4,5: import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr)
  • 16. 2-D Arrays • An array that has 1-D arrays as its elements is called a 2-D array. • These are often used to represent matrix or 2nd order tensors. • NumPy has a whole sub module dedicated towards matrix operations called numpy.mat Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6: import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr)
  • 17. 3-D arrays • n array that has 2-D arrays (matrices) as its elements is called 3-D array. • These are often used to represent a 3rd order tensor. • Create a 3-D array with two 2-D arrays, both containing two arrays with the 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) o/p: [[[1 2 3] [4 5 6]] [[1 2 3] [4 5 6]]]
  • 18. Check Number of Dimensions? NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have. Example 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)
  • 19. • Creating an array from a sequence of elements, you can easily create an array filled with 0’s: np.zeros(2) An array filled with 1’s: np.ones(2) # Create an empty array with 2 elements np.empty(2) #create an array with a range of elements: np.arange(4) #an array that contains a range of evenly spaced intervals. To do this, you will specify the first number, last number, and the step size. np.arange(2, 9, 2)
  • 20. Use the Numpy Linspace Function • The NumPy linspace function (sometimes called np.linspace) is a tool in Python for creating numeric sequences. np.linspace(start = 0, stop = 100, num = 5)
  • 24. Specifying your data type While the default data type is floating point (np.float64), you can explicitly specify which data type you want using the dtype keyword.
  • 25. Adding, removing, and sorting elements arr = np.array([2, 1, 5, 3, 7, 4, 6, 8]) np.sort(arr) a = np.array([1, 2, 3, 4]) b = np.array([5, 6, 7, 8]) #concatenate with np.concatenate() np.concatenate((a, b)) o/p: array([1, 2, 3, 4, 5, 6, 7, 8]) #start with these arrays: x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6]]) np.concatenate((x, y), axis=0) o/p: array([[1, 2], [3, 4], [5, 6]])