Unit 5 Part1
Unit 5 Part1
Team Emertxe
In
python
Single Dimensional Arrays
Single Dimensional Arrays
Creating an Array
import array
#Create an array
a = array.array("i", [1, 2, 3, 4])
#Create an array
a = array("i", [1, 2, 3, 4])
#Create an array
a = array('u', ['a', 'b', 'c', 'd'])
#Here, 'u' stands for unicode character
#Create an array
a = array('i', [1,
2, 3, 4])
#To retrieve the items of an array using array index using while loop
#Create an array
a = array('i', [1,
2, 3, 4])
Example arr[1: 4: 1]
#Create array y with Items from 0th till the last Item in x
y = x[0: ]
print(y)
#Create array y with Items from 0th till the 3rd Item in x
y = x[: 4]
print(y)
#Stride 2 means, after 0th Item, retrieve every 2nd Item from x
y = x[0: 7: 2]
print(y)
#To retrieve the items of an array using array index using for loop
#Create an array
a = array('i', [1,
2, 3, 4])
Method Description
a.extend(x) Appends x at the end of the array a. ‘x’ can be another array or an
iterable object
Method Description
#Append 6 to an array
a.append(6)
print(a)
#Insert 11 at
position 1
a.insert(1, 11)
print(a)
Example-2: To
Example-2: Tocreate
createanan array
array of of float
float datatype
datatype
a == array([10.1,
array([10.1,20.2,
20.2,30.3,
30.3, 40.4,
40.4, 50.5],
50.5], float)
float)
Example-3: To create an array of float datatype without specifying the float datatype
Note: If one item in the array is of float type, then Python interpreter converts
remaining items into the float datatype
a = array([1, 2, 3, 4, 5])
print(a)
Example-5 arange(0, 10, 1.5) Produces [0. 1.5 3. 4.5 6. 7.5 9.]
a = arange(2, 11, 2)
print(a)
Single Dimensional Arrays
Creating Array: numpy-zeros() & ones()
Syntax zeros(n, datatype)
ones(n, datatype)
Example-1 zeros(5) Produces items [0. 0. 0. 0. 0.]
Default datatype is float
Example-2 zeros(5, int) Produces items [0 0 0 0 0]
Example- ones(5, float) Produces items [1. 1. 1. 1. 1.]
3
a = zeros(5, int)
print(a)
a2 = array([1, 2, 3, 4])
2. Syntactically clearer
- Writing a + b is clearer than using the loops
power(a, n) Calculates a ^ n
Relational operators are used to compare arrays of same size
These operators compares corresponding items of the arrays and return another array with
Boolean values
Program-1: To compare two arrays and display the resultant Boolean type array
from numpy import *
a = array([1, 2, 3])
b = array([3, 2, 3])
c = a == b
print(c)
c = a > b
print(c)
c = a <= b
print(c)
Single Dimensional Arrays
Comparing Arrays
any(): Used to determine if any one item of the array is True
all(): Used to determine if all items of the array are True
a = array([1, 2, 3])
b = array([3, 2, 3])
c = a > b
print(c)
print("an
y(): ",
any(c))
print("al
l(): ",
all(c))
if (any(a
> b)):
print
("a
conta
ins
one
item
great
er
than
Single Dimensional Arrays
Comparing Arrays
logical_and(), logical_or() and logical_not() are useful to get the Boolean array as a
result of comparing the compound condition
a = array([1, 2, 3])
b = array([3, 2, 3])
where(): used to create a new array based on whether a given condition is True or False
Syntax: a = where(condition, exp1, exp2)
If condition is True, the exp1 is evaluated, the result is stored in array
a, else exp2 will be evaluated
c = where(a % 2 == 0, a, 0)
print(c)
Single Dimensional Arrays
Comparing Arrays
where(): used to create a new array based on whether a given condition is True or False
Syntax: a = where(condition, exp1, exp2)
If condition is True, the exp1 is evaluated, the result is stored in array
a, else exp2 will be evaluated
Exercise-1: To retrieve the biggest item after comparing two arrays using where()
Single Dimensional Arrays
Comparing Arrays
nonzero(): used to know the positions of items which are non-zero
Returns an array that contains the indices of the items of the array which
are non-zero
Syntax: a = nonzero(array)
c = nonzero(a)
‘Aliasing means not copying’. Means another name to the existing object
a = arange(1, 6)
b = a
print(a)
print(b)
b[0] = 99
print(a)
print(b)
Single Dimensional Arrays
Viewing & Copying
view(): To create the duplicate array
Also called as ‘shallow copying’
a = arange(1, 6)
b = a.view() #Creates new array
print(a)
print(b)
print(b)
Single Dimensional Arrays
Viewing & Copying
copy(): To create the copy the original array
Also called as ‘deep copying’
a = arange(1, 6)
b = a.copy() #Creates new array
print(a)
print(b)
print(b)
Multi Dimensional Arrays
Numpy
Multi Dimensional Arrays
Creating an Array
a = array([[1, 2, 3],
[4, 5, 6]]
Example-2:
Example-2: To
Tocreate
createanan
3D array
array of
with 2-2D datatype
float arrays with each 2 rows and 3 cols
a == array([[[1,
array([10.1,2, 20.2,
3],[4,30.3,
5, 6]]40.4, 50.5],
float) [[1, 1, 1], [1, 0, 1]]]
Multi Dimensional Arrays
Attributes of an Array: The ndim
● The ‘ndim’ attribute represents the number of dimensions or axes of an array
● The number of dimensions are also called as ‘rank’
a = array([1, 2, 3])
print(a.ndim)
print(a.shape)
print(a.shape)
print(a.size)
print(a.size)
Multi Dimensional Arrays
Attributes of an Array: The itemsize
● The ‘itemsize’ attribute gives the memory size of an array element in bytes
print(a.itemsize)
print(a.itemsize)
Multi Dimensional Arrays
Attributes of an Array: The dtype
● The ‘dtype’ attribute gives the datatype of the elements in the array
print(a.dtype)
print(a.dtype)
Multi Dimensional Arrays
Attributes of an Array: The nbytes
● The ‘nbytes’ attribute gives the total number of bytes occupied by an array
print(a.nbytes)
print(a.nbytes)
Multi Dimensional Arrays
Methods of an Array: The reshape()
● The ‘reshape’ method is useful to change the shape of an array
a = arange(10) Outputs:
print(a)
#Change to 1D array
a = a.flatten()
print(a)
Multi Dimensional Arrays
Methods of creating an 2D-Array
Example-1:
[[1 2 3]
[4 5 6]]
Example-3 a = ‘1 2; 3 4; 5 6’ [[1 2]
[3 4]
b = matrix(a) [5 6]]
Matrices in Numpy
Getting Diagonal Items
Function diagonal(matrix)
Function max()
min()
Note: Read the matrices from the user and make the program user friendly
THANK YOU