.. ML Lab 07
.. ML Lab 07
Task 1
Import numpy as np and see the version
In
import numpy as np np.version.version
[ ]:
Out[ ]: '1.21.2'
Task 2
How to create 1D array?
In np.arange(10)
[ ]:
Task 3
How to create a boolean array?
Task 4
How to extract items that satisfy a given condition from 1D array?
Task 5
How to replace items that satisfy a condition with another value in numpy array?
In [ ]:
[0 1 0 3 0 5 0 7 0 9]
Task 6
How to replace items that satisfy a condition without affecting the original array?
Task 7
How to reshape an array?
In np.arange(9).reshape(3, -1)
[ ]:
Task 8
How to stack two arrays vertically?
In a = np.repeat(1, 6).reshape(2, -1) b
[ ]: = np.repeat(2, 6).reshape(2, -1)
np.concatenate([a, b], axis=0)
Task 9
a = np.repeat(1, 6).reshape(2, -1) b
= np.repeat(2, 6).reshape(2, -1)
np.concatenate([a, b], axis=1)
Task 10
How to generate custom sequences in numpy without hardcoding?
In a = [1, 0]
[ ]: np.r_[np.repeat(a, 2), np.tile(a, 2)]
Task 11
How to get the common items between two python numpy arrays?
In a = np.arange(0, 20, 2) b
[ ]: = np.arange(0, 20, 3)
np.intersect1d(a, b)
In a = np.arange(0, 20, 2) b
[ ]: = np.arange(0, 20, 3)
np.setdiff1d(a, b)
Task 13
How to get the positions where elements of two arrays match?
a = np.array([1,2,3,2,3,4,3,4,5,6]) b
=
np.array([7,2,10,2,7,4,9,4,9,8])
np.where(a == b)[0]
In [ ]:
Task 14
How to extract all numbers between a given range from a numpy array?
Out[ ]: array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
Task 15
How to make a python function that handles scalars to work on numpy arrays?
In
[ ]: def minn(x, y):
"""Get the minimum of two items"""
if x <= y: return x
else:
return y
a = np.array([5, 7, 9, 8, 6, 4, 5]) b
= np.array([6, 3, 4, 8, 9, 7, 1])
pair_min(a, b)
Task 16
How to swap two columns in a 2d numpy array?
Task 17
How to swap two rows in a 2d numpy array?
In [ ]:
arr = np.arange(16).reshape(4, 4)
arr[[3, 1, 2, 0], :]
Task 18
How to reverse the rows of a 2D array?
In
arr = np.arange(16).reshape(4, 4) arr[::-1]
[ ]:
Task 19
How to reverse the coluns of a 2D array?
Task 20
How to create a 2D array containing random floats between 5 and 10?
In np.random.uniform(5,10, size=(3,3))
[ ]:
Task 21
How to print only 3 decimal places in python numpy array?
np.set_printoptions(precision=3)
np.random.random([3,3])
Task 22
How to pretty print a numpy array by suppressing the scientific notation (like 1e10)?
In
np.set_printoptions(suppress=True) np.random.random([3,3])/1e3
[ ]:
Task 23
How to limit the number of items printed in output of numpy array?
In
np.set_printoptions(threshold=6) np.arange(100)
[ ]:
Task 24
How to print the full numpy array without truncating
In
np.set_printoptions(threshold=sys.maxsize) np.arange(20)
[ ]:
In [ ]:
url = 'https://archive.ics.uci.edu/ml/machine-learningdatabases/iris/iris.data'
iris = np.genfromtxt(url, delimiter=',', dtype='object')
# names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')
iris[:3]
Task 25
Out[ ]: array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa'],
[b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa'],
[b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa']], dtype=object)
Task 26
How to extract a particular column from 1D array of tuples?
Task 27
How to convert a 1d array of tuples to a 2d numpy array?
In
iris_1d = np.genfromtxt(url, delimiter=',', dtype=object) iris_2d
[ ]:
= np.array([row.tolist()[:4] for row in iris_1d]) iris_2d[:4]
Task 28
How to compute the mean, median, standard deviation of a numpy array?
In [ ]:
Task 29
How to normalize an array so the values range exactly between 0 and 1?
sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])
Smax, Smin = sepallength.max(), sepallength.min() S = (sepallength -
Smin)/sepallength.ptp() print(S)
[0.222 0.167 0.111 0.083 0.194 0.306 0.083 0.194 0.028 0.167 0.306 0.139
0.139 0. 0.417 0.389 0.306 0.222 0.389 0.222 0.306 0.222 0.083 0.222
0.139 0.194 0.194 0.25 0.25 0.111 0.139 0.306 0.25 0.333 0.167 0.194
0.333 0.167 0.028 0.222 0.194 0.056 0.028 0.194 0.222 0.139 0.222 0.083
0.278 0.194 0.75 0.583 0.722 0.333 0.611 0.389 0.556 0.167 0.639 0.25
0.194 0.444 0.472 0.5 0.361 0.667 0.361 0.417 0.528 0.361 0.444 0.5
0.556 0.5 0.583 0.639 0.694 0.667 0.472 0.389 0.333 0.333 0.417 0.472
0.306 0.472 0.667 0.556 0.361 0.333 0.333 0.5 0.417 0.194 0.361 0.389
0.389 0.528 0.222 0.389 0.556 0.417 0.778 0.556 0.611 0.917 0.167 0.833
0.667 0.806 0.611 0.583 0.694 0.389 0.417 0.583 0.611 0.944 0.944 0.472
0.722 0.361 0.944 0.556 0.667 0.806 0.528 0.5 0.583 0.806 0.861 1.
0.583 0.556 0.5 0.944 0.556 0.583 0.472 0.722 0.667 0.722 0.417 0.694
0.667 0.667 0.556 0.611 0.528 0.444]
Task 30
How to compute the softmax score?
In
[ ]: iris = np.genfromtxt(url, delimiter=',', dtype='object')
sepallength = np.array([float(row[0]) for row in iris]) def
softmax(x):
"""Compute softmax values for each sets of scores in x.
https://stackoverflow.com/questions/34968722/how-to-implement-the-softmaxfunct
e_x = np.exp(x - np.max(x)) return e_x / e_x.sum(axis=0)
print(softmax(sepallength))
[0.002 0.002 0.001 0.001 0.002 0.003 0.001 0.002 0.001 0.002 0.003 0.002
0.002 0.001 0.004 0.004 0.003 0.002 0.004 0.002 0.003 0.002 0.001 0.002
0.002 0.002 0.002 0.002 0.002 0.001 0.002 0.003 0.002 0.003 0.002 0.002
0.003 0.002 0.001 0.002 0.002 0.001 0.001 0.002 0.002 0.002 0.002 0.001
0.003 0.002 0.015 0.008 0.013 0.003 0.009 0.004 0.007 0.002 0.01 0.002
0.002 0.005 0.005 0.006 0.004 0.011 0.004 0.004 0.007 0.004 0.005 0.006
In [ ]:
0.007 0.006 0.008 0.01 0.012 0.011 0.005 0.004 0.003 0.003 0.004 0.005
0.003 0.005 0.011 0.007 0.004 0.003 0.003 0.006 0.004 0.002 0.004 0.004
0.004 0.007 0.002 0.004 0.007 0.004 0.016 0.007 0.009 0.027 0.002 0.02
0.011 0.018 0.009 0.008 0.012 0.004 0.004 0.008 0.009 0.03 0.03 0.005
0.013 0.004 0.03 0.007 0.011 0.018 0.007 0.006 0.008 0.018 0.022 0.037
0.008 0.007 0.006 0.03 0.007 0.008 0.005 0.013 0.011 0.013 0.004 0.012
0.011 0.011 0.007 0.009 0.007 0.005]
Task 31
In [ ]:
Out[ ]: array([4.6 , 7.255])
Task 32
How to insert values at random positions in an array?
In [
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')
]:
np.random.seed(100) iris_2d[np.random.randint(150, size=20),
np.random.randint(4, size=20)] = np.nan print(iris_2d[:10])
Task 33
How to find the position of missing values in numpy array?
Task 34
How to filter a numpy array based on two or more conditions?
Task 35
How to drop rows that contain a missing value from a numpy array?
Task 36
How to find the correlation between two columns of a numpy array?
Out[ ]: 0.8717541573048712
Task 37
How to find if a given array has any null values?
Out[ ]: False
Task 38
How to replace all missing values with 0 in a numpy array?
In [
iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3])
]:
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] =
np.nan iris_2d[np.isnan(iris_2d)] = 0 iris_2d[:4]
Task 39
How to find the count of unique values in a numpy array?
Task 40
How to convert a numeric to a categorical (text) array?
In [
iris = np.genfromtxt(url, delimiter=',', dtype='object') petal_length_bin
]:
= np.digitize(iris[:, 2].astype('float'), [0, 3, 5, 10]) label_map = {1:
'small', 2: 'medium', 3: 'large', 4: np.nan} petal_length_cat =
[label_map[x] for x in petal_length_bin] petal_length_cat[:4]
In [
iris_2d = np.genfromtxt(url, delimiter=',', dtype='object')
]:
sepallength = iris_2d[:,
0].astype('float') petallength = iris_2d[:,
2].astype('float') volume = (np.pi * petallength * (sepallength**2))/3
volume = volume[:, np.newaxis] out = np.hstack([iris_2d, volume])
out[:4]
Task 42
Task 43
How to get the second largest value of an array when grouped by another array?
Task 44
How to sort a 2D array by a column
In print(iris[iris[:,0].argsort()][:20]) [
]:
Task 45
iris = np.genfromtxt(url, delimiter=',', dtype='object') vals,
counts = np.unique(iris[:, 2], return_counts=True)
print(vals[np.argmax(counts)])
b'1.5'
Task 46
How to find the position of the first occurrence of a value greater than a given value?
In iris = np.genfromtxt(url, delimiter=',', dtype='object') np.argwhere(iris[:, [
]: 3].astype(float) > 1.0)[0]
Task 47
How to replace all values greater than a given value to a given cutoff?
In np.set_printoptions(precision=2) [
]: np.random.seed(100) a =
np.random.uniform(1,50, 20) np.clip(a,
a_min=10, a_max=30)
Out[ ]: array([27.63, 14.64, 21.8 , 30. , 10. , 10. , 30. , 30. , 10. ,
29.18, 30. , 11.25, 10.08, 10. , 11.77, 30. , 30. , 10. ,
30. , 14.43])
Task 48
How to get the positions of top n values from a numpy array?
In [
np.random.seed(100) a = np.random.uniform(1,50,
]:
20) np.argpartition(-a, 5)[:5]
Task 49
How to compute the row wise counts of all possible values in an array?
In [
np.random.seed(100) arr =
]:
np.random.randint(1,11,size=(6, 10)) arr
Out[ ]:
array([[ 9, 9, 4, 8, 8, 1, 5, 3, 6, 3],
[ 3, 3, 2, 1, 9, 5, 1, 10, 7, 3],
[ 5, 2, 6, 4, 5, 5, 4, 8, 2, 2],
[ 8, 8, 1, 3, 10, 10, 4, 3, 6, 9],
[ 2, 1, 8, 7, 3, 1, 9, 3, 6, 2],
[ 9, 2, 6, 5, 3, 9, 4, 6, 1, 10]])
Task 50
How to convert an array of arrays into a flat 1d array?
In [
arr1 = np.arange(3) arr2
]:
= np.arange(3,7) arr3 =
np.arange(7,10)
array_of_arrays = np.array([arr1, arr2, arr3], dtype=object)
print('array_of_arrays: ', array_of_arrays) arr_2d =
np.concatenate(array_of_arrays) print(arr_2d)
Task 51
How to generate one-hot encodings for an array in numpy?
In [ ]:
np.random.seed(101) arr =
np.random.randint(1,4, size=6)
(arr[:, None] == np.unique(arr)).view(np.int8)
Task 52
species = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)
np.random.seed(100) species_small =
np.sort(np.random.choice(species, size=20)) print([i for val in
np.unique(species_small) for i, grp in
enumerate(species_small
How to create row numbers grouped by a categorical variable?
In [ ]:
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5]
Task 53
How to create groud ids based on a given categorical variable?
In [ ]:
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]
Task 54
How to rank items in an array using numpy?
In np.random.seed(10) a =
[ ]: np.random.randint(20, size=10)
print('Array: ', a)
print(a.argsort().argsort())
In [ ]:
Array: [ 9 4 15 0 17 16 17 8 9 0] [4 2 6 0
8 7 9 3 5 1]
Task 55
How to rank items in a multidimensional array using numpy?
np.random.seed(10) a =
np.random.randint(20, size=[2,5])
print(a)
a.ravel().argsort().argsort().reshape(a.shape)
[[ 9 4 15 0 17]
[16 17 8 9 0]]
Task 56
How to find the maximum value in each row of a numpy array 2d?
In np.random.seed(100) a =
[ ]: np.random.randint(1,10, [5,3])
np.amax(a, axis=1)
Task 57
How to compute the min-by-max for each row for a numpy array 2d?
In
np.random.seed(100) a = np.random.randint(1,10, [5,3])
[ ]:
np.apply_along_axis(lambda x: np.min(x)/np.max(x), arr=a, axis=1)
Task 58
How to find the duplicate records in a numpy array?
In [ ]:
In np.random.seed(100) a =
[ ]: np.random.randint(0, 5, 10) out
= np.full(a.shape[0], True)
unique_positions = np.unique(a, return_index=True)[1]
out[unique_positions] = False print(out)
[False True False True False False True True True True]
Task 59
How to find the grouped mean in numpy?
np
Task 60
How to convert a PIL image to numpy array?
from io import BytesIO from
PIL import Image import
PIL, requests
URL =
'https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg'
response = requests.get(URL)
I = Image.open(BytesIO(response.content))
I = I.resize([150,150]) arr =
np.asarray(I) arr.shape
In [ ]:
In [ ]:
Task 61
How to drop all missing values from a numpy array?
In
a = np.array([1,2,3,np.nan,5,6,7,np.nan]) a[~np.isnan(a)]
[ ]:
Task 62
How to compute the euclidean distance between two arrays?
In a = np.array([1,2,3,4,5]) b
[ ]: = np.array([4,5,6,7,8])
np.linalg.norm(a-b)
Out[ ]: 6.708203932499369
Task 63
How to find all the local maxima (or peaks) in a 1d array?
In
a = np.array([1, 3, 7, 1, 2, 6, 0, 1]) doublediff
[ ]:
= np.diff(np.sign(np.diff(a))) peak_locations =
np.where(doublediff == -2)[0] + 1 peak_locations
Task 64
How to subtract a 1d array from a 2d array, where each item of 1d array subtracts from respective row?
In a_2d = np.array([[3,3,3],[4,4,4],[5,5,5]])
[ ]: b_1d = np.array([1,2,3]) print(a_2d -
b_1d[:,None])
[[2 2 2]
[2 2 2]
[2 2 2]]
Task 65
How to find the index of n'th repetition of an item in an array
In x = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1,
[ ]: 2]) n = 5 np.where(x ==
1)[0][n-1]
Out[ ]: 8
Task 66
How to convert numpy's datetime64 object to datetime's datetime object?
In [ ]:
Task 67
How to compute the moving average of a numpy array?
In np.random.seed(100)
[ ]: Z = np.random.randint(10, size=10) np.convolve(Z,
np.ones(3)/3, mode='valid')
Task 68
How to create a numpy array sequence given only the starting point, length and the step?
In
length = 10
[ ]:
start = 5 step
= 3
Out[ ]: array([ 5, 8, 11, 14, 17, 20, 23, 26, 29, 32])
Task 69
How to fill in missing dates in an irregular series of numpy dates?