0% found this document useful (0 votes)
26 views

Introduction To NUMPY Lecture Note

This summarizes the key aspects of the NumPy library: 1. NumPy is mainly used to create and edit multidimensional arrays. Arrays can contain only one type of object and are faster than lists with more functionalities to represent mathematical concepts like vectors and matrices. 2. To use NumPy, one must first import it and then can create and manipulate arrays using various operations like slicing, indexing, reshaping, arithmetic operations etc. 3. NumPy provides universal functions to perform element-wise operations like math functions on whole arrays in one function call for efficient computations.

Uploaded by

Azri Hamim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Introduction To NUMPY Lecture Note

This summarizes the key aspects of the NumPy library: 1. NumPy is mainly used to create and edit multidimensional arrays. Arrays can contain only one type of object and are faster than lists with more functionalities to represent mathematical concepts like vectors and matrices. 2. To use NumPy, one must first import it and then can create and manipulate arrays using various operations like slicing, indexing, reshaping, arithmetic operations etc. 3. NumPy provides universal functions to perform element-wise operations like math functions on whole arrays in one function call for efficient computations.

Uploaded by

Azri Hamim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

NUMPY

What is NUMPY
• The NumPy library is mainly used to create and edit arrays.
• An array is a data structure similar to a list, with the
difference that it can contain only one type of object.
• Arrays are faster, they have more functionalities, and they
better represent mathematical concepts like vectors and
matrices.
How to use numpy

• First download numpy (or import) to your python


• Use the import numpy as (anything you want)
• Now you can use numpy
How Computer see Array AKA Matrix
Numpy Operations
• ndim – return the array dimension
a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
• dtype – return the element’s data type
a = np.array([(1,2,3)])
print(a.dtype)
• itemsize – return the byte size of each element
a = np.array([(1,2,3)])
print(a.itemsize)
Numpy Operations
• Reshape

a = np.array([(8,9,10),(11,12,13)])
print(a)
a=a.reshape(3,2)
print(a)
Numpy operations
• Slicing

a=np.array([(8,9),(10,11),(12,13)])
print(a[0:2,1])

• We will get [9 11]


• 0:2 to exclude the second index of the third row of an array.
Otherwise we will get [9 11 13].
Try this code
# Python program to demonstrate
# array creation techniques # Create a sequence of integers
import numpy as np # from 0 to 30 with steps of 5
f = np.arange(0, 30, 5)
  print ("\nA sequential array with steps of 5:\n", f)
# Creating array from list with type float  
a = np.array([[1, 2, 4], [5, 8, 7]], dtype = # Create a sequence of 10 values in range 0 to 5
'float') g = np.linspace(0, 5, 10)
print ("Array created using passed list:\n", a) print ("\nA sequential array with 10 values between"
                                        "0 and 5:\n", g)
   
# Creating array from tuple # Reshaping 3X4 array to 2X2X3 array
b = np.array((1 , 3, 2)) arr = np.array([[1, 2, 3, 4],
print ("\nArray created using passed tuple:\n", b)                 [5, 2, 4, 2],
                  [1, 2, 0, 1]])
# Creating a 3X4 array with all zeros  
c = np.zeros((3, 4)) newarr = arr.reshape(2, 2, 3)
print ("\nAn array initialized with all zeros:\n",  
c) print ("\nOriginal array:\n", arr)
print ("Reshaped array:\n", newarr)
   
# Create a constant value array of complex type # Flatten array
d = np.full((3, 3), 6, dtype = 'complex') arr = np.array([[1, 2, 3], [4, 5, 6]])
print ("\nAn array initialized with all 6s." flarr = arr.flatten()
            "Array type is complex:\n", d)  
print ("\nOriginal array:\n", arr)
# Create an array with random values print ("Fattened array:\n", flarr)
e = np.random.random((2, 2))
print ("\nA random array:\n", e)
 
 
Try this codes
# Python program to demonstrate # Python program to demonstrate
# indexing in numpy # basic operations on single array
import numpy as np import numpy as np
   a = np.array([1, 2, 5, 3])
# An exemplar array
arr = np.array([[-1, 2, 0, 4],
 # add 1 to every element
                [4, -0.5, 6, 0], print ("Adding 1 to every element:", a+1)
                [2.6, 0, 7, 8],  # subtract 3 from each element
                [3, -7, 4, 2.0]]) print ("Subtracting 3 from each element:", a-3)
   # multiply each element by 10
# Slicing array print ("Multiplying each element by 10:", a*10)
temp = arr[:2, ::2]
print ("Array with first 2 rows and alternate"  # square each element
                    "columns(0 and 2):\n", temp) print ("Squaring each element:", a**2)
   # modify existing array
# Integer array indexing example a *= 2
temp = arr[[0, 1, 2, 3], [3, 2, 1, 0]] print ("Doubled each element of original array:", a)
print ("\nElements at indices (0, 3), (1, 2), (2, 1),"
                                    "(3, 0):\n", temp)
 # transpose of array
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]])
   
# boolean array indexing example
print ("\nOriginal array:\n", a)
cond = arr > 0 # cond is a boolean array
print ("Transpose of array:\n", a.T)
temp = arr[cond]
print ("\nElements greater than 0:\n", temp)
Try these codes
# Python program to demonstrate # Python program to demonstrate
# unary operators in numpy # binary operators in Numpy
import numpy as np import numpy as np
   
arr = np.array([[1, 5, 6], a = np.array([[1, 2],
                [4, 7, 2],             [3, 4]])
                [3, 1, 9]]) b = np.array([[4, 3],
            [2, 1]])
 
# maximum element of array  
print ("Largest element is:", arr.max()) # add arrays
print ("Row-wise maximum elements:", print ("Array sum:\n", a + b)
                    arr.max(axis = 1))
 
  # multiply arrays (elementwise multiplication)
# minimum element of array print ("Array multiplication:\n", a*b)
print ("Column-wise minimum elements:",
                        arr.min(axis = 0))
 
# matrix multiplication
  print ("Matrix multiplication:\n", a.dot(b))
# sum of array elements
print ("Sum of all array elements:",
                            arr.sum())
 
# cumulative sum along each row
print ("Cumulative sum along each row:\n",
                        arr.cumsum(axis = 1))
Try these codes
# Python program to demonstrate
# universal functions in numpy
import numpy as np
 
# create an array of sine values
a = np.array([0, np.pi/2, np.pi])
print ("Sine values of array elements:", np.sin(a))
 
# exponential values
a = np.array([0, 1, 2, 3])
print ("Exponent of array elements:", np.exp(a))
 
# square root of array values
print ("Square root of array elements:", np.sqrt(a))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy