import numpy as np
first_arr=np.array([1, 2, 3, 5])
first_arr
array([1, 2, 3, 5])
np.insert()
- more useful than append, because you can specify at which index you want to insert. Appending just adds the value to the end of the array.
np.insert(index, value)
new_first_arr=np.insert(first_arr,3,4)
new_first_arr
array([1, 2, 3, 4, 5])
second_arr=np.array([1,2,3,4])
second_arr
array([1, 2, 3, 4])
new_second_arr=np.append(second_arr,5)
new_second_arr
array([1, 2, 3, 4, 5])
third_arr=np.array([1,2,3,4,5])
third_arr
array([1, 2, 3, 4, 5])
np.delete()
np.delete(index)
del_arr=np.delete(third_arr,4)
del_arr
array([1, 2, 3, 4])
np.sort()
- returns a copy of the array, which is the same as the original but in sorted order. With multiple dimensional arrays, each array within will be sorted.
np.sort(array_to_sort)
integers_arr=np.random.randint(0,20,20)
integers_arr
array([ 9, 12, 3, 18, 3, 13, 10, 11, 15, 17, 17, 2, 8, 0, 16, 11, 10, 4, 10, 14])
print(np.sort(integers_arr))
[ 0 2 3 3 4 8 9 10 10 10 11 11 12 13 14 15 16 17 17 18]
integers_2dim_arr=np.array([[3, 2, 5,7, 4], [5, 0, 8,3, 1]])
integers_2dim_arr
array([[3, 2, 5, 7, 4], [5, 0, 8, 3, 1]])
print(np.sort(integers_2dim_arr))
[[2 3 4 5 7] [0 1 3 5 8]]
colors=np.array(['orange','green','yellow','white','black','pink','blue','red'])
colors
array(['orange', 'green', 'yellow', 'white', 'black', 'pink', 'blue', 'red'], dtype='<U6')
print(np.sort(colors))
['black' 'blue' 'green' 'orange' 'pink' 'red' 'white' 'yellow']
Some functions will return a copy while others return a view.
Copy = a new array physically stored at a different location.
View = a different view of the original array, a different reference to the same location in memory.
id()
- shows where in memory an element is stored.
students_ids_number=np.array([1111,1212,1313,1414,1515,1616,1717,1818])
students_ids_number
array([1111, 1212, 1313, 1414, 1515, 1616, 1717, 1818])
students_ids_number_reg=students_ids_number
print("id of students_ids_number",id(students_ids_number))
print("id of students_ids_number_reg",id(students_ids_number_reg))
id of students_ids_number 140157436332432 id of students_ids_number_reg 140157436332432
students_ids_number_reg[1]=2222
print(students_ids_number)
print(students_ids_number_reg)
[1111 2222 1313 1414 1515 1616 1717 1818] [1111 2222 1313 1414 1515 1616 1717 1818]
# This creates a deep copy
students_ids_number_cp=students_ids_number.copy()
print(students_ids_number_cp)
[1111 2222 1313 1414 1515 1616 1717 1818]
# Checking value equality between two arrays
print(students_ids_number_cp==students_ids_number)
[ True True True True True True True True]
print ("id of students_ids_number",id(students_ids_number))
print("id of students_ids_number_cp",id(students_ids_number_cp))
id of students_ids_number 140157436332432 id of students_ids_number_cp 140157132727344
students_ids_number[0]=1000
print ("original: ", students_ids_number)
print("copy: ",students_ids_number_cp)
original: [1000 2222 1313 1414 1515 1616 1717 1818] copy: [1111 2222 1313 1414 1515 1616 1717 1818]
# .view() also creates just another view of the same place in mmemory
# Changes to either will affect the other, because they are the same
students_ids_number_v=students_ids_number.view()
students_ids_number_v[0]=2000
print("original: ", students_ids_number)
print("view:",students_ids_number_v)
original: [2000 2222 1313 1414 1515 1616 1717 1818] view: [2000 2222 1313 1414 1515 1616 1717 1818]
.base
will tell whether or not the array owns the data.¶It returns
None
if the array owns the data, otherwise it returns the original data / array
print(students_ids_number_cp.base)
print(students_ids_number_v.base)
None [2000 2222 1313 1414 1515 1616 1717 1818]
np.reshape(array, (rows, columns))
for 1 dimension or
np.reshape(array, (dimensions, rows, columns))
for more dimensions
- the shape of the array changes while the number of elements stays the same.
If you try to convert to an array shape that is not mathematically possible, you will get a value error. The number of elements required for the reshape must be present in the original array.
first_arr=np.arange(1,13)
first_arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
second_arr=np.reshape(first_arr,(3,4))
second_arr
array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]])
third_arr=np.reshape(first_arr,(6,2))
third_arr
array([[ 1, 2], [ 3, 4], [ 5, 6], [ 7, 8], [ 9, 10], [11, 12]])
# This throws a value error, since an array with 12 elements
# cannot be reshaped in this way
# fourth_arr=np.reshape(first_arr,(4,4))
fifth_arr=np.reshape(first_arr,(3,2,2))
print(fifth_arr)
print("Dimensions of fifth_arr is ",fifth_arr.ndim)
[[[ 1 2] [ 3 4]] [[ 5 6] [ 7 8]] [[ 9 10] [11 12]]] Dimensions of fifth_arr is 3
np.reshape(array, -1)
- To do this, pass a negative 1 after the name of the array you want to flattend.
sixth_arr=np.array([[1,2],[3,4],[5,6]])
sixth_arr
array([[1, 2], [3, 4], [5, 6]])
seventh_arr_flat=np.reshape(sixth_arr,-1)
seventh_arr_flat
array([1, 2, 3, 4, 5, 6])
np.flatten()
and np.ravel()
np.flatten()
- will create a copy so that any changes do not affect the original. This is a copy of the input array, flattened to one dimension.
np.ravel()
- will create a view so that any changes WILL affect the original. But it is memory efficient, since it does not make an actual copy. Returns a flattened, one-dimensional array.
eighth_arr_flat=sixth_arr.flatten()
print("eighth_arr_flat:",eighth_arr_flat)
ninth_arr_rav=sixth_arr.ravel()
print("ninth_arr_rav:",ninth_arr_rav)
eighth_arr_flat: [1 2 3 4 5 6] ninth_arr_rav: [1 2 3 4 5 6]
eighth_arr_flat[0]=100
ninth_arr_rav[0]=200
print("eighth_arr_flat:",eighth_arr_flat)
print("ninth_arr_rav:",ninth_arr_rav)
print("sixth_arr:",sixth_arr)
eighth_arr_flat: [100 2 3 4 5 6] ninth_arr_rav: [200 2 3 4 5 6] sixth_arr: [[200 2] [ 3 4] [ 5 6]]
twodim_arr=np.reshape(np.arange(12),(3,4))
twodim_arr
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
# passing two integers will get the element at that specific
# location in the multidimensional array.
twodim_arr[1,1]
5
# passing one integer will return an entire row from the array
twodim_arr[1]
array([4, 5, 6, 7])
# Look into the following syntax. This is new to me. (3*4*5)
threedim_arr=np.reshape(np.arange(3*4*5),(3,4,5))
threedim_arr
array([[[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]], [[20, 21, 22, 23, 24], [25, 26, 27, 28, 29], [30, 31, 32, 33, 34], [35, 36, 37, 38, 39]], [[40, 41, 42, 43, 44], [45, 46, 47, 48, 49], [50, 51, 52, 53, 54], [55, 56, 57, 58, 59]]])
np.reshape(np.arange(2*2*2),(4,2))
array([[0, 1], [2, 3], [4, 5], [6, 7]])
threedim_arr[0,2,3]
13
# Negative indexing works with arrays
threedim_arr[2,-1,-1]
59
onedim_arr=np.arange(10)
onedim_arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
onedim_arr[2:6]
array([2, 3, 4, 5])
onedim_arr[:5]
array([0, 1, 2, 3, 4])
onedim_arr[-3:]
array([7, 8, 9])
onedim_arr[::2]
array([0, 2, 4, 6, 8])
twodim_arr
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
# Start with the second row, row [1]
# Get every element from the second element, element [1]
# to the end
twodim_arr[1:,1:]
array([[ 5, 6, 7], [ 9, 10, 11]])
# Starting with the first row and every row after,
# get the second element to the end of each row
# Think of it as starting with the first row, get each
# column from the second one on.
twodim_arr[0:, 1:]
array([[ 1, 2, 3], [ 5, 6, 7], [ 9, 10, 11]])
twodim_arr[0:, 2:]
array([[ 2, 3], [ 6, 7], [10, 11]])
twodim_arr[2:, 1:]
array([[ 9, 10, 11]])
# All elements of second row, row [1]
twodim_arr[1,:]
array([4, 5, 6, 7])
# All elements from the third column, col [2]
twodim_arr[:,2]
array([ 2, 6, 10])
twodim_arr[:, 2:]
array([[ 2, 3], [ 6, 7], [10, 11]])
Functions for joining arrays:
np.concatenate(array01, array02, joining_axis)
, default axis=0np.stack()
works along a new axisnp.hstack()
- horizontal stackingnp.vstack()
- vertical stacking, likenp.stack()
first_arr=np.arange(1,11)
second_arr=np.arange(11,21)
print("first_arr",first_arr)
print("second_arr",second_arr)
first_arr [ 1 2 3 4 5 6 7 8 9 10] second_arr [11 12 13 14 15 16 17 18 19 20]
np.concatenate()
¶con_arr=np.concatenate((first_arr,second_arr))
con_arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
# Array with 2 rows, 5 cols
third_2darr=np.array([[1,2,3,4,5], [6,7,8,9,10]])
third_2darr
array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10]])
fourth_2darr=np.array([[11,12,13,14,15], [16,17,18,19,20]])
fourth_2darr
array([[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])
# axis=1 tells it to join along the rows
# It cannot concatenate along axis 0
con2d_arr = np.concatenate((third_2darr,fourth_2darr),axis=1)
con2d_arr
array([[ 1, 2, 3, 4, 5, 11, 12, 13, 14, 15], [ 6, 7, 8, 9, 10, 16, 17, 18, 19, 20]])
np.stack()
¶print("This is first_arr: ", first_arr)
print("This is second_arr: ", second_arr)
st_arr = np.stack((first_arr,second_arr))
print('')
print("This is them using np.stack(): \n", st_arr)
print('')
This is first_arr: [ 1 2 3 4 5 6 7 8 9 10] This is second_arr: [11 12 13 14 15 16 17 18 19 20] This is them using np.stack(): [[ 1 2 3 4 5 6 7 8 9 10] [11 12 13 14 15 16 17 18 19 20]]
np.hstack()
¶print("This is first_arr: ", first_arr)
print("This is second_arr: ", second_arr)
hst_arr=np.hstack((first_arr,second_arr))
print('')
print("This is them using np.hstack(): \n", hst_arr)
print('')
This is first_arr: [ 1 2 3 4 5 6 7 8 9 10] This is second_arr: [11 12 13 14 15 16 17 18 19 20] This is them using np.hstack(): [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
np.vstack()
¶print("This is first_arr: ", first_arr)
print("This is second_arr: ", second_arr)
vst_arr=np.vstack((first_arr,second_arr))
print('')
print("This is them using np.vstack(): \n", vst_arr)
print('')
This is first_arr: [ 1 2 3 4 5 6 7 8 9 10] This is second_arr: [11 12 13 14 15 16 17 18 19 20] This is them using np.vstack(): [[ 1 2 3 4 5 6 7 8 9 10] [11 12 13 14 15 16 17 18 19 20]]
# Checking the equality of the stacks resulting from np.stack()
# and np.vstack()
st_arr == vst_arr
array([[ True, True, True, True, True, True, True, True, True, True], [ True, True, True, True, True, True, True, True, True, True]])
Functions for splitting arrays:
np.split()
- this will only split arrays when the number of elements is evenly divisible by the number of splits. Resulting arrays must have the same shape.np.array_split(array, num_splits)
np.hsplit()
- split along rowsnp.vsplit()
- split along columns
fifth_arr=np.arange(1,13)
fifth_arr
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
sp_arr=np.array_split(fifth_arr,4)
sp_arr
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9]), array([10, 11, 12])]
print(sp_arr[1])
[4 5 6]
# np.array_split() will divide the array into the num_splits
# passed, even if not evenly
sp_arr=np.array_split(fifth_arr,8)
sp_arr
[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8]), array([9]), array([10]), array([11]), array([12])]
print("This is third_2darr: ", third_2darr)
print('')
print("This is third_2darr after hsplit with 5 splits: \n")
print(np.hsplit(third_2darr,5))
print('')
This is third_2darr: [[ 1 2 3 4 5] [ 6 7 8 9 10]] This is third_2darr after hsplit with 5 splits: [array([[1], [6]]), array([[2], [7]]), array([[3], [8]]), array([[4], [9]]), array([[ 5], [10]])]
print("This is third_2darr: \n", third_2darr)
vs_arr=np.vsplit(third_2darr,2)
print('')
print("This is third_2darr after vsplit with 2 splits:")
print(vs_arr)
print('')
This is third_2darr: [[ 1 2 3 4 5] [ 6 7 8 9 10]] This is third_2darr after vsplit with 2 splits: [array([[1, 2, 3, 4, 5]]), array([[ 6, 7, 8, 9, 10]])]