Description
stringlengths 9
105
| Link
stringlengths 45
135
| Code
stringlengths 10
26.8k
| Test_Case
stringlengths 9
202
| Merge
stringlengths 63
27k
|
---|---|---|---|---|
Find the most frequent value in a NumPy array
|
https://www.geeksforgeeks.org/find-the-most-frequent-value-in-a-numpy-array/
|
import numpy as np
# create array
x = np.array([1, 2, 3, 4, 5, 1, 2, 1, 1, 1])
print("Original array:")
print(x)
print("Most frequent value in the above array:")
print(np.bincount(x).argmax())
|
#Output : 1
|
Find the most frequent value in a NumPy array
import numpy as np
# create array
x = np.array([1, 2, 3, 4, 5, 1, 2, 1, 1, 1])
print("Original array:")
print(x)
print("Most frequent value in the above array:")
print(np.bincount(x).argmax())
#Output : 1
[END]
|
Find the most frequent value in a NumPy array
|
https://www.geeksforgeeks.org/find-the-most-frequent-value-in-a-numpy-array/
|
import numpy as np
x = np.array(
[
1,
1,
1,
2,
3,
4,
2,
4,
3,
3,
]
)
print("Original array:")
print(x)
print("Most frequent value in above array")
y = np.bincount(x)
maximum = max(y)
for i in range(len(y)):
if y[i] == maximum:
print(i, end=" ")
|
#Output : 1
|
Find the most frequent value in a NumPy array
import numpy as np
x = np.array(
[
1,
1,
1,
2,
3,
4,
2,
4,
3,
3,
]
)
print("Original array:")
print(x)
print("Most frequent value in above array")
y = np.bincount(x)
maximum = max(y)
for i in range(len(y)):
if y[i] == maximum:
print(i, end=" ")
#Output : 1
[END]
|
Combining a one and a two-dimensional NumPy Array
|
https://www.geeksforgeeks.org/combining-a-one-and-a-two-dimensional-numpy-array/
|
# importing Numpy package
import numpy as np
num_1d = np.arange(5)
print("One dimensional array:")
print(num_1d)
num_2d = np.arange(10).reshape(2, 5)
print("\nTwo dimensional array:")
print(num_2d)
# Combine 1-D and 2-D arrays and display
# their elements using numpy.nditer()
for a, b in np.nditer([num_1d, num_2d]):
print(
"%d:%d" % (a, b),
)
|
#Output : One dimensional array:
|
Combining a one and a two-dimensional NumPy Array
# importing Numpy package
import numpy as np
num_1d = np.arange(5)
print("One dimensional array:")
print(num_1d)
num_2d = np.arange(10).reshape(2, 5)
print("\nTwo dimensional array:")
print(num_2d)
# Combine 1-D and 2-D arrays and display
# their elements using numpy.nditer()
for a, b in np.nditer([num_1d, num_2d]):
print(
"%d:%d" % (a, b),
)
#Output : One dimensional array:
[END]
|
Combining a one and a two-dimensional NumPy Array
|
https://www.geeksforgeeks.org/combining-a-one-and-a-two-dimensional-numpy-array/
|
# importing Numpy package
import numpy as np
num_1d = np.arange(7)
print("One dimensional array:")
print(num_1d)
num_2d = np.arange(21).reshape(3, 7)
print("\nTwo dimensional array:")
print(num_2d)
# Combine 1-D and 2-D arrays and display
# their elements using numpy.nditer()
for a, b in np.nditer([num_1d, num_2d]):
print(
"%d:%d" % (a, b),
)
|
#Output : One dimensional array:
|
Combining a one and a two-dimensional NumPy Array
# importing Numpy package
import numpy as np
num_1d = np.arange(7)
print("One dimensional array:")
print(num_1d)
num_2d = np.arange(21).reshape(3, 7)
print("\nTwo dimensional array:")
print(num_2d)
# Combine 1-D and 2-D arrays and display
# their elements using numpy.nditer()
for a, b in np.nditer([num_1d, num_2d]):
print(
"%d:%d" % (a, b),
)
#Output : One dimensional array:
[END]
|
Combining a one and a two-dimensional NumPy Array
|
https://www.geeksforgeeks.org/combining-a-one-and-a-two-dimensional-numpy-array/
|
# importing Numpy package
import numpy as np
num_1d = np.arange(2)
print("One dimensional array:")
print(num_1d)
num_2d = np.arange(12).reshape(6, 2)
print("\nTwo dimensional array:")
print(num_2d)
# Combine 1-D and 2-D arrays and display
# their elements using numpy.nditer()
for a, b in np.nditer([num_1d, num_2d]):
print(
"%d:%d" % (a, b),
)
|
#Output : One dimensional array:
|
Combining a one and a two-dimensional NumPy Array
# importing Numpy package
import numpy as np
num_1d = np.arange(2)
print("One dimensional array:")
print(num_1d)
num_2d = np.arange(12).reshape(6, 2)
print("\nTwo dimensional array:")
print(num_2d)
# Combine 1-D and 2-D arrays and display
# their elements using numpy.nditer()
for a, b in np.nditer([num_1d, num_2d]):
print(
"%d:%d" % (a, b),
)
#Output : One dimensional array:
[END]
|
How to build an array of all combinations of two NumPy arrays?
|
https://www.geeksforgeeks.org/how-to-build-an-array-of-all-combinations-of-two-numpy-arrays/
|
# importing Numpy package
import numpy as np
# creating 2 numpy arrays
array_1 = np.array([1, 2])
array_2 = np.array([4, 6])
print("Array-1")
print(array_1)
print("\nArray-2")
print(array_2)
# combination of elements of array_1 and array_2
# using numpy.meshgrid().T.reshape()
comb_array = np.array(np.meshgrid(array_1, array_2)).T.reshape(-1, 2)
print("\nCombine array:")
print(comb_array)
|
#Output : numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
|
How to build an array of all combinations of two NumPy arrays?
# importing Numpy package
import numpy as np
# creating 2 numpy arrays
array_1 = np.array([1, 2])
array_2 = np.array([4, 6])
print("Array-1")
print(array_1)
print("\nArray-2")
print(array_2)
# combination of elements of array_1 and array_2
# using numpy.meshgrid().T.reshape()
comb_array = np.array(np.meshgrid(array_1, array_2)).T.reshape(-1, 2)
print("\nCombine array:")
print(comb_array)
#Output : numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
[END]
|
How to build an array of all combinations of two NumPy arrays?
|
https://www.geeksforgeeks.org/how-to-build-an-array-of-all-combinations-of-two-numpy-arrays/
|
# importing Numpy package
import numpy as np
# creating 3 numpy arrays
array_1 = np.array([1, 2, 3])
array_2 = np.array([4, 6, 4])
array_3 = np.array([3, 6])
print("Array-1")
print(array_1)
print("Array-2")
print(array_2)
print("Array-3")
print(array_3)
# combination of elements of array_1,
# array_2 and array_3 using
# numpy.meshgrid().T.reshape()
comb_array = np.array(np.meshgrid(array_1, array_2, array_3)).T.reshape(-1, 3)
print("\nCombine array:")
print(comb_array)
|
#Output : numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
|
How to build an array of all combinations of two NumPy arrays?
# importing Numpy package
import numpy as np
# creating 3 numpy arrays
array_1 = np.array([1, 2, 3])
array_2 = np.array([4, 6, 4])
array_3 = np.array([3, 6])
print("Array-1")
print(array_1)
print("Array-2")
print(array_2)
print("Array-3")
print(array_3)
# combination of elements of array_1,
# array_2 and array_3 using
# numpy.meshgrid().T.reshape()
comb_array = np.array(np.meshgrid(array_1, array_2, array_3)).T.reshape(-1, 3)
print("\nCombine array:")
print(comb_array)
#Output : numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
[END]
|
How to build an array of all combinations of two NumPy arrays?
|
https://www.geeksforgeeks.org/how-to-build-an-array-of-all-combinations-of-two-numpy-arrays/
|
# importing Numpy package
import numpy as np
# creating 4 numpy arrays
array_1 = np.array([50, 21])
array_2 = np.array([4, 4])
array_3 = np.array([1, 10])
array_4 = np.array([7, 14])
print("Array-1")
print(array_1)
print("Array-2")
print(array_2)
print("Array-3")
print(array_3)
print("Array-4")
print(array_4)
# combination of elements of array_1,
# array_2, array_3 and array_4
# using numpy.meshgrid().T.reshape()
comb_array = np.array(np.meshgrid(array_1, array_2, array_3, array_4)).T.reshape(-1, 4)
print("\nCombine array:")
print(comb_array)
|
#Output : numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
|
How to build an array of all combinations of two NumPy arrays?
# importing Numpy package
import numpy as np
# creating 4 numpy arrays
array_1 = np.array([50, 21])
array_2 = np.array([4, 4])
array_3 = np.array([1, 10])
array_4 = np.array([7, 14])
print("Array-1")
print(array_1)
print("Array-2")
print(array_2)
print("Array-3")
print(array_3)
print("Array-4")
print(array_4)
# combination of elements of array_1,
# array_2, array_3 and array_4
# using numpy.meshgrid().T.reshape()
comb_array = np.array(np.meshgrid(array_1, array_2, array_3, array_4)).T.reshape(-1, 4)
print("\nCombine array:")
print(comb_array)
#Output : numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
[END]
|
How to add a border around a NumPy array?
|
https://www.geeksforgeeks.org/how-to-add-a-border-around-a-numpy-array/
|
# importing Numpy package
import numpy as np
# Creating a 2X2 Numpy matrix
array = np.ones((2, 2))
print("Original array")
print(array)
print("\n0 on the border and 1 inside the array")
# constructing border of 0 around 2D identity matrix
# using np.pad()
array = np.pad(array, pad_width=1, mode="constant", constant_values=0)
print(array)
|
#Output : numpy.pad(array, pad_width, mode='constant', **kwargs)
|
How to add a border around a NumPy array?
# importing Numpy package
import numpy as np
# Creating a 2X2 Numpy matrix
array = np.ones((2, 2))
print("Original array")
print(array)
print("\n0 on the border and 1 inside the array")
# constructing border of 0 around 2D identity matrix
# using np.pad()
array = np.pad(array, pad_width=1, mode="constant", constant_values=0)
print(array)
#Output : numpy.pad(array, pad_width, mode='constant', **kwargs)
[END]
|
How to add a border around a NumPy array?
|
https://www.geeksforgeeks.org/how-to-add-a-border-around-a-numpy-array/
|
# importing Numpy package
import numpy as np
# Creating a 3X3 Numpy matrix
array = np.ones((3, 3))
print("Original array")
print(array)
print("\n0 on the border and 1 inside the array")
# constructing border of 0 around 3D identity matrix
# using np.pad()
array = np.pad(array, pad_width=1, mode="constant", constant_values=0)
print(array)
|
#Output : numpy.pad(array, pad_width, mode='constant', **kwargs)
|
How to add a border around a NumPy array?
# importing Numpy package
import numpy as np
# Creating a 3X3 Numpy matrix
array = np.ones((3, 3))
print("Original array")
print(array)
print("\n0 on the border and 1 inside the array")
# constructing border of 0 around 3D identity matrix
# using np.pad()
array = np.pad(array, pad_width=1, mode="constant", constant_values=0)
print(array)
#Output : numpy.pad(array, pad_width, mode='constant', **kwargs)
[END]
|
How to add a border around a NumPy array?
|
https://www.geeksforgeeks.org/how-to-add-a-border-around-a-numpy-array/
|
# importing Numpy package
import numpy as np
# Creating a 4X4 Numpy matrix
array = np.ones((4, 4))
print("Original array")
print(array)
print("\n0 on the border and 1 inside the array")
# constructing border of 0 around 4D identity matrix
# using np.pad()
array = np.pad(array, pad_width=1, mode="constant", constant_values=0)
print(array)
|
#Output : numpy.pad(array, pad_width, mode='constant', **kwargs)
|
How to add a border around a NumPy array?
# importing Numpy package
import numpy as np
# Creating a 4X4 Numpy matrix
array = np.ones((4, 4))
print("Original array")
print(array)
print("\n0 on the border and 1 inside the array")
# constructing border of 0 around 4D identity matrix
# using np.pad()
array = np.pad(array, pad_width=1, mode="constant", constant_values=0)
print(array)
#Output : numpy.pad(array, pad_width, mode='constant', **kwargs)
[END]
|
How to compare two NumPy arrays?
|
https://www.geeksforgeeks.org/how-to-compare-two-numpy-arrays/
|
import numpy as np
an_array = np.array([[1, 2], [3, 4]])
another_array = np.array([[1, 2], [3, 4]])
comparison = an_array == another_array
equal_arrays = comparison.all()
print(equal_arrays)
|
#Output : True
|
How to compare two NumPy arrays?
import numpy as np
an_array = np.array([[1, 2], [3, 4]])
another_array = np.array([[1, 2], [3, 4]])
comparison = an_array == another_array
equal_arrays = comparison.all()
print(equal_arrays)
#Output : True
[END]
|
How to compare two NumPy arrays?
|
https://www.geeksforgeeks.org/how-to-compare-two-numpy-arrays/
|
import numpy as np
a = np.array([101, 99, 87])
b = np.array([897, 97, 111])
print("Array a: ", a)
print("Array b: ", b)
print("a > b")
print(np.greater(a, b))
print("a >= b")
print(np.greater_equal(a, b))
print("a < b")
print(np.less(a, b))
print("a <= b")
print(np.less_equal(a, b))
|
#Output : True
|
How to compare two NumPy arrays?
import numpy as np
a = np.array([101, 99, 87])
b = np.array([897, 97, 111])
print("Array a: ", a)
print("Array b: ", b)
print("a > b")
print(np.greater(a, b))
print("a >= b")
print(np.greater_equal(a, b))
print("a < b")
print(np.less(a, b))
print("a <= b")
print(np.less_equal(a, b))
#Output : True
[END]
|
How to compare two NumPy arrays?
|
https://www.geeksforgeeks.org/how-to-compare-two-numpy-arrays/
|
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 2], [3, 4]])
# Comparing the arrays
if np.array_equal(arr1, arr2):
print("Equal")
else:
print("Not Equal")
|
#Output : True
|
How to compare two NumPy arrays?
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 2], [3, 4]])
# Comparing the arrays
if np.array_equal(arr1, arr2):
print("Equal")
else:
print("Not Equal")
#Output : True
[END]
|
How to check whether specified values are present in NumPy array?
|
https://www.geeksforgeeks.org/how-to-check-whether-specified-values-are-present-in-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a Numpy array
n_array = np.array([[2, 3, 0], [4, 1, 6]])
print("Given array:")
print(n_array)
# Checking whether specific values
# are present in "n_array" or not
print(2 in n_array)
print(0 in n_array)
print(6 in n_array)
print(50 in n_array)
print(10 in n_array)
|
#Output : Given array:
|
How to check whether specified values are present in NumPy array?
# importing Numpy package
import numpy as np
# creating a Numpy array
n_array = np.array([[2, 3, 0], [4, 1, 6]])
print("Given array:")
print(n_array)
# Checking whether specific values
# are present in "n_array" or not
print(2 in n_array)
print(0 in n_array)
print(6 in n_array)
print(50 in n_array)
print(10 in n_array)
#Output : Given array:
[END]
|
How to check whether specified values are present in NumPy array?
|
https://www.geeksforgeeks.org/how-to-check-whether-specified-values-are-present-in-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a Numpy array
n_array = np.array([[2.14, 3, 0.5], [4.5, 1.2, 6.2], [20.2, 5.9, 8.8]])
print("Given array:")
print(n_array)
# Checking whether specific values
# are present in "n_array" or not
print(2.14 in n_array)
print(5.28 in n_array)
print(6.2 in n_array)
print(5.9 in n_array)
print(8.5 in n_array)
|
#Output : Given array:
|
How to check whether specified values are present in NumPy array?
# importing Numpy package
import numpy as np
# creating a Numpy array
n_array = np.array([[2.14, 3, 0.5], [4.5, 1.2, 6.2], [20.2, 5.9, 8.8]])
print("Given array:")
print(n_array)
# Checking whether specific values
# are present in "n_array" or not
print(2.14 in n_array)
print(5.28 in n_array)
print(6.2 in n_array)
print(5.9 in n_array)
print(8.5 in n_array)
#Output : Given array:
[END]
|
How to check whether specified values are present in NumPy array?
|
https://www.geeksforgeeks.org/how-to-check-whether-specified-values-are-present-in-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a Numpy array
n_array = np.array(
[
[4, 5.5, 7, 6.9, 10],
[7.1, 5.3, 40, 8.8, 1],
[4.4, 9.3, 6, 2.2, 11],
[7.1, 4, 5, 9, 10.5],
]
)
print("Given array:")
print(n_array)
# Checking whether specific values
# are present in "n_array" or not
print(2.14 in n_array)
print(5.28 in n_array)
print(8.5 in n_array)
|
#Output : Given array:
|
How to check whether specified values are present in NumPy array?
# importing Numpy package
import numpy as np
# creating a Numpy array
n_array = np.array(
[
[4, 5.5, 7, 6.9, 10],
[7.1, 5.3, 40, 8.8, 1],
[4.4, 9.3, 6, 2.2, 11],
[7.1, 4, 5, 9, 10.5],
]
)
print("Given array:")
print(n_array)
# Checking whether specific values
# are present in "n_array" or not
print(2.14 in n_array)
print(5.28 in n_array)
print(8.5 in n_array)
#Output : Given array:
[END]
|
How to get all 2D diagonals of a 3D NumPy array?
|
https://www.geeksforgeeks.org/how-to-get-all-2d-diagonals-of-a-3d-numpy-array/
|
# Import the numpy package
import numpy as np
# Create 3D-numpy array
# of 4 rows and 4 columns
arr = np.arange(3 * 4 * 4).reshape(3, 4, 4)
print("Original 3d array:\n", arr)
# Create 2D diagonal array
diag_arr = np.diagonal(arr, axis1=1, axis2=2)
print("2d diagonal array:\n", diag_arr)
|
#Output : Original 3d array:
|
How to get all 2D diagonals of a 3D NumPy array?
# Import the numpy package
import numpy as np
# Create 3D-numpy array
# of 4 rows and 4 columns
arr = np.arange(3 * 4 * 4).reshape(3, 4, 4)
print("Original 3d array:\n", arr)
# Create 2D diagonal array
diag_arr = np.diagonal(arr, axis1=1, axis2=2)
print("2d diagonal array:\n", diag_arr)
#Output : Original 3d array:
[END]
|
How to get all 2D diagonals of a 3D NumPy array?
|
https://www.geeksforgeeks.org/how-to-get-all-2d-diagonals-of-a-3d-numpy-array/
|
# Import the numpy package
import numpy as np
# Create 3D numpy array
# of 3 rows and 4 columns
arr = np.arange(3 * 3 * 4).reshape(3, 3, 4)
print("Original 3d array:\n", arr)
# Create 2D diagonal array
diag_arr = np.diagonal(arr, axis1=1, axis2=2)
print("2d diagonal array:\n", diag_arr)
|
#Output : Original 3d array:
|
How to get all 2D diagonals of a 3D NumPy array?
# Import the numpy package
import numpy as np
# Create 3D numpy array
# of 3 rows and 4 columns
arr = np.arange(3 * 3 * 4).reshape(3, 3, 4)
print("Original 3d array:\n", arr)
# Create 2D diagonal array
diag_arr = np.diagonal(arr, axis1=1, axis2=2)
print("2d diagonal array:\n", diag_arr)
#Output : Original 3d array:
[END]
|
How to get all 2D diagonals of a 3D NumPy array?
|
https://www.geeksforgeeks.org/how-to-get-all-2d-diagonals-of-a-3d-numpy-array/
|
# Import the numpy package
import numpy as np
# Create 3D numpy array
# of 5 rows and 6 columns
arr = np.arange(3 * 5 * 6).reshape(3, 5, 6)
print("Original 3d array:\n", arr)
# Create 2D diagonal array
diag_arr = np.diagonal(arr, axis1=1, axis2=2)
print("2d diagonal array:\n", diag_arr)
|
#Output : Original 3d array:
|
How to get all 2D diagonals of a 3D NumPy array?
# Import the numpy package
import numpy as np
# Create 3D numpy array
# of 5 rows and 6 columns
arr = np.arange(3 * 5 * 6).reshape(3, 5, 6)
print("Original 3d array:\n", arr)
# Create 2D diagonal array
diag_arr = np.diagonal(arr, axis1=1, axis2=2)
print("2d diagonal array:\n", diag_arr)
#Output : Original 3d array:
[END]
|
Flatten a Matrix Rowix in Python using NumPy
|
https://www.geeksforgeeks.org/flatten-a-matrix-in-python-using-numpy/
|
# importing numpy as np
import numpy as np
# declare matrix with np
gfg = np.array([[2, 3], [4, 5]])
# using array.flatten() method
flat_gfg = gfg.flatten()
print(flat_gfg)
|
#Output : [2 3 4 5]
|
Flatten a Matrix Rowix in Python using NumPy
# importing numpy as np
import numpy as np
# declare matrix with np
gfg = np.array([[2, 3], [4, 5]])
# using array.flatten() method
flat_gfg = gfg.flatten()
print(flat_gfg)
#Output : [2 3 4 5]
[END]
|
Flatten a Matrix Rowix in Python using NumPy
|
https://www.geeksforgeeks.org/flatten-a-matrix-in-python-using-numpy/
|
# importing numpy as np
import numpy as np
# declare matrix with np
gfg = np.array([[6, 9], [8, 5], [18, 21]])
# using array.flatten() method
gfg.flatten()
|
#Output : [2 3 4 5]
|
Flatten a Matrix Rowix in Python using NumPy
# importing numpy as np
import numpy as np
# declare matrix with np
gfg = np.array([[6, 9], [8, 5], [18, 21]])
# using array.flatten() method
gfg.flatten()
#Output : [2 3 4 5]
[END]
|
Flatten a Matrix Rowix in Python using NumPy
|
https://www.geeksforgeeks.org/flatten-a-matrix-in-python-using-numpy/
|
# importing numpy as np
import numpy as np
# declare matrix with np
gfg = np.array([[6, 9, 12], [8, 5, 2], [18, 21, 24]])
# using array.flatten() method
flat_gfg = gfg.flatten(order="A")
print(flat_gfg)
|
#Output : [2 3 4 5]
|
Flatten a Matrix Rowix in Python using NumPy
# importing numpy as np
import numpy as np
# declare matrix with np
gfg = np.array([[6, 9, 12], [8, 5, 2], [18, 21, 24]])
# using array.flatten() method
flat_gfg = gfg.flatten(order="A")
print(flat_gfg)
#Output : [2 3 4 5]
[END]
|
Flatten a 2d numpy array into 1d array
|
https://www.geeksforgeeks.org/python-flatten-a-2d-numpy-array-into-1d-array/
|
# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1.flatten()
# printing result
print("New resulting array: ", result)
|
#Output : initial array [[1 2 3]
|
Flatten a 2d numpy array into 1d array
# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1.flatten()
# printing result
print("New resulting array: ", result)
#Output : initial array [[1 2 3]
[END]
|
Flatten a 2d numpy array into 1d array
|
https://www.geeksforgeeks.org/python-flatten-a-2d-numpy-array-into-1d-array/
|
# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1.ravel()
# printing result
print("New resulting array: ", result)
|
#Output : initial array [[1 2 3]
|
Flatten a 2d numpy array into 1d array
# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1.ravel()
# printing result
print("New resulting array: ", result)
#Output : initial array [[1 2 3]
[END]
|
Flatten a 2d numpy array into 1d array
|
https://www.geeksforgeeks.org/python-flatten-a-2d-numpy-array-into-1d-array/
|
# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1.reshape([1, 9])
# printing result
print("New resulting array: ", result)
|
#Output : initial array [[1 2 3]
|
Flatten a 2d numpy array into 1d array
# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1.reshape([1, 9])
# printing result
print("New resulting array: ", result)
#Output : initial array [[1 2 3]
[END]
|
Move axes of an array to new positions
|
https://www.geeksforgeeks.org/numpy-moveaxis-function-python/
|
# Python program explaining
# numpy.moveaxis() function
# importing numpy as geek
import numpy as geek
arr = geek.zeros((1, 2, 3, 4))
gfg = geek.moveaxis(arr, 0, -1).shape
print(gfg)
|
#Output :
|
Move axes of an array to new positions
# Python program explaining
# numpy.moveaxis() function
# importing numpy as geek
import numpy as geek
arr = geek.zeros((1, 2, 3, 4))
gfg = geek.moveaxis(arr, 0, -1).shape
print(gfg)
#Output :
[END]
|
Move axes of an array to new positions
|
https://www.geeksforgeeks.org/numpy-moveaxis-function-python/
|
# Python program explaining
# numpy.moveaxis() function
# importing numpy as geek
import numpy as geek
arr = geek.zeros((1, 2, 3, 4))
gfg = geek.moveaxis(arr, -1, 0).shape
print(gfg)
|
#Output :
|
Move axes of an array to new positions
# Python program explaining
# numpy.moveaxis() function
# importing numpy as geek
import numpy as geek
arr = geek.zeros((1, 2, 3, 4))
gfg = geek.moveaxis(arr, -1, 0).shape
print(gfg)
#Output :
[END]
|
Intercharacternge two axes of an array
|
https://www.geeksforgeeks.org/numpy-swapaxes-function-python/
|
# Python program explaining
# numpy.swapaxes() function
# importing numpy as geek
import numpy as geek
arr = geek.array([[2, 4, 6]])
gfg = geek.swapaxes(arr, 0, 1)
print(gfg)
|
#Output :
|
Intercharacternge two axes of an array
# Python program explaining
# numpy.swapaxes() function
# importing numpy as geek
import numpy as geek
arr = geek.array([[2, 4, 6]])
gfg = geek.swapaxes(arr, 0, 1)
print(gfg)
#Output :
[END]
|
Intercharacternge two axes of an array
|
https://www.geeksforgeeks.org/numpy-swapaxes-function-python/
|
# Python program explaining
# numpy.swapaxes() function
# importing numpy as geek
import numpy as geek
arr = geek.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])
gfg = geek.swapaxes(arr, 0, 2)
print(gfg)
|
#Output :
|
Intercharacternge two axes of an array
# Python program explaining
# numpy.swapaxes() function
# importing numpy as geek
import numpy as geek
arr = geek.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])
gfg = geek.swapaxes(arr, 0, 2)
print(gfg)
#Output :
[END]
|
NumPy - Fibonacci Series using Binet Fomula
|
https://www.geeksforgeeks.org/numpy-fibonacci-series-using-binet-formula/
|
import numpy as np
# We are creating an array contains n = 10 elements
# for getting first 10 Fibonacci numbers
a = np.arange(1, 11)
lengthA = len(a)
# splitting of terms for easiness
sqrtFive = np.sqrt(5)
alpha = (1 + sqrtFive) / 2
beta = (1 - sqrtFive) / 2
# Implementation of formula
# np.rint is used for rounding off to integer
Fn = np.rint(((alpha**a) - (beta**a)) / (sqrtFive))
print("The first {} numbers of Fibonacci series are {} . ".format(lengthA, Fn))
|
#Output : # Here user input was 10
|
NumPy - Fibonacci Series using Binet Fomula
import numpy as np
# We are creating an array contains n = 10 elements
# for getting first 10 Fibonacci numbers
a = np.arange(1, 11)
lengthA = len(a)
# splitting of terms for easiness
sqrtFive = np.sqrt(5)
alpha = (1 + sqrtFive) / 2
beta = (1 - sqrtFive) / 2
# Implementation of formula
# np.rint is used for rounding off to integer
Fn = np.rint(((alpha**a) - (beta**a)) / (sqrtFive))
print("The first {} numbers of Fibonacci series are {} . ".format(lengthA, Fn))
#Output : # Here user input was 10
[END]
|
NumPy - Fibonacci Series using Binet Fomula
|
https://www.geeksforgeeks.org/numpy-fibonacci-series-using-binet-formula/
|
import numpy as np
# We are creating an array contains n elements
# for getting first 'n' Fibonacci numbers
fNumber = int(input("Enter the value of n + 1'th number : "))
a = np.arange(1, fNumber)
length_a = len(a)
# splitting of terms for easiness
sqrt_five = np.sqrt(5)
alpha = (1 + sqrt_five) / 2
beta = (1 - sqrt_five) / 2
# Implementation of formula
# np.rint is used for rounding off to integer
Fn = np.rint(((alpha**a) - (beta**a)) / (sqrt_five))
print("The first {} numbers of Fibonacci series are {} . ".format(length_a, Fn))
|
#Output : # Here user input was 10
|
NumPy - Fibonacci Series using Binet Fomula
import numpy as np
# We are creating an array contains n elements
# for getting first 'n' Fibonacci numbers
fNumber = int(input("Enter the value of n + 1'th number : "))
a = np.arange(1, fNumber)
length_a = len(a)
# splitting of terms for easiness
sqrt_five = np.sqrt(5)
alpha = (1 + sqrt_five) / 2
beta = (1 - sqrt_five) / 2
# Implementation of formula
# np.rint is used for rounding off to integer
Fn = np.rint(((alpha**a) - (beta**a)) / (sqrt_five))
print("The first {} numbers of Fibonacci series are {} . ".format(length_a, Fn))
#Output : # Here user input was 10
[END]
|
Counts the number of non-zero values in the array
|
https://www.geeksforgeeks.org/numpy-count_nonzero-method-python/
|
# Python program explaining
# numpy.count_nonzero() function
# importing numpy as geek
import numpy as geek
arr = [[0, 1, 2, 3, 0], [0, 5, 6, 0, 7]]
gfg = geek.count_nonzero(arr)
print(gfg)
|
#Output :
|
Counts the number of non-zero values in the array
# Python program explaining
# numpy.count_nonzero() function
# importing numpy as geek
import numpy as geek
arr = [[0, 1, 2, 3, 0], [0, 5, 6, 0, 7]]
gfg = geek.count_nonzero(arr)
print(gfg)
#Output :
[END]
|
Counts the number of non-zero values in the array
|
https://www.geeksforgeeks.org/numpy-count_nonzero-method-python/
|
# Python program explaining
# numpy.count_nonzero() function
# importing numpy as geek
import numpy as geek
arr = [[0, 1, 2, 3, 4], [5, 0, 6, 0, 7]]
gfg = geek.count_nonzero(arr, axis=0)
print(gfg)
|
#Output :
|
Counts the number of non-zero values in the array
# Python program explaining
# numpy.count_nonzero() function
# importing numpy as geek
import numpy as geek
arr = [[0, 1, 2, 3, 4], [5, 0, 6, 0, 7]]
gfg = geek.count_nonzero(arr, axis=0)
print(gfg)
#Output :
[END]
|
Count the number of elements along a given axis
|
https://www.geeksforgeeks.org/numpy-size-function-python/
|
# Python program explaining
# numpy.size() method
# importing numpy
import numpy as np
# Making a random array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
# By default, give the total number of elements.
print(np.size(arr))
|
#Output : 8
|
Count the number of elements along a given axis
# Python program explaining
# numpy.size() method
# importing numpy
import numpy as np
# Making a random array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
# By default, give the total number of elements.
print(np.size(arr))
#Output : 8
[END]
|
Count the number of elements along a given axis
|
https://www.geeksforgeeks.org/numpy-size-function-python/
|
# Python program explaining
# numpy.size() method
# importing numpy
import numpy as np
# Making a random array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
# count the number of elements along the axis.
# Here rows and columns are being treated
# as elements
# gives no. of rows along x-axis
print(np.size(arr, 0))
# gives no. of columns along y-axis
print(np.size(arr, 1))
|
#Output : 8
|
Count the number of elements along a given axis
# Python program explaining
# numpy.size() method
# importing numpy
import numpy as np
# Making a random array
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
# count the number of elements along the axis.
# Here rows and columns are being treated
# as elements
# gives no. of rows along x-axis
print(np.size(arr, 0))
# gives no. of columns along y-axis
print(np.size(arr, 1))
#Output : 8
[END]
|
Trim the leading and/or trailing zeros from a 1-D array
|
https://www.geeksforgeeks.org/numpy-trim_zeros-in-python/
|
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
# without trim parameter
# returns an array without leading and trailing zeros
res = geek.trim_zeros(gfg)
print(res)
|
#Output :
|
Trim the leading and/or trailing zeros from a 1-D array
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
# without trim parameter
# returns an array without leading and trailing zeros
res = geek.trim_zeros(gfg)
print(res)
#Output :
[END]
|
Trim the leading and/or trailing zeros from a 1-D array
|
https://www.geeksforgeeks.org/numpy-trim_zeros-in-python/
|
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
# without trim parameter
# returns an array without any leading zeros
res = geek.trim_zeros(gfg, "f")
print(res)
|
#Output :
|
Trim the leading and/or trailing zeros from a 1-D array
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
# without trim parameter
# returns an array without any leading zeros
res = geek.trim_zeros(gfg, "f")
print(res)
#Output :
[END]
|
Trim the leading and/or trailing zeros from a 1-D array
|
https://www.geeksforgeeks.org/numpy-trim_zeros-in-python/
|
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
# without trim parameter
# returns an array without any trailing zeros
res = geek.trim_zeros(gfg, "b")
print(res)
|
#Output :
|
Trim the leading and/or trailing zeros from a 1-D array
import numpy as geek
gfg = geek.array((0, 0, 0, 0, 1, 5, 7, 0, 6, 2, 9, 0, 10, 0, 0))
# without trim parameter
# returns an array without any trailing zeros
res = geek.trim_zeros(gfg, "b")
print(res)
#Output :
[END]
|
Reverse a numpy array
|
https://www.geeksforgeeks.org/python-reverse-a-numpy-array/
|
import numpy as np
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
# using shortcut method to reverse
res = np.flip(ini_array)
# printing result
print("final array", str(res))
|
#Output : final array [5 4 6 3 2 1]
|
Reverse a numpy array
import numpy as np
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
# using shortcut method to reverse
res = np.flip(ini_array)
# printing result
print("final array", str(res))
#Output : final array [5 4 6 3 2 1]
[END]
|
Reverse a numpy array
|
https://www.geeksforgeeks.org/python-reverse-a-numpy-array/
|
import numpy as np
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
# printing initial ini_array
print("initial array", str(ini_array))
# printing type of ini_array
print("type of ini_array", type(ini_array))
# using shortcut method to reverse
res = ini_array[::-1]
# printing result
print("final array", str(res))
|
#Output : final array [5 4 6 3 2 1]
|
Reverse a numpy array
import numpy as np
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
# printing initial ini_array
print("initial array", str(ini_array))
# printing type of ini_array
print("type of ini_array", type(ini_array))
# using shortcut method to reverse
res = ini_array[::-1]
# printing result
print("final array", str(res))
#Output : final array [5 4 6 3 2 1]
[END]
|
Reverse a numpy array
|
https://www.geeksforgeeks.org/python-reverse-a-numpy-array/
|
import numpy as np
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
# printing initial ini_array
print("initial array", str(ini_array))
# printing type of ini_array
print("type of ini_array", type(ini_array))
# using flipud method to reverse
res = np.flipud(ini_array)
# printing result
print("final array", str(res))
|
#Output : final array [5 4 6 3 2 1]
|
Reverse a numpy array
import numpy as np
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
# printing initial ini_array
print("initial array", str(ini_array))
# printing type of ini_array
print("type of ini_array", type(ini_array))
# using flipud method to reverse
res = np.flipud(ini_array)
# printing result
print("final array", str(res))
#Output : final array [5 4 6 3 2 1]
[END]
|
How to make a NumPy array read-only?
|
https://www.geeksforgeeks.org/how-to-make-a-numpy-array-read-only/
|
import numpy as np
a = np.zeros(11)
print("Before any change ")
print(a)
a[1] = 2
print("Before after first change ")
print(a)
a.setflags(write=False)
print("After making array immutable on attempting second change ")
a[1] = 7
|
#Output : array.flags.writable=False
|
How to make a NumPy array read-only?
import numpy as np
a = np.zeros(11)
print("Before any change ")
print(a)
a[1] = 2
print("Before after first change ")
print(a)
a.setflags(write=False)
print("After making array immutable on attempting second change ")
a[1] = 7
#Output : array.flags.writable=False
[END]
|
Get the maximum value from given matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-max/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[64, 1; 12, 3]")
# applying matrix.max() method
geeks = gfg.max()
print(geeks)
|
#Output :
|
Get the maximum value from given matrix
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[64, 1; 12, 3]")
# applying matrix.max() method
geeks = gfg.max()
print(geeks)
#Output :
[END]
|
Get the maximum value from given matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-max/
|
# import the important module in python
import numpy as np
# make a matrix with numpy
gfg = np.matrix("[1, 2, 3; 4, 5, 6; 7, 8, 9]")
# applying matrix.max() method
geeks = gfg.max()
print(geeks)
|
#Output :
|
Get the maximum value from given matrix
# import the important module in python
import numpy as np
# make a matrix with numpy
gfg = np.matrix("[1, 2, 3; 4, 5, 6; 7, 8, 9]")
# applying matrix.max() method
geeks = gfg.max()
print(geeks)
#Output :
[END]
|
Get the minimum value from given matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-min/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[64, 1; 12, 3]")
# applying matrix.min() method
geeks = gfg.min()
print(geeks)
|
#Output :
|
Get the minimum value from given matrix
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[64, 1; 12, 3]")
# applying matrix.min() method
geeks = gfg.min()
print(geeks)
#Output :
[END]
|
Get the minimum value from given matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-min/
|
# import the important module in python
import numpy as np
# make a matrix with numpy
gfg = np.matrix("[1, 2, 3; 4, 5, 6; 7, 8, -9]")
# applying matrix.min() method
geeks = gfg.min()
print(geeks)
|
#Output :
|
Get the minimum value from given matrix
# import the important module in python
import numpy as np
# make a matrix with numpy
gfg = np.matrix("[1, 2, 3; 4, 5, 6; 7, 8, -9]")
# applying matrix.min() method
geeks = gfg.min()
print(geeks)
#Output :
[END]
|
Find the number of rows and columns of a given matrix using NumPy
|
https://www.geeksforgeeks.org/find-the-number-of-rows-and-columns-of-a-given-matrix-using-numpy/
|
import numpy as np
matrix = np.arange(1, 9).reshape((3, 3))
# Original matrix
print(matrix)
# Number of rows and columns of the said matrix
print(matrix.shape)
|
#Output : shape()
|
Find the number of rows and columns of a given matrix using NumPy
import numpy as np
matrix = np.arange(1, 9).reshape((3, 3))
# Original matrix
print(matrix)
# Number of rows and columns of the said matrix
print(matrix.shape)
#Output : shape()
[END]
|
Find the number of rows and columns of a given matrix using NumPy
|
https://www.geeksforgeeks.org/find-the-number-of-rows-and-columns-of-a-given-matrix-using-numpy/
|
import numpy as np
matrix = np.arange(10, 15).reshape((3, 2))
# Original matrix:
print(matrix)
# Number of rows and columns of the said matrix
print(matrix.shape)
|
#Output : shape()
|
Find the number of rows and columns of a given matrix using NumPy
import numpy as np
matrix = np.arange(10, 15).reshape((3, 2))
# Original matrix:
print(matrix)
# Number of rows and columns of the said matrix
print(matrix.shape)
#Output : shape()
[END]
|
Selementsect the elements from a given matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-take/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 12, 3, 4, 6, 7]")
# applying matrix.take() method
geek = gfg.take(2)
print(geek)
|
#Output :
|
Selementsect the elements from a given matrix
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 12, 3, 4, 6, 7]")
# applying matrix.take() method
geek = gfg.take(2)
print(geek)
#Output :
[END]
|
Selementsect the elements from a given matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-take/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 9; 12, 3, 1; 4, 5, 6]")
# applying matrix.take() method
geek = gfg.take(0, 1)
print(geek)
|
#Output :
|
Selementsect the elements from a given matrix
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 9; 12, 3, 1; 4, 5, 6]")
# applying matrix.take() method
geek = gfg.take(0, 1)
print(geek)
#Output :
[END]
|
Find the sum of values in a matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-sum/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1; 12, 3]")
# applying matrix.sum() method
geek = gfg.sum()
print(geek)
|
#Output :
|
Find the sum of values in a matrix
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1; 12, 3]")
# applying matrix.sum() method
geek = gfg.sum()
print(geek)
#Output :
[END]
|
Find the sum of values in a matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-sum/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 9; 12, 3, 1; 4, 5, 6]")
# applying matrix.sum() method
geek = gfg.sum(axis=1)
print(geek)
|
#Output :
|
Find the sum of values in a matrix
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 9; 12, 3, 1; 4, 5, 6]")
# applying matrix.sum() method
geek = gfg.sum(axis=1)
print(geek)
#Output :
[END]
|
Calculate the sum of the diagonal elements of a NumPy array
|
https://www.geeksforgeeks.org/calculate-the-sum-of-the-diagonal-elements-of-a-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15], [30, 44, 2], [11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the Trace of a matrix
trace = np.trace(n_array)
print("\nTrace of given 3X3 matrix:")
print(trace)
|
#Output : numpy.diagonal(a, offset=0, axis1=0, axis2=1
|
Calculate the sum of the diagonal elements of a NumPy array
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15], [30, 44, 2], [11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the Trace of a matrix
trace = np.trace(n_array)
print("\nTrace of given 3X3 matrix:")
print(trace)
#Output : numpy.diagonal(a, offset=0, axis1=0, axis2=1
[END]
|
Calculate the sum of the diagonal elements of a NumPy array
|
https://www.geeksforgeeks.org/calculate-the-sum-of-the-diagonal-elements-of-a-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a 4X4 Numpy matrix
n_array = np.array(
[[55, 25, 15, 41], [30, 44, 2, 54], [11, 45, 77, 11], [11, 212, 4, 20]]
)
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the Trace of a matrix
trace = np.trace(n_array)
print("\nTrace of given 4X4 matrix:")
print(trace)
|
#Output : numpy.diagonal(a, offset=0, axis1=0, axis2=1
|
Calculate the sum of the diagonal elements of a NumPy array
# importing Numpy package
import numpy as np
# creating a 4X4 Numpy matrix
n_array = np.array(
[[55, 25, 15, 41], [30, 44, 2, 54], [11, 45, 77, 11], [11, 212, 4, 20]]
)
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the Trace of a matrix
trace = np.trace(n_array)
print("\nTrace of given 4X4 matrix:")
print(trace)
#Output : numpy.diagonal(a, offset=0, axis1=0, axis2=1
[END]
|
Calculate the sum of the diagonal elements of a NumPy array
|
https://www.geeksforgeeks.org/calculate-the-sum-of-the-diagonal-elements-of-a-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15], [30, 44, 2], [11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# Finding the diagonal elements of a matrix
diag = np.diagonal(n_array)
print("\nDiagonal elements are:")
print(diag)
print("\nSum of Diagonal elements is:")
print(sum(diag))
|
#Output : numpy.diagonal(a, offset=0, axis1=0, axis2=1
|
Calculate the sum of the diagonal elements of a NumPy array
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15], [30, 44, 2], [11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# Finding the diagonal elements of a matrix
diag = np.diagonal(n_array)
print("\nDiagonal elements are:")
print(diag)
print("\nSum of Diagonal elements is:")
print(sum(diag))
#Output : numpy.diagonal(a, offset=0, axis1=0, axis2=1
[END]
|
Calculate the sum of the diagonal elements of a NumPy array
|
https://www.geeksforgeeks.org/calculate-the-sum-of-the-diagonal-elements-of-a-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a 5X5 Numpy matrix
n_array = np.array(
[
[5, 2, 1, 4, 6],
[9, 4, 2, 5, 2],
[11, 5, 7, 3, 9],
[5, 6, 6, 7, 2],
[7, 5, 9, 3, 3],
]
)
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# Finding the diagonal elements of a matrix
diag = np.diagonal(n_array)
print("\nDiagonal elements are:")
print(diag)
print("\nSum of Diagonal elements is:")
print(sum(diag))
|
#Output : numpy.diagonal(a, offset=0, axis1=0, axis2=1
|
Calculate the sum of the diagonal elements of a NumPy array
# importing Numpy package
import numpy as np
# creating a 5X5 Numpy matrix
n_array = np.array(
[
[5, 2, 1, 4, 6],
[9, 4, 2, 5, 2],
[11, 5, 7, 3, 9],
[5, 6, 6, 7, 2],
[7, 5, 9, 3, 3],
]
)
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# Finding the diagonal elements of a matrix
diag = np.diagonal(n_array)
print("\nDiagonal elements are:")
print(diag)
print("\nSum of Diagonal elements is:")
print(sum(diag))
#Output : numpy.diagonal(a, offset=0, axis1=0, axis2=1
[END]
|
Adding and Subtracting Matrix Rowices in Python
|
https://www.geeksforgeeks.org/adding-and-subtracting-matrices-in-python/
|
# importing numpy as np
import numpy as np
# creating first matrix
A = np.array([[1, 2], [3, 4]])
# creating second matrix
B = np.array([[4, 5], [6, 7]])
print("Printing elements of first matrix")
print(A)
print("Printing elements of second matrix")
print(B)
# adding two matrix
print("Addition of two matrix")
print(np.add(A, B))
|
#Output : Suppose we have two matrices A and B.
|
Adding and Subtracting Matrix Rowices in Python
# importing numpy as np
import numpy as np
# creating first matrix
A = np.array([[1, 2], [3, 4]])
# creating second matrix
B = np.array([[4, 5], [6, 7]])
print("Printing elements of first matrix")
print(A)
print("Printing elements of second matrix")
print(B)
# adding two matrix
print("Addition of two matrix")
print(np.add(A, B))
#Output : Suppose we have two matrices A and B.
[END]
|
Adding and Subtracting Matrix Rowices in Python
|
https://www.geeksforgeeks.org/adding-and-subtracting-matrices-in-python/
|
# importing numpy as np
import numpy as np
# creating first matrix
A = np.array([[1, 2], [3, 4]])
# creating second matrix
B = np.array([[4, 5], [6, 7]])
print("Printing elements of first matrix")
print(A)
print("Printing elements of second matrix")
print(B)
# subtracting two matrix
print("Subtraction of two matrix")
print(np.subtract(A, B))
|
#Output : Suppose we have two matrices A and B.
|
Adding and Subtracting Matrix Rowices in Python
# importing numpy as np
import numpy as np
# creating first matrix
A = np.array([[1, 2], [3, 4]])
# creating second matrix
B = np.array([[4, 5], [6, 7]])
print("Printing elements of first matrix")
print(A)
print("Printing elements of second matrix")
print(B)
# subtracting two matrix
print("Subtraction of two matrix")
print(np.subtract(A, B))
#Output : Suppose we have two matrices A and B.
[END]
|
Adding and Subtracting Matrix Rowices in Python
|
https://www.geeksforgeeks.org/adding-and-subtracting-matrices-in-python/
|
# Input matrices
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[4, 5], [6, 7]]
# Printing elements of matrix1
print("Printing elements of first matrix")
for row in matrix1:
for element in row:
print(element, end=" ")
print()
# Printing elements of matrix2
print("Printing elements of second matrix")
for row in matrix2:
for element in row:
print(element, end=" ")
print()
# Subtracting two matrices
result = [[0, 0], [0, 0]]
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
result[i][j] = matrix1[i][j] - matrix2[i][j]
# Printing the result
print("Subtraction of two matrix")
for row in result:
for element in row:
print(element, end=" ")
print()
|
#Output : Suppose we have two matrices A and B.
|
Adding and Subtracting Matrix Rowices in Python
# Input matrices
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[4, 5], [6, 7]]
# Printing elements of matrix1
print("Printing elements of first matrix")
for row in matrix1:
for element in row:
print(element, end=" ")
print()
# Printing elements of matrix2
print("Printing elements of second matrix")
for row in matrix2:
for element in row:
print(element, end=" ")
print()
# Subtracting two matrices
result = [[0, 0], [0, 0]]
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
result[i][j] = matrix1[i][j] - matrix2[i][j]
# Printing the result
print("Subtraction of two matrix")
for row in result:
for element in row:
print(element, end=" ")
print()
#Output : Suppose we have two matrices A and B.
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# printing initial array
print("initial_array : ", str(ini_array))
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.append(ini_array, column_to_be_added, axis=1)
# printing result
print("resultant array", str(arr))
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# printing initial array
print("initial_array : ", str(ini_array))
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.append(ini_array, column_to_be_added, axis=1)
# printing result
print("resultant array", str(arr))
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.concatenate([ini_array, column_to_be_added], axis=1)
# printing result
print("resultant array", str(arr))
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.concatenate([ini_array, column_to_be_added], axis=1)
# printing result
print("resultant array", str(arr))
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.insert(ini_array, 0, column_to_be_added, axis=1)
# printing result
print("resultant array", str(arr))
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([[1], [2], [3]])
# Adding column to array using append() method
arr = np.insert(ini_array, 0, column_to_be_added, axis=1)
# printing result
print("resultant array", str(arr))
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([1, 2, 3])
# Adding column to numpy array
result = np.hstack((ini_array, np.atleast_2d(column_to_be_added).T))
# printing result
print("resultant array", str(result))
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([1, 2, 3])
# Adding column to numpy array
result = np.hstack((ini_array, np.atleast_2d(column_to_be_added).T))
# printing result
print("resultant array", str(result))
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([1, 2, 3])
# Adding column to numpy array
result = np.column_stack((ini_array, column_to_be_added))
# printing result
print("resultant array", str(result))
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as column
column_to_be_added = np.array([1, 2, 3])
# Adding column to numpy array
result = np.column_stack((ini_array, column_to_be_added))
# printing result
print("resultant array", str(result))
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# printing initial array
print("initial_array : ", str(ini_array))
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
# Adding row to numpy array
result = np.r_[ini_array, [row_to_be_added]]
# printing result
print("resultant array", str(result))
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# printing initial array
print("initial_array : ", str(ini_array))
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
# Adding row to numpy array
result = np.r_[ini_array, [row_to_be_added]]
# printing result
print("resultant array", str(result))
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
# last row
row_n = arr.shape[0]
arr = np.insert(ini_array, row_n, [row_to_be_added], axis=0)
# printing result
print("resultant array", str(arr))
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
# last row
row_n = arr.shape[0]
arr = np.insert(ini_array, row_n, [row_to_be_added], axis=0)
# printing result
print("resultant array", str(arr))
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
# Adding row to numpy array
result = np.vstack((ini_array, row_to_be_added))
# printing result
print("resultant array", str(result))
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
import numpy as np
ini_array = np.array([[1, 2, 3], [45, 4, 7], [9, 6, 10]])
# Array to be added as row
row_to_be_added = np.array([1, 2, 3])
# Adding row to numpy array
result = np.vstack((ini_array, row_to_be_added))
# printing result
print("resultant array", str(result))
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
# importing Numpy package
import numpy as np
# creating an empty 2d array of int type
empt_array = np.empty((0, 2), int)
print("Empty array:")
print(empt_array)
# adding two new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[10, 20]]), axis=0)
empt_array = np.append(empt_array, np.array([[40, 50]]), axis=0)
print("\nNow array is:")
print(empt_array)
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
# importing Numpy package
import numpy as np
# creating an empty 2d array of int type
empt_array = np.empty((0, 2), int)
print("Empty array:")
print(empt_array)
# adding two new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[10, 20]]), axis=0)
empt_array = np.append(empt_array, np.array([[40, 50]]), axis=0)
print("\nNow array is:")
print(empt_array)
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
# importing Numpy package
import numpy as np
# creating an empty 3d array of int type
empt_array = np.empty((0, 3), int)
print("Empty array:")
print(empt_array)
# adding three new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[10, 20, 40]]), axis=0)
empt_array = np.append(empt_array, np.array([[40, 50, 55]]), axis=0)
empt_array = np.append(empt_array, np.array([[40, 50, 55]]), axis=0)
print("\nNow array is:")
print(empt_array)
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
# importing Numpy package
import numpy as np
# creating an empty 3d array of int type
empt_array = np.empty((0, 3), int)
print("Empty array:")
print(empt_array)
# adding three new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[10, 20, 40]]), axis=0)
empt_array = np.append(empt_array, np.array([[40, 50, 55]]), axis=0)
empt_array = np.append(empt_array, np.array([[40, 50, 55]]), axis=0)
print("\nNow array is:")
print(empt_array)
#Output : initial_array : [[ 1 2 3]
[END]
|
Ways to add row/columns in numpy array
|
https://www.geeksforgeeks.org/python-ways-to-add-row-columns-in-numpy-array/
|
# importing Numpy package
import numpy as np
# creating an empty 4d array of int type
empt_array = np.empty((0, 4), int)
print("Empty array:")
print(empt_array)
# adding four new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[100, 200, 400, 888]]), axis=0)
empt_array = np.append(empt_array, np.array([[405, 500, 550, 558]]), axis=0)
empt_array = np.append(empt_array, np.array([[404, 505, 555, 145]]), axis=0)
empt_array = np.append(empt_array, np.array([[44, 55, 550, 150]]), axis=0)
print("\nNow array is:")
print(empt_array)
|
#Output : initial_array : [[ 1 2 3]
|
Ways to add row/columns in numpy array
# importing Numpy package
import numpy as np
# creating an empty 4d array of int type
empt_array = np.empty((0, 4), int)
print("Empty array:")
print(empt_array)
# adding four new rows to empt_array
# using np.append()
empt_array = np.append(empt_array, np.array([[100, 200, 400, 888]]), axis=0)
empt_array = np.append(empt_array, np.array([[405, 500, 550, 558]]), axis=0)
empt_array = np.append(empt_array, np.array([[404, 505, 555, 145]]), axis=0)
empt_array = np.append(empt_array, np.array([[44, 55, 550, 150]]), axis=0)
print("\nNow array is:")
print(empt_array)
#Output : initial_array : [[ 1 2 3]
[END]
|
Matrix Rowix Multiplication in NumPy
|
https://www.geeksforgeeks.org/matrix-multiplication-in-numpy/
|
# importing the module
import numpy as np
# creating two matrices
p = [[1, 2], [2, 3]]
q = [[4, 5], [6, 7]]
print("Matrix p :")
print(p)
print("Matrix q :")
print(q)
# computing product
result = np.dot(p, q)
# printing the result
print("The matrix multiplication is :")
print(result)
|
#Output :
|
Matrix Rowix Multiplication in NumPy
# importing the module
import numpy as np
# creating two matrices
p = [[1, 2], [2, 3]]
q = [[4, 5], [6, 7]]
print("Matrix p :")
print(p)
print("Matrix q :")
print(q)
# computing product
result = np.dot(p, q)
# printing the result
print("The matrix multiplication is :")
print(result)
#Output :
[END]
|
Matrix Rowix Multiplication in NumPy
|
https://www.geeksforgeeks.org/matrix-multiplication-in-numpy/
|
# importing the module
import numpy as np
# creating two matrices
p = [[1, 2], [2, 3], [4, 5]]
q = [[4, 5, 1], [6, 7, 2]]
print("Matrix p :")
print(p)
print("Matrix q :")
print(q)
# computing product
result = np.dot(p, q)
# printing the result
print("The matrix multiplication is :")
print(result)
|
#Output :
|
Matrix Rowix Multiplication in NumPy
# importing the module
import numpy as np
# creating two matrices
p = [[1, 2], [2, 3], [4, 5]]
q = [[4, 5, 1], [6, 7, 2]]
print("Matrix p :")
print(p)
print("Matrix q :")
print(q)
# computing product
result = np.dot(p, q)
# printing the result
print("The matrix multiplication is :")
print(result)
#Output :
[END]
|
How to Calculate the determinant of a matrix using NumPy?
|
https://www.geeksforgeeks.org/how-to-calculate-the-determinant-of-a-matrix-using-numpy/
|
# importing Numpy package
import numpy as np
# creating a 2X2 Numpy matrix
n_array = np.array([[50, 29], [30, 44]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the determinant of matrix
det = np.linalg.det(n_array)
print("\nDeterminant of given 2X2 matrix:")
print(int(det))
|
#Output : numpy.linalg.det(array)
|
How to Calculate the determinant of a matrix using NumPy?
# importing Numpy package
import numpy as np
# creating a 2X2 Numpy matrix
n_array = np.array([[50, 29], [30, 44]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the determinant of matrix
det = np.linalg.det(n_array)
print("\nDeterminant of given 2X2 matrix:")
print(int(det))
#Output : numpy.linalg.det(array)
[END]
|
How to Calculate the determinant of a matrix using NumPy?
|
https://www.geeksforgeeks.org/how-to-calculate-the-determinant-of-a-matrix-using-numpy/
|
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15], [30, 44, 2], [11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the determinant of matrix
det = np.linalg.det(n_array)
print("\nDeterminant of given 3X3 square matrix:")
print(int(det))
|
#Output : numpy.linalg.det(array)
|
How to Calculate the determinant of a matrix using NumPy?
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15], [30, 44, 2], [11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the determinant of matrix
det = np.linalg.det(n_array)
print("\nDeterminant of given 3X3 square matrix:")
print(int(det))
#Output : numpy.linalg.det(array)
[END]
|
How to Calculate the determinant of a matrix using NumPy?
|
https://www.geeksforgeeks.org/how-to-calculate-the-determinant-of-a-matrix-using-numpy/
|
# importing Numpy package
import numpy as np
# creating a 5X5 Numpy matrix
n_array = np.array(
[
[5, 2, 1, 4, 6],
[9, 4, 2, 5, 2],
[11, 5, 7, 3, 9],
[5, 6, 6, 7, 2],
[7, 5, 9, 3, 3],
]
)
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the determinant of matrix
det = np.linalg.det(n_array)
print("\nDeterminant of given 5X5 square matrix:")
print(int(det))
|
#Output : numpy.linalg.det(array)
|
How to Calculate the determinant of a matrix using NumPy?
# importing Numpy package
import numpy as np
# creating a 5X5 Numpy matrix
n_array = np.array(
[
[5, 2, 1, 4, 6],
[9, 4, 2, 5, 2],
[11, 5, 7, 3, 9],
[5, 6, 6, 7, 2],
[7, 5, 9, 3, 3],
]
)
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the determinant of matrix
det = np.linalg.det(n_array)
print("\nDeterminant of given 5X5 square matrix:")
print(int(det))
#Output : numpy.linalg.det(array)
[END]
|
How to inverse a matrix using NumPy
|
https://www.geeksforgeeks.org/how-to-inverse-a-matrix-using-numpy/
|
# Import required package
import numpy as np
# Taking a 3 * 3 matrix
A = np.array([[6, 1, 1], [4, -2, 5], [2, 8, 7]])
# Calculating the inverse of the matrix
print(np.linalg.inv(A))
|
#Output : if det(A) != 0
|
How to inverse a matrix using NumPy
# Import required package
import numpy as np
# Taking a 3 * 3 matrix
A = np.array([[6, 1, 1], [4, -2, 5], [2, 8, 7]])
# Calculating the inverse of the matrix
print(np.linalg.inv(A))
#Output : if det(A) != 0
[END]
|
How to inverse a matrix using NumPy
|
https://www.geeksforgeeks.org/how-to-inverse-a-matrix-using-numpy/
|
# Import required package
import numpy as np
# Taking a 4 * 4 matrix
A = np.array([[6, 1, 1, 3], [4, -2, 5, 1], [2, 8, 7, 6], [3, 1, 9, 7]])
# Calculating the inverse of the matrix
print(np.linalg.inv(A))
|
#Output : if det(A) != 0
|
How to inverse a matrix using NumPy
# Import required package
import numpy as np
# Taking a 4 * 4 matrix
A = np.array([[6, 1, 1, 3], [4, -2, 5, 1], [2, 8, 7, 6], [3, 1, 9, 7]])
# Calculating the inverse of the matrix
print(np.linalg.inv(A))
#Output : if det(A) != 0
[END]
|
How to inverse a matrix using NumPy
|
https://www.geeksforgeeks.org/how-to-inverse-a-matrix-using-numpy/
|
# Import required package
import numpy as np
# Inverses of several matrices can
# be computed at once
A = np.array([[[1.0, 2.0], [3.0, 4.0]], [[1, 3], [3, 5]]])
# Calculating the inverse of the matrix
print(np.linalg.inv(A))
|
#Output : if det(A) != 0
|
How to inverse a matrix using NumPy
# Import required package
import numpy as np
# Inverses of several matrices can
# be computed at once
A = np.array([[[1.0, 2.0], [3.0, 4.0]], [[1, 3], [3, 5]]])
# Calculating the inverse of the matrix
print(np.linalg.inv(A))
#Output : if det(A) != 0
[END]
|
How to count the frequency of unique values in NumPy array?
|
https://www.geeksforgeeks.org/how-to-count-the-frequency-of-unique-values-in-numpy-array/
|
# import library
import numpy as np
ini_array = np.array([10, 20, 5, 10, 8, 20, 8, 9])
# Get a tuple of unique values
# and their frequency in
# numpy array
unique, frequency = np.unique(ini_array, return_counts=True)
# print unique values array
print("Unique Values:", unique)
# print frequency array
print("Frequency Values:", frequency)
|
#Output : Unique Values: [ 5 8 9 10 20]
|
How to count the frequency of unique values in NumPy array?
# import library
import numpy as np
ini_array = np.array([10, 20, 5, 10, 8, 20, 8, 9])
# Get a tuple of unique values
# and their frequency in
# numpy array
unique, frequency = np.unique(ini_array, return_counts=True)
# print unique values array
print("Unique Values:", unique)
# print frequency array
print("Frequency Values:", frequency)
#Output : Unique Values: [ 5 8 9 10 20]
[END]
|
How to count the frequency of unique values in NumPy array?
|
https://www.geeksforgeeks.org/how-to-count-the-frequency-of-unique-values-in-numpy-array/
|
# import library
import numpy as np
# create a 1d-array
ini_array = np.array([10, 20, 5, 10, 8, 20, 8, 9])
# Get a tuple of unique values
# and their frequency
# in numpy array
unique, frequency = np.unique(ini_array, return_counts=True)
# convert both into one numpy array
count = np.asarray((unique, frequency))
print("The values and their frequency are:\n", count)
|
#Output : Unique Values: [ 5 8 9 10 20]
|
How to count the frequency of unique values in NumPy array?
# import library
import numpy as np
# create a 1d-array
ini_array = np.array([10, 20, 5, 10, 8, 20, 8, 9])
# Get a tuple of unique values
# and their frequency
# in numpy array
unique, frequency = np.unique(ini_array, return_counts=True)
# convert both into one numpy array
count = np.asarray((unique, frequency))
print("The values and their frequency are:\n", count)
#Output : Unique Values: [ 5 8 9 10 20]
[END]
|
How to count the frequency of unique values in NumPy array?
|
https://www.geeksforgeeks.org/how-to-count-the-frequency-of-unique-values-in-numpy-array/
|
# import library
import numpy as np
# create a 1d-array
ini_array = np.array([10, 20, 5, 10, 8, 20, 8, 9])
# Get a tuple of unique values
# and their frequency in
# numpy array
unique, frequency = np.unique(ini_array, return_counts=True)
# convert both into one numpy array
# and then transpose it
count = np.asarray((unique, frequency)).T
print("The values and their frequency are in transpose form:\n", count)
|
#Output : Unique Values: [ 5 8 9 10 20]
|
How to count the frequency of unique values in NumPy array?
# import library
import numpy as np
# create a 1d-array
ini_array = np.array([10, 20, 5, 10, 8, 20, 8, 9])
# Get a tuple of unique values
# and their frequency in
# numpy array
unique, frequency = np.unique(ini_array, return_counts=True)
# convert both into one numpy array
# and then transpose it
count = np.asarray((unique, frequency)).T
print("The values and their frequency are in transpose form:\n", count)
#Output : Unique Values: [ 5 8 9 10 20]
[END]
|
Multiply matrices of complex numbers using NumPy in Python
|
https://www.geeksforgeeks.org/multiply-matrices-of-complex-numbers-using-numpy-in-python/
|
# importing numpy as library
import numpy as np
# creating matrix of complex number
x = np.array([2 + 3j, 4 + 5j])
print("Printing First matrix:")
print(x)
y = np.array([8 + 7j, 5 + 6j])
print("Printing Second matrix:")
print(y)
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z)
|
#Output : numpy.vdot(vector_a, vector_b)
|
Multiply matrices of complex numbers using NumPy in Python
# importing numpy as library
import numpy as np
# creating matrix of complex number
x = np.array([2 + 3j, 4 + 5j])
print("Printing First matrix:")
print(x)
y = np.array([8 + 7j, 5 + 6j])
print("Printing Second matrix:")
print(y)
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z)
#Output : numpy.vdot(vector_a, vector_b)
[END]
|
Multiply matrices of complex numbers using NumPy in Python
|
https://www.geeksforgeeks.org/multiply-matrices-of-complex-numbers-using-numpy-in-python/
|
# importing numpy as library
import numpy as np
# creating matrix of complex number
x = np.array([[2 + 3j, 4 + 5j], [4 + 5j, 6 + 7j]])
print("Printing First matrix:")
print(x)
y = np.array([[8 + 7j, 5 + 6j], [9 + 10j, 1 + 2j]])
print("Printing Second matrix:")
print(y)
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z)
|
#Output : numpy.vdot(vector_a, vector_b)
|
Multiply matrices of complex numbers using NumPy in Python
# importing numpy as library
import numpy as np
# creating matrix of complex number
x = np.array([[2 + 3j, 4 + 5j], [4 + 5j, 6 + 7j]])
print("Printing First matrix:")
print(x)
y = np.array([[8 + 7j, 5 + 6j], [9 + 10j, 1 + 2j]])
print("Printing Second matrix:")
print(y)
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z)
#Output : numpy.vdot(vector_a, vector_b)
[END]
|
Compute the outer product of two given vectors using NumPy in Python
|
https://www.geeksforgeeks.org/compute-the-outer-product-of-two-given-vectors-using-numpy-in-python/
|
# Importing library
import numpy as np
# Creating two 1-D arrays
array1 = np.array([6, 2])
array2 = np.array([2, 5])
print("Original 1-D arrays:")
print(array1)
print(array2)
# Output
print("Outer Product of the two array is:")
result = np.outer(array1, array2)
print(result)
|
#Output : Original 1-D arrays:
|
Compute the outer product of two given vectors using NumPy in Python
# Importing library
import numpy as np
# Creating two 1-D arrays
array1 = np.array([6, 2])
array2 = np.array([2, 5])
print("Original 1-D arrays:")
print(array1)
print(array2)
# Output
print("Outer Product of the two array is:")
result = np.outer(array1, array2)
print(result)
#Output : Original 1-D arrays:
[END]
|
Compute the outer product of two given vectors using NumPy in Python
|
https://www.geeksforgeeks.org/compute-the-outer-product-of-two-given-vectors-using-numpy-in-python/
|
# Importing library
import numpy as np
# Creating two 2-D matrix
matrix1 = np.array([[1, 3], [2, 6]])
matrix2 = np.array([[0, 1], [1, 9]])
print("Original 2-D matrix:")
print(matrix1)
print(matrix2)
# Output
print("Outer Product of the two matrix is:")
result = np.outer(matrix1, matrix2)
print(result)
|
#Output : Original 1-D arrays:
|
Compute the outer product of two given vectors using NumPy in Python
# Importing library
import numpy as np
# Creating two 2-D matrix
matrix1 = np.array([[1, 3], [2, 6]])
matrix2 = np.array([[0, 1], [1, 9]])
print("Original 2-D matrix:")
print(matrix1)
print(matrix2)
# Output
print("Outer Product of the two matrix is:")
result = np.outer(matrix1, matrix2)
print(result)
#Output : Original 1-D arrays:
[END]
|
Compute the outer product of two given vectors using NumPy in Python
|
https://www.geeksforgeeks.org/compute-the-outer-product-of-two-given-vectors-using-numpy-in-python/
|
# Importing library
import numpy as np
# Creating two 3-D matrix
matrix1 = np.array([[2, 8, 2], [3, 4, 8], [0, 2, 1]])
matrix2 = np.array([[2, 1, 1], [0, 1, 0], [2, 3, 0]])
print("Original 3-D matrix:")
print(matrix1)
print(matrix2)
# Output
print("Outer Product of the two matrix is:")
result = np.outer(matrix1, matrix2)
print(result)
|
#Output : Original 1-D arrays:
|
Compute the outer product of two given vectors using NumPy in Python
# Importing library
import numpy as np
# Creating two 3-D matrix
matrix1 = np.array([[2, 8, 2], [3, 4, 8], [0, 2, 1]])
matrix2 = np.array([[2, 1, 1], [0, 1, 0], [2, 3, 0]])
print("Original 3-D matrix:")
print(matrix1)
print(matrix2)
# Output
print("Outer Product of the two matrix is:")
result = np.outer(matrix1, matrix2)
print(result)
#Output : Original 1-D arrays:
[END]
|
Calculate inner, outer, and cross products of matrices and vectors using NumPy
|
https://www.geeksforgeeks.org/calculate-inner-outer-and-cross-products-of-matrices-and-vectors-using-numpy/
|
# Python Program illustrating
# numpy.inner() method
import numpy as np
# Vectors
a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
# Inner Product of Vectors
print("\nInner product of vectors a and b =")
print(np.inner(a, b))
print("---------------------------------------")
# Matrices
x = np.array([[2, 3, 4], [3, 2, 9]])
y = np.array([[1, 5, 0], [5, 10, 3]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
# Inner product of matrices
print("\nInner product of matrices x and y =")
print(np.inner(x, y))
|
#Output : numpy.inner(arr1, arr2)
|
Calculate inner, outer, and cross products of matrices and vectors using NumPy
# Python Program illustrating
# numpy.inner() method
import numpy as np
# Vectors
a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
# Inner Product of Vectors
print("\nInner product of vectors a and b =")
print(np.inner(a, b))
print("---------------------------------------")
# Matrices
x = np.array([[2, 3, 4], [3, 2, 9]])
y = np.array([[1, 5, 0], [5, 10, 3]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
# Inner product of matrices
print("\nInner product of matrices x and y =")
print(np.inner(x, y))
#Output : numpy.inner(arr1, arr2)
[END]
|
Calculate inner, outer, and cross products of matrices and vectors using NumPy
|
https://www.geeksforgeeks.org/calculate-inner-outer-and-cross-products-of-matrices-and-vectors-using-numpy/
|
# Python Program illustrating
# numpy.outer() method
import numpy as np
# Vectors
a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
# Outer product of vectors
print("\nOuter product of vectors a and b =")
print(np.outer(a, b))
print("------------------------------------")
# Matrices
x = np.array([[3, 6, 4], [9, 4, 6]])
y = np.array([[1, 15, 7], [3, 10, 8]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
# Outer product of matrices
print("\nOuter product of matrices x and y =")
print(np.outer(x, y))
|
#Output : numpy.inner(arr1, arr2)
|
Calculate inner, outer, and cross products of matrices and vectors using NumPy
# Python Program illustrating
# numpy.outer() method
import numpy as np
# Vectors
a = np.array([2, 6])
b = np.array([3, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
# Outer product of vectors
print("\nOuter product of vectors a and b =")
print(np.outer(a, b))
print("------------------------------------")
# Matrices
x = np.array([[3, 6, 4], [9, 4, 6]])
y = np.array([[1, 15, 7], [3, 10, 8]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
# Outer product of matrices
print("\nOuter product of matrices x and y =")
print(np.outer(x, y))
#Output : numpy.inner(arr1, arr2)
[END]
|
Calculate inner, outer, and cross products of matrices and vectors using NumPy
|
https://www.geeksforgeeks.org/calculate-inner-outer-and-cross-products-of-matrices-and-vectors-using-numpy/
|
# Python Program illustrating
# numpy.cross() method
import numpy as np
# Vectors
a = np.array([3, 6])
b = np.array([9, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
# Cross product of vectors
print("\nCross product of vectors a and b =")
print(np.cross(a, b))
print("------------------------------------")
# Matrices
x = np.array([[2, 6, 9], [2, 7, 3]])
y = np.array([[7, 5, 6], [3, 12, 3]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
# Cross product of matrices
print("\nCross product of matrices x and y =")
print(np.cross(x, y))
|
#Output : numpy.inner(arr1, arr2)
|
Calculate inner, outer, and cross products of matrices and vectors using NumPy
# Python Program illustrating
# numpy.cross() method
import numpy as np
# Vectors
a = np.array([3, 6])
b = np.array([9, 10])
print("Vectors :")
print("a = ", a)
print("\nb = ", b)
# Cross product of vectors
print("\nCross product of vectors a and b =")
print(np.cross(a, b))
print("------------------------------------")
# Matrices
x = np.array([[2, 6, 9], [2, 7, 3]])
y = np.array([[7, 5, 6], [3, 12, 3]])
print("\nMatrices :")
print("x =", x)
print("\ny =", y)
# Cross product of matrices
print("\nCross product of matrices x and y =")
print(np.cross(x, y))
#Output : numpy.inner(arr1, arr2)
[END]
|
Compute the covariance matrix of two given NumPy arrays
|
https://www.geeksforgeeks.org/compute-the-covariance-matrix-of-two-given-numpy-arrays/
|
import numpy as np
array1 = np.array([0, 1, 1])
array2 = np.array([2, 2, 1])
# Original array1
print(array1)
# Original array2
print(array2)
# Covariance matrix
print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2))
|
#Output : [0 1 1]
|
Compute the covariance matrix of two given NumPy arrays
import numpy as np
array1 = np.array([0, 1, 1])
array2 = np.array([2, 2, 1])
# Original array1
print(array1)
# Original array2
print(array2)
# Covariance matrix
print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2))
#Output : [0 1 1]
[END]
|
Compute the covariance matrix of two given NumPy arrays
|
https://www.geeksforgeeks.org/compute-the-covariance-matrix-of-two-given-numpy-arrays/
|
import numpy as np
array1 = np.array([2, 1, 1, 4])
array2 = np.array([2, 2, 1, 1])
# Original array1
print(array1)
# Original array2
print(array2)
# Covariance matrix
print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2))
|
#Output : [0 1 1]
|
Compute the covariance matrix of two given NumPy arrays
import numpy as np
array1 = np.array([2, 1, 1, 4])
array2 = np.array([2, 2, 1, 1])
# Original array1
print(array1)
# Original array2
print(array2)
# Covariance matrix
print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2))
#Output : [0 1 1]
[END]
|
Compute the covariance matrix of two given NumPy arrays
|
https://www.geeksforgeeks.org/compute-the-covariance-matrix-of-two-given-numpy-arrays/
|
import numpy as np
array1 = np.array([1, 2])
array2 = np.array([1, 2])
# Original array1
print(array1)
# Original array2
print(array2)
# Covariance matrix
print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2))
|
#Output : [0 1 1]
|
Compute the covariance matrix of two given NumPy arrays
import numpy as np
array1 = np.array([1, 2])
array2 = np.array([1, 2])
# Original array1
print(array1)
# Original array2
print(array2)
# Covariance matrix
print("\nCovariance matrix of the said arrays:\n", np.cov(array1, array2))
#Output : [0 1 1]
[END]
|
Compute the covariance matrix of two given NumPy arrays
|
https://www.geeksforgeeks.org/compute-the-covariance-matrix-of-two-given-numpy-arrays/
|
import numpy as np
x = [1.23, 2.12, 3.34, 4.5]
y = [2.56, 2.89, 3.76, 3.95]
# find out covariance with respect
# rows
cov_mat = np.stack((x, y), axis=1)
print("shape of matrix x and y:", np.shape(cov_mat))
print("shape of covariance matrix:", np.shape(np.cov(cov_mat)))
print(np.cov(cov_mat))
|
#Output : [0 1 1]
|
Compute the covariance matrix of two given NumPy arrays
import numpy as np
x = [1.23, 2.12, 3.34, 4.5]
y = [2.56, 2.89, 3.76, 3.95]
# find out covariance with respect
# rows
cov_mat = np.stack((x, y), axis=1)
print("shape of matrix x and y:", np.shape(cov_mat))
print("shape of covariance matrix:", np.shape(np.cov(cov_mat)))
print(np.cov(cov_mat))
#Output : [0 1 1]
[END]
|
Compute the Kronecker product of two mulitdimension NumPy arrays
|
https://www.geeksforgeeks.org/compute-the-kronecker-product-of-two-mulitdimension-numpy-arrays/
|
# Importing required modules
import numpy
# Creating arrays
array1 = numpy.array([[1, 2], [3, 4]])
print("Array1:\n", array1)
array2 = numpy.array([[5, 6], [7, 8]])
print("\nArray2:\n", array2)
# Computing the Kronecker Product
kroneckerProduct = numpy.kron(array1, array2)
print("\nArray1 ????????? A")
print(kroneckerProduct)
|
#Output : A = |?????????(a00)?????????
|
Compute the Kronecker product of two mulitdimension NumPy arrays
# Importing required modules
import numpy
# Creating arrays
array1 = numpy.array([[1, 2], [3, 4]])
print("Array1:\n", array1)
array2 = numpy.array([[5, 6], [7, 8]])
print("\nArray2:\n", array2)
# Computing the Kronecker Product
kroneckerProduct = numpy.kron(array1, array2)
print("\nArray1 ????????? A")
print(kroneckerProduct)
#Output : A = |?????????(a00)?????????
[END]
|
Compute the Kronecker product of two mulitdimension NumPy arrays
|
https://www.geeksforgeeks.org/compute-the-kronecker-product-of-two-mulitdimension-numpy-arrays/
|
# Importing required modules
import numpy
# Creating arrays
array1 = numpy.array([[1, 2, 3]])
print("Array1:\n", array1)
array2 = numpy.array([[3, 2, 1]])
print("\nArray2:\n", array2)
# Computing the Kronecker Product
kroneckerProduct = numpy.kron(array1, array2)
print("\nArray1 ????????? A")
print(kroneckerProduct)
|
#Output : A = |?????????(a00)?????????
|
Compute the Kronecker product of two mulitdimension NumPy arrays
# Importing required modules
import numpy
# Creating arrays
array1 = numpy.array([[1, 2, 3]])
print("Array1:\n", array1)
array2 = numpy.array([[3, 2, 1]])
print("\nArray2:\n", array2)
# Computing the Kronecker Product
kroneckerProduct = numpy.kron(array1, array2)
print("\nArray1 ????????? A")
print(kroneckerProduct)
#Output : A = |?????????(a00)?????????
[END]
|
Compute the Kronecker product of two mulitdimension NumPy arrays
|
https://www.geeksforgeeks.org/compute-the-kronecker-product-of-two-mulitdimension-numpy-arrays/
|
# Importing required modules
import numpy
# Creating arrays
array1 = numpy.array([[1, 2, 3], [4, 5, 6]])
print("Array1:\n", array1)
array2 = numpy.array([[1, 2], [3, 4], [5, 6]])
print("\nArray2:\n", array2)
# Computing the Kronecker Product
kroneckerProduct = numpy.kron(array1, array2)
print("\nArray1 ????????? A")
print(kroneckerProduct)
|
#Output : A = |?????????(a00)?????????
|
Compute the Kronecker product of two mulitdimension NumPy arrays
# Importing required modules
import numpy
# Creating arrays
array1 = numpy.array([[1, 2, 3], [4, 5, 6]])
print("Array1:\n", array1)
array2 = numpy.array([[1, 2], [3, 4], [5, 6]])
print("\nArray2:\n", array2)
# Computing the Kronecker Product
kroneckerProduct = numpy.kron(array1, array2)
print("\nArray1 ????????? A")
print(kroneckerProduct)
#Output : A = |?????????(a00)?????????
[END]
|
Convert the matrix into a list
|
https://www.geeksforgeeks.org/python-numpy-matrix-tolist/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 12, 3]")
# applying matrix.tolist() method
geek = gfg.tolist()
print(geek)
|
#Output :
|
Convert the matrix into a list
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 12, 3]")
# applying matrix.tolist() method
geek = gfg.tolist()
print(geek)
#Output :
[END]
|
Convert the matrix into a list
|
https://www.geeksforgeeks.org/python-numpy-matrix-tolist/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 9; 12, 3, 1; 4, 5, 6]")
# applying matrix.tolist() method
geek = gfg.tolist()
print(geek)
|
#Output :
|
Convert the matrix into a list
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1, 9; 12, 3, 1; 4, 5, 6]")
# applying matrix.tolist() method
geek = gfg.tolist()
print(geek)
#Output :
[END]
|
Return the indices of elements where the given condition is satisfied
|
https://www.geeksforgeeks.org/numpy-where-in-python/
|
# Python program explaining
# where() function
import numpy as np
np.where([[True, False], [True, True]], [[1, 2], [3, 4]], [[5, 6], [7, 8]])
|
#Output : array([[1, 6],
|
Return the indices of elements where the given condition is satisfied
# Python program explaining
# where() function
import numpy as np
np.where([[True, False], [True, True]], [[1, 2], [3, 4]], [[5, 6], [7, 8]])
#Output : array([[1, 6],
[END]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.