Array - Colab
Array - Colab
#Creating Arrays
array1=np.array([10,20,30]) # Array of integers
print (array1)
[10 20 30]
[-2 2 6 10 14 18 22]
array3=np.array([[2.4,3],[4.91,7],[0,-1]]) # 2D array
print(array3)
[[ 2.4 3. ]
[ 4.91 7. ]
[ 0. -1. ]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[1. 1.]
[1. 1.]
[1. 1.]]
[0 1 2 3 4 5]
# Reshaping Arrays
array9=np.arange(12).reshape(4,3) # need to specify the number of rows and number of column
print(array9)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
[10 20 30]
1
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
2
# Slicing Arrays
arr=np.array([1,2,3,4,5,6,7,8,9,10,11,12])
arr_slice=arr[5:8]
print(arr_slice)
[6 7 8]
# change values in arr_slice, and observe that the mutations are reflected
arr_slice[1]=12345
print(arr)
[ 1 2 3 4 5 6 12345 8 9 10 11 12]
[ 1 2 3 4 5 64 64 64 9 10 11 12]
# .copy()
# As Numpy has been designed to be able to work with very large arrays,
# Imagine performance and memory problems if Numpy insisted on always copy
# If you want a copy of a slice of an ndarray instead of a view, you will
print(arr)
arr_slice2=arr[5:8]. copy()
print(arr_slice2)
[ 1 2 3 4 5 64 64 64 9 10 11 12]
[64 64 64]
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
5
5
arr
np.dot(arr.T, arr)