Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

numpy creating arrays

import numpy as np

# create numpy array
arr = np.array([1, 2, 3, 4, 5])
print(type(arr))

# 1d-array
arr = np.array(42)
print(type(arr))

# 2d-array
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

# 3d-array
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)

# copy an array any changes make to the copy of the array does not affect the original
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42

print(arr)
# output [42  2  3  4  5]

print(x)
# output [1 2 3 4 5]

# view an array any changes made to the view will affect the original array
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42

print(arr)
# output [42  2  3  4  5]

print(x)
# output [42  2  3  4  5]
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #numpy #creating #arrays
ADD COMMENT
Topic
Name
4+7 =