0% found this document useful (0 votes)
6 views3 pages

NumPy Array Operations and Examples

The document provides examples of using NumPy to create and manipulate arrays, including creating arrays of specific shapes, reshaping them, and performing mathematical operations. It demonstrates how to access elements in both lists and NumPy arrays, as well as how to perform element-wise multiplication and dot products between arrays. Key functions such as np.array, np.ones, np.full, np.random.random, np.reshape, and np.dot are highlighted.

Uploaded by

minthantphone
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

NumPy Array Operations and Examples

The document provides examples of using NumPy to create and manipulate arrays, including creating arrays of specific shapes, reshaping them, and performing mathematical operations. It demonstrates how to access elements in both lists and NumPy arrays, as well as how to perform element-wise multiplication and dot products between arrays. Key functions such as np.array, np.ones, np.full, np.random.random, np.reshape, and np.dot are highlighted.

Uploaded by

minthantphone
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1)

m = [[1, 2, 3],
[4, 5, 6]]
#We can turn this list of lists into a NumPy array by calling the array constructor,
as follows:
m = [Link]([[1, 2, 3],[4, 5, 6]])

2)
# The tuple (2, 3, 4) means a matrix of size 2x3x4.
>>> x = [Link]((2, 3, 4))
>>> print([Link]) (2, 3, 4)
>>> print(x)

3)
# create an array full of 7's, # of shape (2, 3).
>>> print([Link]((2, 3), 7))
# create an array of random values # between 0 and 1.
>>> print([Link]((2, 2)))

4)
>>> c = [Link](6)
>>> print(c) [0 1 2 3 4 5]
# change the size from (6,) to (2, 3).
>>> d = [Link](c, (2, 3))
>>> print(d)

5)
c = [Link](6) # [0 1 2 3 4 5]
# change the size from (6,) to (2, 3).
d = [Link](c, (2, 3))
print(d)
# change the size from (6,) to (3, 2).
d = [Link](c, (3, 2))
print(d)

6)
md_arr = [Link]([[1, 2, 3],[4, 5, 6]])
print(md_arr.shape) (2, 3)
print(md_arr[0, 0], md_arr[0, 1],md_arr[1, 0], md_arr[1, 2])

7)
lst = [[1, 2, 3], [4, 5, 6]
print(lst[0][2]) # item at row 0, column 2 is 3
#But when dealing with 2D NumPy arrays, we should use one set of square brackets,
separating our two indices with a comma.

mat = [Link](lst)
print(mat[0, 2]) # item at row 0, column 2 is 3

8)
b = [Link]([[1, 4, 5],[9,7,4]]
print(2 + b)

9)
import numpy as np
arr=[Link]([[1,2,3,4,5,6],[11,12,13,14,15,16],[31,32,33,34,35,36],[41,42,43,44,45,46],
[51,52,53,54,55,56],[61,62,63,64,65,66]])
print(arr)
print(arr[:,1])
print(arr[::2,::3])
print(arr[1,2:5])
print(arr[3:5,4:6])
print(arr[::5,::5])
print(arr[1:5,1:5])

10)
b = [Link]([[1, 4, 5][9, 7, 4]])
np.set_printoptions(precision=3)
print([Link](b))

11)
b = [Link]([[1, 4, 5],[9, 7, 4]])
c = [Link]([[0, 1, 2],[3, 4, 5]])
# Note: both matrices must have the same shape
print(b*c)

12)
b = [Link]([[1, 4, 5],[9, 7, 4]])
d = [Link]([[ 4, 2],[ 9, 8],[-3, 6])
# shapes must be (x, y) dot (y, z) --> (x, z)
print([Link](b, d))

You might also like