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

.. ML Lab 07

This document contains 30 tasks demonstrating various operations in NumPy like creating arrays, extracting elements, reshaping arrays, stacking arrays, applying element-wise operations, aggregating statistics, normalizing data, and computing softmax scores. Each task includes a short description, example code to demonstrate the operation, and sample output. The document serves as a tutorial for common NumPy tasks.

Uploaded by

Shezi Fezi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

.. ML Lab 07

This document contains 30 tasks demonstrating various operations in NumPy like creating arrays, extracting elements, reshaping arrays, stacking arrays, applying element-wise operations, aggregating statistics, normalizing data, and computing softmax scores. Each task includes a short description, example code to demonstrate the operation, and sample output. The document serves as a tutorial for common NumPy tasks.

Uploaded by

Shezi Fezi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

ML LAB LAB # 07

Shehzad fazal 18-SE-76

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)
[ ]:

Out[ ]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Task 3
How to create a boolean array?

In np.array([True, False, True, True, False], dtype=bool)


[ ]:

Out[ ]: array([ True, False, True, True, False])

Task 4
How to extract items that satisfy a given condition from 1D array?

In # extract items divisible by


[ ]: 2 arr = np.arange(10) arr[arr
% 2 == 0]
Out[ ]: array([0, 2, 4, 6, 8])

Task 5
How to replace items that satisfy a condition with another value in numpy array?

arr = np.arange(10) arr[arr


% 2 == 0] = 0 print(arr)

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?

In arr = np.arange(10) np.where(arr


[ ]: % 2 == 0, -99, arr)

Out[ ]: array([-99, 1, -99, 3, -99, 5, -99, 7, -99, 9])

Task 7
How to reshape an array?

In np.arange(9).reshape(3, -1)
[ ]:

Out[ ]: array([[0, 1, 2],


[3, 4, 5],
[6, 7, 8]])

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)

Out[ ]: array([[1, 1, 1],


[1, 1, 1],
[2, 2, 2],
[2, 2, 2]])

Task 9
a = np.repeat(1, 6).reshape(2, -1) b
= np.repeat(2, 6).reshape(2, -1)
np.concatenate([a, b], axis=1)

How to stack two arrays horizontally? In


[ ]:
Out[ ]: array([[1, 1, 1, 2, 2, 2],
[1, 1, 1, 2, 2, 2]])

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)]

Out[ ]: array([1, 1, 0, 0, 1, 0, 1, 0])

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)

Out[ ]: array([ 0, 6, 12, 18])


Task 12
How to remove from one array those items that exist in another?

In a = np.arange(0, 20, 2) b
[ ]: = np.arange(0, 20, 3)
np.setdiff1d(a, b)

Out[ ]: array([ 2, 4, 8, 10, 14, 16])

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 [ ]:

Out[ ]: array([1, 3, 5, 7], dtype=int64)

Task 14
How to extract all numbers between a given range from a numpy array?

In a = np.arange(30) a[(a >=


[ ]: 10) & (a < 20)]

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

pair_min = np.vectorize(minn, otypes=[int])

a = np.array([5, 7, 9, 8, 6, 4, 5]) b
= np.array([6, 3, 4, 8, 9, 7, 1])
pair_min(a, b)

Out[ ]: array([5, 3, 4, 8, 6, 4, 1])

Task 16
How to swap two columns in a 2d numpy array?

In arr = np.arange(16).reshape(4, 4) arr[:,


[ ]: [3, 2, 1, 0]]

Out[ ]: array([[ 3, 2, 1, 0],


[ 7, 6, 5, 4],
[11, 10, 9, 8],
[15, 14, 13, 12]])

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], :]

Out[ ]: array([[12, 13, 14, 15],


[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[ 0, 1, 2, 3]])

Task 18
How to reverse the rows of a 2D array?

In
arr = np.arange(16).reshape(4, 4) arr[::-1]
[ ]:

Out[ ]: array([[12, 13, 14, 15],


[ 8, 9, 10, 11],
[ 4, 5, 6, 7],
[ 0, 1, 2, 3]])

Task 19
How to reverse the coluns of a 2D array?

In arr = np.arange(16).reshape(4, 4) arr[:,


[ ]: ::-1]

Out[ ]: array([[ 3, 2, 1, 0],


[ 7, 6, 5, 4],
[11, 10, 9, 8],
[15, 14, 13, 12]])

Task 20
How to create a 2D array containing random floats between 5 and 10?

In np.random.uniform(5,10, size=(3,3))
[ ]:

Out[ ]: array([[6.05, 7.26, 9.35],


In [ ]:

[5.32, 8.12, 7.62],


[7.81, 5.03, 6.54]])

Task 21
How to print only 3 decimal places in python numpy array?

np.set_printoptions(precision=3)
np.random.random([3,3])

Out[ ]: array([[0.95 , 0.127, 0.079],


[0.311, 0.632, 0.699],
[0.642, 0.92 , 0.299]])

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
[ ]:

Out[ ]: array([[0.001, 0. , 0.001],


[0.001, 0. , 0.001],
[0. , 0. , 0. ]])

Task 23
How to limit the number of items printed in output of numpy array?

In
np.set_printoptions(threshold=6) np.arange(100)
[ ]:

Out[ ]: array([ 0, 1, 2, ..., 97, 98, 99])

Task 24
How to print the full numpy array without truncating

In
np.set_printoptions(threshold=sys.maxsize) np.arange(20)
[ ]:
In [ ]:

Out[ ]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,


16,
17, 18, 19])

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?

In iris_1d = np.genfromtxt(url, delimiter=',', dtype=object)


[ ]: species = np.array([row[4] for row in iris_1d])
species[:5]

Out[ ]: array([b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',


b'Iris-setosa'], dtype='|S15')

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]

Out[ ]: array([[b'5.1', b'3.5', b'1.4', b'0.2'],


[b'4.9', b'3.0', b'1.4', b'0.2'],
[b'4.7', b'3.2', b'1.3', b'0.2'],
[b'4.6', b'3.1', b'1.5', b'0.2']], dtype='|S3')

Task 28
How to compute the mean, median, standard deviation of a numpy array?
In [ ]:

In sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0]) mu,


[ ]: med, sd = np.mean(sepallength), np.median(sepallength), np.std(sepallength)
print(mu, med, sd)

5.843333333333334 5.8 0.8253012917851409

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]

sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])


np.percentile(sepallength, q=[5, 95])

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])

[[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']
[b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']
[b'5.0' b'3.6' b'1.4' b'0.2' b'Iris-setosa']
[b'5.4' b'3.9' b'1.7' b'0.4' b'Iris-setosa']
[b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']
[b'5.0' b'3.4' b'1.5' b'0.2' b'Iris-setosa']
[b'4.4' nan b'1.4' b'0.2' b'Iris-setosa']
[b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']]

Task 33
How to find the position of missing values in 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 print("Position of missing values: \n", np.where(np.isnan(iris_2d[:,
0])))

Position of missing values:


(array([ 38, 80, 106, 113, 121], dtype=int64),)

Task 34
How to filter a numpy array based on two or more conditions?

In iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', [


]: usecols=[0,1,2,3]) condition = (iris_2d[:, 2] > 1.5) & (iris_2d[:, 0] < 5.0)
iris_2d[condition]
Out[ ]: array([[4.8, 3.4, 1.6, 0.2],
[4.8, 3.4, 1.9, 0.2],
[4.7, 3.2, 1.6, 0.2],
[4.8, 3.1, 1.6, 0.2],
[4.9, 2.4, 3.3, 1. ],
[4.9, 2.5, 4.5, 1.7]])

Task 35
How to drop rows that contain a missing value from 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.sum(np.isnan(iris_2d), axis = 1) == 0][:5]

Out[ ]: array([[4.9, 3. , 1.4, 0.2],


[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[4.6, 3.4, 1.4, 0.3]])

Task 36
How to find the correlation between two columns of a numpy array?

In iris = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) [


]: np.corrcoef(iris[:, 0], iris[:, 2])[0, 1]

Out[ ]: 0.8717541573048712

Task 37
How to find if a given array has any null values?

In iris_2d = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0,1,2,3]) [


]: np.isnan(iris_2d).any()

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]

Out[ ]: array([[5.1, 3.5, 1.4, 0.2],


[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2]])

Task 39
How to find the count of unique values in a numpy array?

In iris = np.genfromtxt(url, delimiter=',', dtype='object') species [


]: = np.array([row.tolist()[4] for row in iris]) np.unique(species,
return_counts=True)

Out[ ]: (array([b'Iris-setosa', b'Iris-versicolor',


b'Irisvirginica'], dtype='|S15'), array([50,
50, 50], dtype=int64))

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]

Out[ ]: ['small', 'small', 'small', 'small']


Task 41
How to create a new column from existing columns of a numpy array?

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]

Out[ ]: array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa',


38.13265162927291],
[b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa',
35.200498485922445],
[b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa', 30.0723720777127],
[b'4.6', b'3.1', b'1.5', b'0.2', b'Iris-setosa',
33.238050274980004]], dtype=object)

Task 42

iris = np.genfromtxt(url, delimiter=',', dtype='object') species = iris[:, 4]


np.random.seed(100) probs = np.r_[np.linspace(0, 0.500, num=50),
np.linspace(0.501, .750, num=50), np. index = np.searchsorted(probs,
np.random.random(150)) species_out = species[index] print(np.unique(species_out,
return_counts=True))

How to do probabilistic sampling in numpy?


In [ ]:

(array([b'Iris-setosa', b'Iris-versicolor', b'Iris-virginica'],


dtype=object), array([77, 37, 36], dtype=int64))

Task 43
How to get the second largest value of an array when grouped by another array?

In iris = np.genfromtxt(url, delimiter=',', dtype='object') petal_len_setosa [


]: = iris[iris[:, 4] == b'Iris-setosa', [2]].astype('float')
np.unique(np.sort(petal_len_setosa))[-2]
Out[ ]: 1.7

Task 44
How to sort a 2D array by a column

In print(iris[iris[:,0].argsort()][:20]) [
]:

[[b'4.3' b'3.0' b'1.1' b'0.1' b'Iris-setosa']


[b'4.4' b'3.2' b'1.3' b'0.2' b'Iris-setosa']
[b'4.4' b'3.0' b'1.3' b'0.2' b'Iris-setosa']
[b'4.4' b'2.9' b'1.4' b'0.2' b'Iris-setosa']
[b'4.5' b'2.3' b'1.3' b'0.3' b'Iris-setosa']
[b'4.6' b'3.6' b'1.0' b'0.2' b'Iris-setosa']
[b'4.6' b'3.1' b'1.5' b'0.2' b'Iris-setosa']
[b'4.6' b'3.4' b'1.4' b'0.3' b'Iris-setosa']
[b'4.6' b'3.2' b'1.4' b'0.2' b'Iris-setosa']
[b'4.7' b'3.2' b'1.3' b'0.2' b'Iris-setosa']
[b'4.7' b'3.2' b'1.6' b'0.2' b'Iris-setosa']
[b'4.8' b'3.0' b'1.4' b'0.1' b'Iris-setosa']
[b'4.8' b'3.0' b'1.4' b'0.3' b'Iris-setosa']
[b'4.8' b'3.4' b'1.9' b'0.2' b'Iris-setosa']
[b'4.8' b'3.4' b'1.6' b'0.2' b'Iris-setosa']
[b'4.8' b'3.1' b'1.6' b'0.2' b'Iris-setosa']
[b'4.9' b'2.4' b'3.3' b'1.0' b'Iris-versicolor']
[b'4.9' b'2.5' b'4.5' b'1.7' b'Iris-virginica']
[b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']
[b'4.9' b'3.1' b'1.5' b'0.1' b'Iris-setosa']]

Task 45
iris = np.genfromtxt(url, delimiter=',', dtype='object') vals,
counts = np.unique(iris[:, 2], return_counts=True)
print(vals[np.argmax(counts)])

How to find the most frequent value in a numpy array?


In [ ]:

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]

Out[ ]: array([50], dtype=int64)

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]

Out[ ]: array([15, 10, 3, 7, 18], dtype=int64)

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)

array_of_arrays: [array([0, 1, 2]) array([3, 4, 5, 6]) array([7, 8, 9])] [0 1 2


3 4 5 6 7 8 9]

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)

Out[ ]: array([[0, 1, 0],


[0, 0, 1],
[0, 1, 0],
[0, 1, 0],
[0, 1, 0],
[1, 0, 0]], dtype=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 [ ]:

species = np.genfromtxt(url, delimiter=',', dtype='str', usecols=4)


[ np.random.seed(100) species_small = np.sort(np.random.choice(species, size=20))
output = [np.argwhere(np.unique(species_small) == s).tolist()[0][0] for val in
np.
print(output)

[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]]

Out[ ]: array([[4, 2, 6, 0, 8],


[7, 9, 3, 5, 1]], dtype=int64)

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)

Out[ ]: array([9, 8, 6, 3, 9])

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)

Out[ ]: array([0.44, 0.12, 0.5 , 1. , 0.11])

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]

iris = np.genfromtxt(url, delimiter=',', dtype='object') names =


('sepallength', 'sepalwidth', 'petallength', 'petalwidth',
'species') numeric_column = iris[:, 1].astype('float') #
sepalwidth grouping_column = iris[:, 4] # species
[[group_val, numeric_column[grouping_column==group_val].mean()] for group_val
in

Task 59
How to find the grouped mean in numpy?

np

Out[ ]: [[b'Iris-setosa', 3.418],


[b'Iris-versicolor', 2.7700000000000005], [b'Iris-
virginica', 2.974]]

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 [ ]:

Out[ ]: (150, 150, 3)

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)]
[ ]:

Out[ ]: array([1., 2., 3., 5., 6., 7.])

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

Out[ ]: array([2, 5], dtype=int64)

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?

dt64 = np.datetime64('2018-02-25 22:10:10')


from datetime import datetime dt64.tolist()

In [ ]:

Out[ ]: datetime.datetime(2018, 2, 25, 22, 10, 10)

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')

Out[ ]: array([6.33, 6. , 5.67, 4.67, 3.67, 2. , 3.67, 3. ])

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

def seq(start, length, step): end


= start + (step*length)
return np.arange(start, end, step) seq(start,
length, step)

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?

In [ ]: dates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-25'), 2)


d filled_in = np.array([np.arange(date, (date+d)) for date, d in zip(dates,
np.diff( np.hstack([filled_in, dates[-1]])

Out[ ]: array(['2018-02-01', '2018-02-02', '2018-02-03', '2018-02-04',


'2018-02-05', '2018-02-06', '2018-02-07', '2018-02-08',
'2018-02-09', '2018-02-10', '2018-02-11', '2018-02-12',
'2018-02-13', '2018-02-14', '2018-02-15', '2018-02-16',
'2018-02-17', '2018-02-18', '2018-02-19', '2018-02-20',
'2018-02-21', '2018-02-22', '2018-02-23'], dtype='datetime64[D]')

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy