#the_numpy_array_
#size_shape_ndim_dtype
import numpy as np
#1_dtype
# Creating an array of integers with data type 'int16'
arrint16 = np.array([10, 20, 30], dtype=np.int16)
print(arrint16.dtype)
# Output: int16
# Creating an array of floats with data type 'float64'
arrfloat = np.array([1.1, 2.2, 3.3], dtype=np.float64)
print(arrfloat.dtype)
# Output: float64
import numpy as np
#2_size
#Creating a 2D array and calculating its size
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr1.size)
# Output: 6
#Creating a 1D array and calculating its size
arr2 = np.array([10, 20, 30, 40, 50])
print(arr2.size)
# Output: 5
import numpy as np
#3_shape
#Creating a 3D array and checking its shape
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
print(arr3.shape)
# Output: (3, 2, 2)
#Creating a 1D array and checking its shape
arr4 = np.array([1, 2, 3, 4, 5])
print(arr4.shape)
# 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
#Creating a 1D array and checking its number of dimensions
arr6 = np.array([10, 20, 30])
print(arr6.ndim)
# Output: 1
import numpy as np
#create_an_array_filled_with_zeros
arrzeros = np.zeros((2, 3))
print(arrzeros)
# Output:
# [[0. 0. 0.]
# [0. 0. 0.]]
import numpy as np
#array_filled_with_ones
arrones = np.ones((3, 2))
print(arrones)
# Output:
# [[1. 1.]
# [1. 1.]
# [1. 1.]]
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
arr = np.array([[1, 2, 3],
[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]]
#Important array methods
# Reshape
print("Reshape:")
print(arr.reshape(1, 9))
# Ravel
print("\nRavel:")
print(arr.ravel())
# Flatten
print("\nFlatten:")
print(arr.flatten())
# Hstack and Vstack
arr1 = np.array([[10, 11, 12]])
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:")
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Square each element
print(np.square(arr))
# Square root of each element
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. 1.41421356 1.73205081]
[2. 2.23606798 2.44948974]]
[[ 1 4 9]
[16 25 36]]
#np.sum_
import numpy as np
arr = np.array([[1, 2], [3, 4]])
sumresult = np.sum(arr)
print(sumresult)
#output: 10
#np.prod(): Computes the product of array elements over a specified axis.
import numpy as np
arr = np.array([[1, 2], [3, 4]])
prodresult = np.prod(arr)
print(prodresult)
#output: 24
#np.mean(): Computes the arithmetic mean along the specified axis.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
meanresult = np.mean(arr)
print(meanresult)
#output: 3.0
#np.std(): Computes the standard deviation along the specified axis
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
stdresult = np.std(arr)
print(stdresult)
#output: 1.4142135623730951
#np.var(): Computes the variance along the specified axis.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
varresult = np.var(arr)
print(varresult)
#output: 2.0
#np.min(): Returns the minimum value along the specified axis.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
minresult = np.min(arr)
print(minresult)
#output: 1
#np.max(): Returns the maximum value along the specified axis.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
maxresult = np.max(arr)
print(maxresult)
#output:5
#np.argmin(): Returns the indices of the minimum values along the specified axis.
arr= np.array([1, 2, 3, 4, 5])
argminresult = np.argmin(arr)
print(argminresult)
#output: 0
#np.argmax(): Returns the indices of the maximum values along the specified axis.
arr = np.array([1, 2, 3, 4, 5])
argmaxresult = np.argmax(arr)
print(argmaxresult)
#output: 4