rougier / numpy-100

100 numpy exercises (with solutions)
MIT License
12.22k stars 5.76k forks source link

Alternative solutions for Exercises N36, N81 #214

Open Artur-Arstamyan opened 1 month ago

Artur-Arstamyan commented 1 month ago

Exercise N36 - Extract the integer part of a random array of positive numbers using 4 different methods
np.modf() returns two arrays: one with the fractional part and one with the integer part.

import numpy as np
Z = np.random.uniform(0,10,10)
print(np.modf(Z)[1])

Exercise N81 - Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? Using np.roll function

import numpy as np
Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
print(np.array([np.roll(Z, -i)[:4] for i in range(len(Z)-3)]))
rougier commented 1 month ago

Thanks.