0% found this document useful (0 votes)
12 views

p#02 ML 46

Uploaded by

Kiki Nhabinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

p#02 ML 46

Uploaded by

Kiki Nhabinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

import numpy as np

import numpy as np

# 1. Demonstrate the working of specified functions:

# numpy.linspace()
a_linspace = np.linspace(1, 10, 5)
print("Arrays linspace:",a_linspace )
print("---------------------------------------------------------------
-----------")
# numpy.repeat()
a_repeat = np.repeat(3, 4)
print("Array repeat:",a_repeat)
print("---------------------------------------------------------------
-----------")
# numpy.random()
a_random = np.random.rand(3, 2)
print("Array Random:",a_random)
print("---------------------------------------------------------------
-----------")
# numpy.nan()
a_nan = np.nan
print("Array NaN:",a_nan)
print("---------------------------------------------------------------
-----------")
# numpy.min() and numpy.max()
a = np.array([4, 2, 8, 6, 5])
min_val = np.min(a)
max_val = np.max(a)
print("Minimam Value:",min_val)
print("---------------------------------------------------------------
-----------")
print("Maximam Value:",max_val)
print("---------------------------------------------------------------
-----------")

# numpy.shape()
a_shape = np.array([[1, 2], [3, 4], [5, 6]])
shape = np.shape(a_shape)
print(a)
print("---------------------------------------------------------------
-----------")
print("Array Shape:",a_shape)

print("---------------------------------------------------------------
-----------")
# numpy.argmax()
a_argmax = np.array([10, 30, 20, 40, 50])
argmax_idx = np.argmax(a_argmax)
print("Maximum argument:",a_argmax)
print("---------------------------------------------------------------
-----------")
print("argument index:",argmax_idx)

print("---------------------------------------------------------------
-----------")
# numpy.reshape()
a_reshape = np.arange(12).reshape(3, 4)
print("Array reshape:",a_reshape)

print("---------------------------------------------------------------
-----------")
# numpy.histogram()
hist_values, bin_edges = np.histogram(a_argmax, bins=[0, 20, 40, 60])

print("Computation of histogram:",hist_values, bin_edges)

print("---------------------------------------------------------------
-----------")
# numpy.mean()
mean_val = np.mean(a_argmax)
print("Array mean value:",mean_val)

print("---------------------------------------------------------------
-----------")
# numpy.sort()
a_sort = np.array([5, 3, 1, 4, 2])
sorted_a = np.sort(a_sort)
print(a_sort)
print("---------------------------------------------------------------
-----------")
print("Sorted Array:",sorted_a)

Arrays linspace: [ 1. 3.25 5.5 7.75 10. ]


----------------------------------------------------------------------
----
Array repeat: [3 3 3 3]
----------------------------------------------------------------------
----
Array Random: [[0.25458965 0.46699484]
[0.3527139 0.41972761]
[0.58007813 0.59765947]]
----------------------------------------------------------------------
----
Array NaN: nan
----------------------------------------------------------------------
----
Minimam Value: 2
----------------------------------------------------------------------
----
Maximam Value: 8
----------------------------------------------------------------------
----
[4 2 8 6 5]
----------------------------------------------------------------------
----
Array Shape: [[1 2]
[3 4]
[5 6]]
----------------------------------------------------------------------
----
Maximum argument: [10 30 20 40 50]
----------------------------------------------------------------------
----
argument index: 4
----------------------------------------------------------------------
----
Array reshape: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
----------------------------------------------------------------------
----
Computation of histogram: [1 2 2] [ 0 20 40 60]
----------------------------------------------------------------------
----
Array mean value: 30.0
----------------------------------------------------------------------
----
[5 3 1 4 2]
----------------------------------------------------------------------
----
Sorted Array: [1 2 3 4 5]

# 2. Create a 4x2 integer array and print its attributes:


arr_4x2 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
print("Shape:", arr_4x2.shape)
print("Dimensions:", arr_4x2.ndim)

Shape: (4, 2)
Dimensions: 2

# 3. Create a 5x2 integer array with values ranging from 100 to 200 in
steps of 10:
arr_range = np.arange(100, 200, 10).reshape(5, 2)
print(arr_range)

[[100 110]
[120 130]
[140 150]
[160 170]
[180 190]]

# 4. Return the third column from all rows of the provided array:
a = np.array([[11, 22, 33], [44, 55, 66], [77, 88, 99]])
third_column = a[:, 2]
print(a)
print("---------------------------------------------------------------
-----------")
print("third_column:", third_column)

[[11 22 33]
[44 55 66]
[77 88 99]]
third_column: [33 66 99]

# 5. Return odd rows and even columns from a given array:


a = np.array([[3, 6, 9, 12], [15, 18, 21, 24], [27, 30, 33, 36], [39,
42, 45, 48], [51, 54, 57, 60]])
odd_rows_even_columns = a[::2, 1::2]
print(a)
print("---------------------------------------------------------------
-----------")
print("odd_rows_even_columns:", odd_rows_even_columns)

[[ 3 6 9 12]
[15 18 21 24]
[27 30 33 36]
[39 42 45 48]
[51 54 57 60]]
odd_rows_even_columns: [[ 6 12]
[30 36]
[54 60]]

# 6. Create a result array by adding and then squaring two arrays:


a1 = np.array([[8, 9, 10], [23, 28, 29]])
a2 = np.array([[17, 36, 28], [6, 7, 1]])
result_array = (a1 + a2) ** 2

print("result_array:",result_array )

result_array: [[ 625 2025 1444]


[ 841 1225 900]]

# 7. Split an array into four equal-sized sub-arrays:


arr_split = np.arange(10, 34).reshape(8, 3)
sub_arrays = np.split(arr_split, 4)
print("arr_split", arr_split)
print("---------------------------------------------------------------
-----------")
print("sub_arrays", sub_arrays)
arr_split [[10 11 12]
[13 14 15]
[16 17 18]
[19 20 21]
[22 23 24]
[25 26 27]
[28 29 30]
[31 32 33]]
sub_arrays [array([[10, 11, 12],
[13, 14, 15]]), array([[16, 17, 18],
[19, 20, 21]]), array([[22, 23, 24],
[25, 26, 27]]), array([[28, 29, 30],
[31, 32, 33]])]

# 8. Sort a numpy array by the second row and second column:


sampleArray = np.array([[36, 46, 77], [87, 28, 19], [55, 97, 68]])
sorted_by_second_row = sampleArray[:, sampleArray[1].argsort()]
sorted_by_second_column = sampleArray[sampleArray[:, 1].argsort()]
print("sorted by the second row:",sorted_by_second_row)
print("---------------------------------------------------------------
-----------")
print("sorted by the second column:",sorted_by_second_column)

sorted by the second row: [[77 46 36]


[19 28 87]
[68 97 55]]
----------------------------------------------------------------------
----
sorted by the second column: [[87 28 19]
[36 46 77]
[55 97 68]]

# 9. Print max from axis 0 and min from axis 1 of a 2-D array:
a = np.array([[35, 46, 77], [83, 23, 13], [55, 96, 67]])
max_axis0 = np.max(a, axis=0)
min_axis1 = np.min(a, axis=1)

print("max axis 0:",max_axis0)


print("---------------------------------------------------------------
-----------")
print("mix axis 1:",min_axis1)

max axis 0: [83 96 77]


----------------------------------------------------------------------
----
mix axis 1: [35 13 55]

# 10. Delete the second column and insert a new column:


a = np.array([[44, 45, 76], [84, 24, 15], [56, 97, 68]])
newColumn = np.array([[20, 20, 20]])
a = np.delete(a, 1, axis=1)
a = np.insert(a, 1, newColumn, axis=1)

You might also like