Numpy exercises

Numpy exercises

# impoprt numpy
import numpy as np
# Create an arbitrary one dimensional array called x
x = np.array([1,4,0,10,8])
# Create a new array which consists of the even indices of x you created before
y = x[::2]
y
array([1, 0, 8])
# Create a new array in backwards ordering from x
z = x[::-1]
z
array([ 8, 10,  0,  4,  1])
x = np.array([3, 10, 1, 9, 21, 13])
y = x[2:5]
y[0] = 0
print(x[2])
0
#5) Create a two dimensional array called x
m = np.random.randn(3,4)
m
array([[-0.48543923, -0.46328568, -0.82910729,  0.67650504],
       [-2.59927498, -0.44642341,  0.12432377, -0.34605895],
       [-0.73963889, -1.40607813, -0.72125986,  1.52833794]])
# Create a new array from m, in which the elements of each row are in reverse order.
n = m[:,::-1]
n
array([[ 0.67650504, -0.82910729, -0.46328568, -0.48543923],
       [-0.34605895,  0.12432377, -0.44642341, -2.59927498],
       [ 1.52833794, -0.72125986, -1.40607813, -0.73963889]])
# Another one, where the rows are in reverse order.
k = m[::-1,:]
k
array([[-0.73963889, -1.40607813, -0.72125986,  1.52833794],
       [-2.59927498, -0.44642341,  0.12432377, -0.34605895],
       [-0.48543923, -0.46328568, -0.82910729,  0.67650504]])
# Create an array from m, where columns and rows are in reverse order.
l = m[::-1,::-1]
l
array([[ 1.52833794, -0.72125986, -1.40607813, -0.73963889],
       [-0.34605895,  0.12432377, -0.44642341, -2.59927498],
       [ 0.67650504, -0.82910729, -0.46328568, -0.48543923]])
# Cut of the first and last row and the first and last column.
m[1:-1,1:-1]
array([[-0.44642341,  0.12432377]])