-
Notifications
You must be signed in to change notification settings - Fork 42
100 narray exercises
This is narray version of 100 numpy exercises (Repository)
-
Import the numpy package under the name
np
(★☆☆)Python:
import numpy as np
Ruby:
require "numo/narray"
-
Print the numpy version and the configuration (★☆☆)
Python:
print(np.__version__) np.show_config()
Ruby:
-
Create a null vector of size 10 (★☆☆)
Python:
Z = np.zeros(10) print(Z)
Ruby:
p Numo::Int32.zeros(10) p Numo::Float64.zeros(10) p Numo::NArray[*[0]*10]
-
How to get the documentation of the numpy add function from the command line ? (★☆☆)
Python:
python -c "import numpy; numpy.info(numpy.add)"
Ruby:
ri 'Numo::DFloat#+'
-
Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
Python:
Z = np.zeros(10) Z[4] = 1 print(Z)
Ruby:
z = Numo::Float64.zeros(10) z[4] = 1 p z
-
Create a vector with values ranging from 10 to 49 (★☆☆)
Python:
Z = np.arange(10,50) print(Z)
Ruby:
p Numo::Int32[10...50] p Numo::Int32[10..49] p Numo::Int32.new(40).seq(10)
-
Reverse a vector (first element becomes last) (★☆☆)
Python:
Z = np.arange(50) Z = Z[::-1]
Ruby:
Numo::Int32.new(50).seq.reverse
-
Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
Python:
Z = np.arange(9).reshape(3,3) print(Z)
Ruby:
Numo::Int32.new(3,3).seq
-
Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
Python:
nz = np.nonzero([1,2,0,0,4,0]) print(nz)
Ruby:
p Numo::NArray[1,2,0,0,4,0].ne(0).where
-
Create a 3x3 identity matrix (★☆☆)
Python:
Z = np.eye(3) print(Z)
Ruby:
p Numo::Float64.eye(3)
-
Create a 3x3x3 array with random values (★☆☆)
Python:
Z = np.random.random((3,3,3)) print(Z)
Ruby:
p Numo::Float64.new(3,3,3).rand
-
Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
Python:
Z = np.random.random((10,10)) Zmin, Zmax = Z.min(), Z.max() print(Zmin, Zmax)
Ruby:
p Numo::Float64.new(3,3,3).rand.minmax
-
Create a random vector of size 30 and find the mean value (★☆☆)
Python:
Z = np.random.random(30) m = Z.mean() print(m)
Ruby:
p Numo::Float64.new(30).rand.mean
-
Create a 2d array with 1 on the border and 0 inside (★☆☆)
Python:
Z = np.ones((10,10)) Z[1:-1,1:-1] = 0
Ruby:
z = Numo::Float64.ones(10,10) z[1..-2,1..-2] = 0 p z
-
What is the result of the following expression ? (★☆☆)
Python:
0 * np.nan np.nan == np.nan np.inf > np.nan np.nan - np.nan 0.3 == 3 * 0.1
Ruby:
0 * Float::NAN Float::NAN == Float::NAN Float::INFINITY > Float::NAN Float::NAN - Float::NAN 0.3 == 3 * 0.1
-
Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
Python:
Z = np.diag(1+np.arange(4),k=-1) print(Z)
Ruby:
z = Numo::Int32.zeros(5,5) z.diagonal(-1)[] = Numo::Int32[1..4] p z
-
Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
Python:
Z = np.zeros((8,8),dtype=int) Z[1::2,::2] = 1 Z[::2,1::2] = 1 print(Z)
Ruby:
x = Numo::Int32.new(1,8).seq y = Numo::Int32.new(8,1).seq z = (x+y)%2 p z
-
Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element ?
Python:
print(np.unravel_index(100,(6,7,8)))
Ruby:
# NArray allows unraveled index access z = Numo::Int32.new(6,7,8).seq p z[100]
-
Create a checkerboard 8x8 matrix using the tile function (★☆☆)
Python:
Z = np.tile( np.array([[0,1],[1,0]]), (4,4)) print(Z)
Ruby:
TBD
-
Normalize a 5x5 random matrix (★☆☆)
Python:
Z = np.random.random((5,5)) Zmax, Zmin = Z.max(), Z.min() Z = (Z - Zmin)/(Zmax - Zmin) print(Z)
Ruby:
z = Numo::Float64.new(5,5).rand zmin,zmax = z.minmax z = (z - zmin)/(zmax - zmin) p z
-
Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
Python:
Z = np.dot(np.ones((5,3)), np.ones((3,2))) print(Z)
Ruby:
x = Numo::DFloat.ones(5,3) y = Numo::DFloat.ones(3,2) z = x.dot y p z
-
Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
Python:
# Author: Evgeni Burovski Z = np.arange(11) Z[(3 < Z) & (Z <= 8)] *= -1
Ruby:
z = Numo::Int32.new(11).seq z[(3<z).and(z<=8)] *= -1
-
Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
Python:
Z = np.zeros((5,5)) Z += np.arange(5) print(Z)
Ruby:
z = Numo::Float64.zeros(5,5) z += Numo::Int32.new(5).seq p z