Arrayprogs: November 29, 2024
Arrayprogs: November 29, 2024
[1]: #---------------------------------------------------------------------------------------------
#numpy stddev
import numpy as np
# create a 2D array
array1 = np.array([[2, 5, 9],
[3, 8, 11],
[4, 6, 7]])
1
[5]: #---------------------------------------------------------------------------------------------
#numpyrand
#rand in numpy
import numpy as np
from numpy import random
x = random.randint(100)
print("\n Random integer from 0 to 100 ")
print(x)
#The choice() method allows you to generate a random value based on an array of␣
↪values.
#The choice() method takes an array as a parameter and randomly returns one of␣
↪the values.
x = random.choice([3, 5, 7, 9])
print("\n Random integer from array elements ")
print(x)
#Generate a 2-D array that consists of the values in the array parameter (3, 5,␣
↪7, and 9)
2
random.seed(10)
print("\n seed() output")
print(random.rand(4))
[[9 7 7 5 3]
[3 7 5 9 7]
[5 5 5 9 5]]
Shuffled list
['orange', 'apple', 'banana', 'cherry']
seed() output
[0.77132064 0.02075195 0.63364823 0.74880388]
[37]: #---------------------------------------------------------------------------------------------
#numpy percentile
import numpy as np
# create an array
array1 = np.array([1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
3
# compute the 75th percentile of the array
result2 = np.percentile(array1, 75)
print("75th percentile:",result2)
'''
Here,
[37]: '\nHere,\n\n25% of the values in array1 are less than or equal to 5.5.\n75% of
the values in array1 are less than or equal to 14.5.'
[9]: #---------------------------------------------------------------------------------------------
#numpyminmax
import numpy as np
# create an array
array1 = np.array([2,6,9,15,17,22,65,1,62])
Minimum value: 1
Maximum value: 65
[11]: #---------------------------------------------------------------------------------------------
#numpymedian
import numpy as np
4
print("\n Median for odd number of elements : ")
print(median)
# create a 2D array
array1 = np.array([[2, 4, 6],
[8, 10, 12],
[14, 16, 18]])
5
Median along vertical axis: [ 8. 10. 12.]
Median of entire array: 10.0
[13]: #---------------------------------------------------------------------------------------------
#numpymean
import numpy as np
# create a 2D array
array1 = np.array([[1, 3],
[5, 7]])
[15]: #---------------------------------------------------------------------------------------------
#numpyarraysaveloadnpy
#Save/Load Single NumPy Array in text File
import numpy as np
6
[8, 10, 12]])
Array Saved…
Array Loaded…
[[ 2 4 6]
[ 8 10 12]]
[17]: #---------------------------------------------------------------------------------------------
#numpyarraysaveloadbinary
#Save/Load Single NumPy Array in text File
import numpy as np
Array Saved…
Array Loaded…
[[ 2 4 6]
[ 8 10 12]]
7
[19]: #---------------------------------------------------------------------------------------------
#numpyarraysaveload
#Save/Load Single NumPy Array in text File
import numpy as np
# create an array
array1 = np.array([[1, 3, 5], [2, 4, 6]])
Array Saved…
Array Loaded…
[[1. 3. 5.]
[2. 4. 6.]]
[23]: #---------------------------------------------------------------------------------------------
#arraysplitting
# Python program to demonstrate splitting in numpy
import numpy as np
newarr = np.array_split(arr, 3)
print("\n\n Splitting the array in 3 parts ")
print(newarr)
newarr = np.array_split(arr, 4)
print("\n\n Splitting the array in 4 parts ")
print(newarr)
8
print("\n *******************Splitting 2-D Arrays **************************")
#Split the 2-D array into three 2-D arrays.
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
newarr = np.array_split(arr, 3)
print("\n\n Splitting the array into three 2-D arrays ")
print(newarr)
newarr = np.array_split(arr, 3)
print("\n\n Splitting the array into three 2-D arrays ")
print(newarr)
#Use the hsplit() method to split the 2-D array into three 2-D arrays along␣
↪rows.
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15],␣
↪[16, 17, 18]])
newarr = np.hsplit(arr, 3)
print("\n After splitting the 2-D array into three 2-D arrays along rows")
print(newarr)
#Use the vsplit() method to split the 2-D array into three 2-D arrays along␣
↪rows.
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15],␣
↪[16, 17, 18]])
newarr = np.vsplit(arr, 3)
print("\n After splitting the 2-D array into three 2-D arrays along Columns")
print(newarr)
#Use the dsplit() method to split the 3-D array into 2 arrays along depth.
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
newarr = np.dsplit(arr, 2)
print("\n After splitting the 2-D array into three 2-D arrays along depth")
print(newarr)
newarr = np.hsplit(arr, 2)
print("\n After splitting the 2-D array into three 2-D arrays along rows")
print(newarr)
newarr = np.vsplit(arr, 2)
print("\n After splitting the 2-D array into three 2-D arrays along Columns")
print(newarr)
9
Splitting the array in 4 parts
[array([1, 2]), array([3, 4]), array([5]), array([6])]
After splitting the 2-D array into three 2-D arrays along rows
[array([[ 1],
[ 4],
[ 7],
[10],
[13],
[16]]), array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]), array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
After splitting the 2-D array into three 2-D arrays along Columns
[array([[1, 2, 3],
[4, 5, 6]]), array([[ 7, 8, 9],
[10, 11, 12]]), array([[13, 14, 15],
[16, 17, 18]])]
After splitting the 2-D array into three 2-D arrays along depth
[array([[[1],
[3]],
10
[[5],
[7]]]), array([[[2],
[4]],
[[6],
[8]]])]
After splitting the 2-D array into three 2-D arrays along rows
[array([[[1, 2]],
[[7, 8]]])]
After splitting the 2-D array into three 2-D arrays along Columns
[array([[[1, 2],
[3, 4]]]), array([[[5, 6],
[7, 8]]])]
[25]: #---------------------------------------------------------------------------------------------
#Arrayslicing
#Save/Load Single NumPy Array in text File
import numpy as np
print("\n\n Slice elements from index 4 to the end of the array ")
print(arr[4:])
print("\n\n Slice elements from the beginning to index 4 (not included) ")
print(arr[:4])
print("\n\n Slice from the index 3 from the end to index 1 from the end ")
print(arr[-3:-1])
print("\n\n **Slice from the index 3 from the end to index 1 from the end ")
print(arr[-3:])
11
print(arr[1:5:2])
print("\n\n Slice every other element from the entire array ")
print(arr[::2])
print("\n\n From the second element, slice elements from index 1 to index 4␣
↪(not included) ")
print("\n\n From both elements, slice index 1 to index 4 (not included), this␣
↪will return a 2-D array ")
print(arr[0:2, 1:4])
Slice from the index 3 from the end to index 1 from the end
[5 6]
**Slice from the index 3 from the end to index 1 from the end
[5 6 7]
12
Slice every other element from the entire array
[1 3 5 7]
From the second element, slice elements from index 1 to index 4 (not included)
[7 8 9]
From both elements, slice index 1 to index 4 (not included), this will return a
2-D array
[[2 3 4]
[7 8 9]]
[27]: #---------------------------------------------------------------------------------------------
#array reshping
#Array Reshaping
#Convert the following 1-D array with 12 elements into a 2-D array.
#The outermost dimension will have 4 arrays, each with 3 elements:
import numpy as np
#Convert the following 1-D array with 12 elements into a 3-D array.
#The outermost dimension will have 2 arrays that contains 3 arrays, each with 2␣
↪elements:
newarr = arr.reshape(2, 3, 2)
print("\n\n 1-D array with 12 elements into a 3-D array \n")
print(newarr)
'''Unknown Dimension
You are allowed to have one "unknown" dimension.
13
Meaning that you do not have to specify an exact number for one of the␣
↪dimensions in the reshape method.
Pass -1 as the value, and NumPy will calculate this number for you.'''
'''
Flattening the arrays
Flattening array means converting a multidimensional array into a 1D array.
We can use reshape(-1) to do this.'''
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
14
Flattened array
[1 2 3 4 5 6]
[29]: #Arrayindexing
import numpy as np
print("\n\n Get additon of the third element and fourth element \n")
print(print(arr[2] + arr[3]))
print("\n\n Access the element on the first row, second column \n")
print('2nd element on 1st row: ', arr[0, 1])
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print("\n\n Access the third element of the second array of the first array \n")
print(arr[0, 1, 2])
'''
The first number represents the first dimension, which contains two arrays:
[[1, 2, 3], [4, 5, 6]]
and:
[[7, 8, 9], [10, 11, 12]]
Since we selected 0, we are left with the first array:
[[1, 2, 3], [4, 5, 6]]
The second number represents the second dimension, which also contains two␣
↪arrays:
[1, 2, 3]
15
and:
[4, 5, 6]
Since we selected 1, we are left with the second array:
[4, 5, 6]
The third number represents the third dimension, which contains three values:
4
5
6
Since we selected 2, we end up with the third value:
6
'''
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print("\nPrint the last element from the 2nd dimension \n")
print('Last element from 2nd dim: ', arr[1, -1])
7
None
16
Access the third element of the second array of the first array
17
print("No. of dimensions: ", arr.ndim)
# importing numpy as np
import numpy as np
18
print (gfg)
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]
[[13 14]
[15 16]]]
19
Concatenating along columns (axis=1)
[[[ 1 2]
[ 3 4]
[ 9 10]
[11 12]]
[[ 5 6]
[ 7 8]
[13 14]
[15 16]]]
[[ 9 10]
[11 12]]]
[[[ 5 6]
[ 7 8]]
[[13 14]
[15 16]]]]
[[ 5 6]
[ 7 8]
[13 14]
[15 16]]]
20
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]
[[13 14]
[15 16]]]
# importing numpy as np
import numpy as np
21
#vstack() to stack along Columns.
arr = np.vstack((arr1, arr2))
print("\n\n Vertical Stacking using vstack()")
print(arr)
[ ]:
22