Docsity

Numpy: An Introduction to Array Operations and Manipulation, Study Guides, Projects, Research of Advanced Computer Programming

Toggle sidebar
bg1
Numpy
May 29, 2019
Numpy Basics
In [1]: import numpy as np
#Create a rank 1 array
a=np.array([1,2,3])
print(type(a))
# check the object created
print(a.dtype)
# data type of the elements of the array
print(a.ndim)
# check the dimention of the array
print(a.shape)
# check the (row,col) of the array, shape returns a tuple
print(a[0], a[1], a[2])
a[0]= 5
# Change an element of the array
print(a)
# Create a rank 2 array
b=np.array([[1,2,3],[4,5,6]])
print(b.shape)
print(b[0,0], b[0,1], b[1,0])
<class
'
numpy.ndarray
'
>
int64
1
(3,)
1 2 3
[5 2 3]
(2, 3)
1 2 4
In [2]:
#different ways of creating arrays
import numpy as np
# Create an array of all zeros
a=np.zeros((2,2))
print(a)
# Create an array of all ones
b=np.ones((1,2))
1
pf3
pf4
pf5

Partial preview of the text

Download Numpy: An Introduction to Array Operations and Manipulation and more Study Guides, Projects, Research Advanced Computer Programming in PDF only on Docsity!

Numpy

May 29, 2019

Numpy Basics

In [1]: import numpy as np

#Create a rank 1 array a = np.array([1, 2, 3]) print(type(a))# check the object created print(a.dtype)# data type of the elements of the array print(a.ndim)# check the dimention of the array print(a.shape)# check the (row,col) of the array, shape returns a tuple print(a[0], a[1], a[2]) a[0] = 5 # Change an element of the array print(a)

Create a rank 2 array

b = np.array([[1,2,3],[4,5,6]]) print(b.shape) print(b[0, 0], b[0, 1], b[1, 0])

int 1 (3,) 1 2 3 [5 2 3] (2, 3) 1 2 4

In [2]: #different ways of creating arrays import numpy as np

Create an array of all zeros

a = np.zeros((2,2)) print(a)

Create an array of all ones

b = np.ones((1,2))

print(b)

Create a constant array

c = np.full((2,2), 7) print(c)

Create a 2x2 identity matrix

d = np.eye(2) print(d)

Create an array filled with random values

e = np.random.random((2,2)) print(e)

#using arange and reshape methods f = np.arange(12).reshape(4,3) print(f)

[[0. 0.] [0. 0.]] [[1. 1.]] [[7 7] [7 7]] [[1. 0.] [0. 1.]] [[0.85243703 0.07421425] [0.35034596 0.0979054 ]] [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]]

In [3]: #Indexing & Slicing import numpy as np a = np.arange(10) print(a) print(a[0])#prints the element at the given index

s = slice(0,7,2) # a part of array with starting index and ending index #with steps given is sliced print (a[s]) b = a[2:9:2]# another way of slicing print(b)

[0 1 2 3 4 5 6 7 8 9] 0 [0 2 4 6]

print(b*2)

#functions applied to elements of arrays print(a.sum()) print(a.min()) print(np.log(a)) print('\n') print(np.sqrt(b))

[ 4 13 17 11] [ 3 40 72 18] 133 [ 4 6 9 10]

[ 2 16 18 4]

[1.09861229 1.60943791 2.07944154 2.19722458]

[1. 2.82842712 3. 1.41421356]

In [6]: ar = np.array([[6, 1, 1], [4, -2, 5], [2, 8, 7]]) print(ar) print('\n') #transpose ar_trans= ar.T print(ar_trans) print('\n') #determinant print(np.linalg.det(ar)) #rank print(np.linalg.matrix_rank(ar)) #inverse print(np.linalg.inv(ar)) print('\n') #power iteration print(np.linalg.matrix_power(ar, 3))

[[ 6 1 1] [ 4 -2 5] [ 2 8 7]]

[[ 6 4 2]

[ 1 -2 8]

[ 1 5 7]]

[[ 0.17647059 -0.00326797 -0.02287582]

[ 0.05882353 -0.13071895 0.08496732]

[-0.11764706 0.1503268 0.05228758]]

[[336 162 228]

[406 162 469]

[698 702 905]]