Array Notes
Array Notes
1
2. y= a[0: ]
3. y= x[:4]
4. y= x[-4:]
5. y= x[-4, -1]
6. y= x[0: 7: 2]
7. x[1: 3] = array(‘i’, [10, 20])
2
• No need to specify the type
• Default: char
5. • e = array([‘Bengaluru’, ‘Delhi’, ‘Bombay’], dtype = str)
• e = array([‘Bengaluru’, ‘Delhi’, ‘Bombay’])
– dtype can be skipped
6. Array copy
• a = ([1, 2, 3, 4, 5])
• b = (array(a))
• c=a
3
T13: Creating Arrays using zeros() and ones()
1. Uses:
• zeros(): To create an array with all zeroes
• ones(): To create an array with all ones
2. Syntax:
• zeros(n, datatype)
• ones(n, datatype)
3. Default:
• datatype: float
4. Example:
1. zeros(5)
2. zeros(10, int)
3. zeros(5, float)
4. ones(5, int)
5. ones(5)
4
3. Example:
• a = array([1, 2, 3, 4])
• b = array([0, 2, 3, 1])
• c = a == b
• c=a>b
• c=a<b
4. any() and all()
• any(): Returns True, if any one of the item is True
• all(): Returns True, if all the items in the array is True
5. Logical operators on arrays: a = array([0, 10, 3])
• logical_and(a > 0, a < 4)
– Returns [False False True]
• logical_or(a > 4, a < 0)
– Returns [True True False]
• logical_not(a)
– Returns [True Flase False]
6. where()
• Use: To create new array based on whether a given condition is True
or False
• Synatx:
array = where(condition, expression1, expression2)
• Example:
a = array([10, 21, 30, 41, 50])
b = where(a % 2, a, 0)
7. nonzero()
• Use: To know the position of the items which are non zero
• Example:
a = array([1, 2, 0, -1, 0])
b = nonzero(a)
5
1. Copies the contents of array a to b
2. Any modifications done in b will also be reflected in a
3. Usage: b = a.view()
2. copy(): Also called ‘Deep Copying’
1. Copies the contents of array a to b
2. Any modifications done in b will not be reflected in a
3. Usage: b = a.copy()
6
4. itemsize |
5. dtype |
6. nbytes |
3. The ndim attribute
1. Gives number of dimensions or axes of an array
2. Usage: arrayname.ndim
4. The shape attribute
1. Gives the shape of the array
2. Example:
• For 1D, shape gives number of items in row
• For 2D, shape gives number of rows ‘n’ cols
3. Usage: arrayname.shape
4. We can change the shape of an array
a.shape = (3, 2) #Changes to 3 Rows ‘n’ 2 Cols
5. The size attribute
1. Gives total number of items in an array
2. Usage: arrayname.size
6. The itemsize attribute
1. Gives the size of item in terms of bytes
2. Usage: arrayname.itemsize
7. The dtype attribute
1. Gives the data type of an array
2. Usage: arrayname.dtype
8. The nbytes attribute
1. Gives total number of bytes occupied by the array in the memory
2. Usage: arrayname.nbyes
3. Total Memory = SIZE * sizeof(item)
9. The reshape() method
1. Useful: To change the shape of the array
2. Example:
• a = arange(10)
• a = a.reshape(2, 5)
– Change the shape to 2 rows n 5 cols
10. The flatten() method
1. Useful: To return the copy the array collapsed to 1D array
7
T21: Working with Multi-Dimensional array
1. Ways creating multi-dimensional arrays
1. array()
2. ones() and zeroes()
3. eye()
4. reshape()
2. The array()
1. a = array([[1, 2], [3, 4]])
2. The memory allocation will be contiguous
3. The ones() and zeros()
1. Syntax: ones((r, c), dtype)
2. Example: ones((3, 4), float)
3. |||ly for zeros()
4. The eye()
1. Creates 2D array ‘n’ fills diagonal elements with ’1’s
2. Syntax: eye(n, dtype = datatype)
3. Default: float
4. Example: a = eye(3)
5. The reshape()
1. Used: To convert 1D into nD arrays
2. Syntax: reshape(arrayname, (n, r, c))
3. Example-1:
a = array([1, 2, 3, 4, 5, 6])
b = reshape(a, (2, 3))
4. Example-2: To convert 1D into 3D array or 2 2D array of size 3x2
a = arange(12)
b = reshape(a, (2, 3, 2))
8
T23: Slicing in Multi-dimensional array
1. Syntax: arrayname[start, stop, stepsize] - arrayname[row : col: stepsize] -
arrayname[start: stop, start: stop, stepsize] #This is refined syntax
2. Default:
• start: 0
• stop: n
• stepsize: 1
3. Examples:
1. a[: , :]
• a[::]
• a[:]
• Display’s the entire 2D arrays
2. a[0, :] -> Displays 0th row items
3. a[:, 0] -> Displays 0th col items
4. a[0:1, 0:1] -> Displays 0th row, 1st col item
• a[1:2, 1:2] -> Displays 1st row, 2nd col item
5. a[0:2, 0:3]
• 0th row to 1st row
• 0th col to 2nd col
6. a[2:4, 3: ]
• 2nd to 3rd row
• 3rd col to end
9
T25: Getting diagonal elements of a matrix
1. To retrieve a diagonal items from a matrix use diagonal()
2. Syntax: diagonal(matrix)
3. Example:
• a = diagonal(matrix)
10
T30: Transpose of a Matrix
1. Methods:
• transpose()
• getT()
2. t = m.transpose() - t = m.getT()
11