#The Numpy Array 20240306 131018 0000
#The Numpy Array 20240306 131018 0000
#size_shape_ndim_dtype
import numpy as np
#1_dtype
print(arrint16.dtype)
# Output: int16
# Output: float64
import numpy as np
#2_size
# Output: 6
# Output: 5
import numpy as np
#3_shape
# Output: (3, 2, 2)
# Output: (5,)
import numpy as np
#4_ndim
#Creating a 2D array and checking its number of dimensions
arr5 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr5.ndim)
# Output: 2
# Output: 1
import numpy as np
#create_an_array_filled_with_zeros
import numpy as np
#array_filled_with_ones
import numpy as np
#array_filled_with_specified_value
arrfull = np.full((2, 2), 5)
print(arrfull)
# Output:
# [[5 5]
# [5 5]]
import numpy as np
#array_with_range_of_values
arrarange = np.arange(1, 10, 2)
print(arrarange)
# Output: [1 3 5 7 9]
import numpy as np
#array with evenly spaced value
arrlinspace = np.linspace(0, 10, 5)
print(arrlinspace)
# Output: [ 0. 2.5 5. 7.5 10. ]
import numpy as np
#array with values spaced evenly on a logarithmic scale
arrlogspace = np.logspace(1, 3, 4)
print(arrlogspace)
# Output: [ 10. 46.4 215.4 1000. ]
import numpy as np
#square identity matrix
arridentity = np.identity(3)
print(arridentity)
# Output:
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]
import numpy as np
#2D array with ones in the diagonal
arreye = np.eye(3, 4)
print(arreye)
# Output:
# [[1. 0. 0. 0.]
# [0. 1. 0. 0.]
# [0. 0. 1. 0.]]
import numpy as np
#array into specific data type
arr = np.array([1, 2, 3])
arrnew = arr.astype(float)
print(arrnew)
# Output: [1. 2. 3.]
import numpy as np
import numpy as np
# Creating a sample array
[4, 5,6],
[7, 8,9]])
# Indexing
print("Indexing:")
print(arr[0, 0])
print(arr[1])
print(arr[:, 1])
# Slicing
print("\nSlicing:")
print(arr[0:2, 1:])
#output:
#Indexing:
#[4 5 6]
#[2 5 8]
#Slicing:
#[[2 3]
# [5 6]]
# Reshape
print("Reshape:")
print(arr.reshape(1, 9))
# Ravel
print("\nRavel:")
print(arr.ravel())
# Flatten
print("\nFlatten:")
print(arr.flatten())
# Hstack and Vstack
print("\nHstack:")
print(np.hstack((arr, arr1)))
arr2 = np.array([[13],
[14],
[15]])
print("\nVstack:")
print(np.vstack((arr, arr2)))
#output:
#Reshape:
[[1 2 3 4 5 6 7 8 9]]
#Ravel:
[1 2 3 4 5 6 7 8 9]
#Flatten:
[1 2 3 4 5 6 7 8 9]
#Hstack:
[[ 1 2 3 10 11 12]]
#Vstack:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[13 14 15]]
# Element-wise functions
print("\nElement-wise Functions:")
[4, 5, 6]])
print(np.square(arr))
print(np.sqrt(arr))
# Using vectorize
vec_square = np.vectorize(lambda x: x ** 2)
print(vec_square(arr))
#output:
#Element-wise Functions:
[[ 1 4 9]
[16 25 36]]
[[ 1 4 9]
[16 25 36]]
#np.sum_
import numpy as np
sumresult = np.sum(arr)
print(sumresult)
#output: 10
import numpy as np
prodresult = np.prod(arr)
print(prodresult)
#output: 24
import numpy as np
meanresult = np.mean(arr)
print(meanresult)
#output: 3.0
import numpy as np
stdresult = np.std(arr)
print(stdresult)
#output: 1.4142135623730951
varresult = np.var(arr)
print(varresult)
#output: 2.0
import numpy as np
minresult = np.min(arr)
print(minresult)
#output: 1
import numpy as np
maxresult = np.max(arr)
print(maxresult)
#output:5
#np.argmin(): Returns the indices of the minimum values along the specified axis.
argminresult = np.argmin(arr)
print(argminresult)
#output: 0
#np.argmax(): Returns the indices of the maximum values along the specified axis.
argmaxresult = np.argmax(arr)
print(argmaxresult)
#output: 4