Skip to content

Instantly share code, notes, and snippets.

@Createdd
Created February 20, 2020 16:11
Show Gist options
  • Save Createdd/2232fb9017a3b9a2936d5dfdf5ec1ea5 to your computer and use it in GitHub Desktop.
Save Createdd/2232fb9017a3b9a2936d5dfdf5ec1ea5 to your computer and use it in GitHub Desktop.
import numpy as np
one_dim = np.ones(shape=(5), dtype=np.int)
# print(one_dim)
# [1 1 1 1 1]
two_dim = np.ones(shape=(5, 2), dtype=np.int)
# print(two_dim)
# [[1 1]
# [1 1]
# [1 1]
# [1 1]
# [1 1]]
three_dim = np.ones(shape=(3, 2, 1), dtype=np.int)
# print(three_dim)
# [[[1]
# [1]]
# [[1]
# [1]]
# [[1]
# [1]]]
a_range = np.arange(5)
# print(a_range)
# [0 1 2 3 4]
a_sliced_range = np.arange(2, 5)
# print(a_range)
# [2 3 4]
a_range = np.arange(5)
# print(a_range)
# print(a_range[2])
# [0 1 2 3 4]
# 2
a_range = np.arange(5)
# print(a_range)
# print(a_range[2:4])
# [0 1 2 3 4]
# [2 3]
two_dim = np.linspace((0, 5, 10), (4, 9, 14), 5)
# print(two_dim)
# # [[ 0. 5. 10.]
# # [ 1. 6. 11.]
# # [ 2. 7. 12.]
# # [ 3. 8. 13.]
# # [ 4. 9. 14.]]
# print(two_dim[2:4])
# # [[ 2. 7. 12.]
# # [ 3. 8. 13.]]
# print(two_dim[2, 2])
# # 12.
# print(two_dim[:, 2])
# # [10. 11. 12. 13. 14.]
# print(two_dim[2, :])
# # [ 2. 7. 12.]
a_range = np.arange(27)
three_dim = a_range.reshape(3, 3, 3)
# print(three_dim)
# [[[ 0 1 2]
# [ 3 4 5]
# [ 6 7 8]]
# [[ 9 10 11]
# [12 13 14]
# [15 16 17]]
# [[18 19 20]
# [21 22 23]
# [24 25 26]]]
# print(three_dim[0, 1, 2])
# 5
# print(three_dim[0, 1])
# [3 4 5]
# print(three_dim[0][1])
# [3 4 5]
# print(three_dim[:, 2, 1])
# [ 7 16 25]
# print(three_dim[0, :, 0])
# [0 3 6]
# print(three_dim[0, 0, :])
#[0 1 2]
# print(three_dim[0])
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
a_range = np.arange(2*2*2*3)
four_dim = a_range.reshape(2, 2, 2, 3)
# print(four_dim)
# [[[[ 0 1 2]
# [ 3 4 5]]
# [[ 6 7 8]
# [ 9 10 11]]]
# [[[12 13 14]
# [15 16 17]]
# [[18 19 20]
# [21 22 23]]]]
# print(four_dim[:, 0, 0, 1])
# [ 1 13]
a_range = np.arange(6)
new_shape = (3, 2, 1)
a_range = np.reshape(a_range, new_shape)
# print(a_range)
# [[[0]
# [1]]
# [[2]
# [3]]
# [[4]
# [5]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment