Description
stringlengths 9
105
| Link
stringlengths 45
135
| Code
stringlengths 10
26.8k
| Test_Case
stringlengths 9
202
| Merge
stringlengths 63
27k
|
---|---|---|---|---|
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
# a is an array of integers.
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print("Indices of elements <4")
b = np.where(a < 4)
print(b)
print("Elements which are <4")
print(a[b])
|
#Output : array([[1, 6],
|
Return the indices of elements where the given condition is satisfied
# Python program explaining
# where() function
import numpy as np
# a is an array of integers.
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print("Indices of elements <4")
b = np.where(a < 4)
print(b)
print("Elements which are <4")
print(a[b])
#Output : array([[1, 6],
[END]
|
Replace NaN values with average of columns
|
https://www.geeksforgeeks.org/python-replace-nan-values-with-average-of-columns/
|
# Python code to demonstrate
# to replace nan values
# with an average of columns
import numpy as np
# Initialising numpy array
ini_array = np.array(
[[1.3, 2.5, 3.6, np.nan], [2.6, 3.3, np.nan, 5.5], [2.1, 3.2, 5.4, 6.5]]
)
# printing initial array
print("initial array", ini_array)
# column mean
col_mean = np.nanmean(ini_array, axis=0)
# printing column mean
print("columns mean", str(col_mean))
# find indices where nan value is present
inds = np.where(np.isnan(ini_array))
# replace inds with avg of column
ini_array[inds] = np.take(col_mean, inds[1])
# printing final array
print("final array", ini_array)
|
#Output : initial array [[ 1.3 2.5 3.6 nan]
|
Replace NaN values with average of columns
# Python code to demonstrate
# to replace nan values
# with an average of columns
import numpy as np
# Initialising numpy array
ini_array = np.array(
[[1.3, 2.5, 3.6, np.nan], [2.6, 3.3, np.nan, 5.5], [2.1, 3.2, 5.4, 6.5]]
)
# printing initial array
print("initial array", ini_array)
# column mean
col_mean = np.nanmean(ini_array, axis=0)
# printing column mean
print("columns mean", str(col_mean))
# find indices where nan value is present
inds = np.where(np.isnan(ini_array))
# replace inds with avg of column
ini_array[inds] = np.take(col_mean, inds[1])
# printing final array
print("final array", ini_array)
#Output : initial array [[ 1.3 2.5 3.6 nan]
[END]
|
Replace NaN values with average of columns
|
https://www.geeksforgeeks.org/python-replace-nan-values-with-average-of-columns/
|
# Python code to demonstrate
# to replace nan values
# with average of columns
import numpy as np
# Initialising numpy array
ini_array = np.array(
[[1.3, 2.5, 3.6, np.nan], [2.6, 3.3, np.nan, 5.5], [2.1, 3.2, 5.4, 6.5]]
)
# printing initial array
print("initial array", ini_array)
# replace nan with col means
res = np.where(
np.isnan(ini_array),
np.ma.array(ini_array, mask=np.isnan(ini_array)).mean(axis=0),
ini_array,
)
# printing final array
print("final array", res)
|
#Output : initial array [[ 1.3 2.5 3.6 nan]
|
Replace NaN values with average of columns
# Python code to demonstrate
# to replace nan values
# with average of columns
import numpy as np
# Initialising numpy array
ini_array = np.array(
[[1.3, 2.5, 3.6, np.nan], [2.6, 3.3, np.nan, 5.5], [2.1, 3.2, 5.4, 6.5]]
)
# printing initial array
print("initial array", ini_array)
# replace nan with col means
res = np.where(
np.isnan(ini_array),
np.ma.array(ini_array, mask=np.isnan(ini_array)).mean(axis=0),
ini_array,
)
# printing final array
print("final array", res)
#Output : initial array [[ 1.3 2.5 3.6 nan]
[END]
|
Replace NaN values with average of columns
|
https://www.geeksforgeeks.org/python-replace-nan-values-with-average-of-columns/
|
# Python code to demonstrate
# to replace nan values
# with average of columns
import numpy as np
# Initialising numpy array
ini_array = np.array(
[[1.3, 2.5, 3.6, np.nan], [2.6, 3.3, np.nan, 5.5], [2.1, 3.2, 5.4, 6.5]]
)
# printing initial array
print("initial array", ini_array)
# indices where values is nan in array
indices = np.where(np.isnan(ini_array))
# Iterating over numpy array to replace nan with values
for row, col in zip(*indices):
ini_array[row, col] = np.mean(ini_array[~np.isnan(ini_array[:, col]), col])
# printing final array
print("final array", ini_array)
|
#Output : initial array [[ 1.3 2.5 3.6 nan]
|
Replace NaN values with average of columns
# Python code to demonstrate
# to replace nan values
# with average of columns
import numpy as np
# Initialising numpy array
ini_array = np.array(
[[1.3, 2.5, 3.6, np.nan], [2.6, 3.3, np.nan, 5.5], [2.1, 3.2, 5.4, 6.5]]
)
# printing initial array
print("initial array", ini_array)
# indices where values is nan in array
indices = np.where(np.isnan(ini_array))
# Iterating over numpy array to replace nan with values
for row, col in zip(*indices):
ini_array[row, col] = np.mean(ini_array[~np.isnan(ini_array[:, col]), col])
# printing final array
print("final array", ini_array)
#Output : initial array [[ 1.3 2.5 3.6 nan]
[END]
|
Replace NaN values with average of columns
|
https://www.geeksforgeeks.org/python-replace-nan-values-with-average-of-columns/
|
def replace_nan_with_mean(arr):
col_means = [
sum(filter(lambda x: x is not None, col))
/ len(list(filter(lambda x: x is not None, col)))
for col in zip(*arr)
]
for i in range(len(arr)):
arr[i] = [col_means[j] if x is None else x for j, x in enumerate(arr[i])]
return arr
arr = [[1.3, 2.5, 3.6, None], [2.6, 3.3, None, 5.5], [2.1, 3.2, 5.4, 6.5]]
print(replace_nan_with_mean(arr))
|
#Output : initial array [[ 1.3 2.5 3.6 nan]
|
Replace NaN values with average of columns
def replace_nan_with_mean(arr):
col_means = [
sum(filter(lambda x: x is not None, col))
/ len(list(filter(lambda x: x is not None, col)))
for col in zip(*arr)
]
for i in range(len(arr)):
arr[i] = [col_means[j] if x is None else x for j, x in enumerate(arr[i])]
return arr
arr = [[1.3, 2.5, 3.6, None], [2.6, 3.3, None, 5.5], [2.1, 3.2, 5.4, 6.5]]
print(replace_nan_with_mean(arr))
#Output : initial array [[ 1.3 2.5 3.6 nan]
[END]
|
Replace negative value with zero in numpy array
|
https://www.geeksforgeeks.org/python-replace-negative-value-with-zero-in-numpy-array/
|
# Python code to demonstrate
# to replace negative value with 0
import numpy as np
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
# printing initial arrays
print("initial array", ini_array1)
# code to replace all negative value with 0
ini_array1[ini_array1 < 0] = 0
# printing result
print("New resulting array: ", ini_array1)
|
#Output : initial array [ 1 2 -3 4 -5 -6]
|
Replace negative value with zero in numpy array
# Python code to demonstrate
# to replace negative value with 0
import numpy as np
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
# printing initial arrays
print("initial array", ini_array1)
# code to replace all negative value with 0
ini_array1[ini_array1 < 0] = 0
# printing result
print("New resulting array: ", ini_array1)
#Output : initial array [ 1 2 -3 4 -5 -6]
[END]
|
Replace negative value with zero in numpy array
|
https://www.geeksforgeeks.org/python-replace-negative-value-with-zero-in-numpy-array/
|
# Python code to demonstrate
# to replace negative values with 0
import numpy as np
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
# printing initial arrays
print("initial array", ini_array1)
# code to replace all negative value with 0
result = np.where(ini_array1 < 0, 0, ini_array1)
# printing result
print("New resulting array: ", result)
|
#Output : initial array [ 1 2 -3 4 -5 -6]
|
Replace negative value with zero in numpy array
# Python code to demonstrate
# to replace negative values with 0
import numpy as np
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
# printing initial arrays
print("initial array", ini_array1)
# code to replace all negative value with 0
result = np.where(ini_array1 < 0, 0, ini_array1)
# printing result
print("New resulting array: ", result)
#Output : initial array [ 1 2 -3 4 -5 -6]
[END]
|
Replace negative value with zero in numpy array
|
https://www.geeksforgeeks.org/python-replace-negative-value-with-zero-in-numpy-array/
|
# Python code to demonstrate
# to replace negative values with 0
import numpy as np
# supposing maxx value array can hold
maxx = 1000
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
# printing initial arrays
print("initial array", ini_array1)
# code to replace all negative value with 0
result = np.clip(ini_array1, 0, 1000)
# printing result
print("New resulting array: ", result)
|
#Output : initial array [ 1 2 -3 4 -5 -6]
|
Replace negative value with zero in numpy array
# Python code to demonstrate
# to replace negative values with 0
import numpy as np
# supposing maxx value array can hold
maxx = 1000
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
# printing initial arrays
print("initial array", ini_array1)
# code to replace all negative value with 0
result = np.clip(ini_array1, 0, 1000)
# printing result
print("New resulting array: ", result)
#Output : initial array [ 1 2 -3 4 -5 -6]
[END]
|
Replace negative value with zero in numpy array
|
https://www.geeksforgeeks.org/python-replace-negative-value-with-zero-in-numpy-array/
|
# Python code to demonstrate
# to replace negative values with 0
import numpy as np
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
# printing initial arrays
print("initial array", ini_array1)
# Creating a array of 0
zero_array = np.zeros(ini_array1.shape, dtype=ini_array1.dtype)
print("Zero array", zero_array)
# code to replace all negative value with 0
ini_array2 = np.maximum(ini_array1, zero_array)
# printing result
print("New resulting array: ", ini_array2)
|
#Output : initial array [ 1 2 -3 4 -5 -6]
|
Replace negative value with zero in numpy array
# Python code to demonstrate
# to replace negative values with 0
import numpy as np
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
# printing initial arrays
print("initial array", ini_array1)
# Creating a array of 0
zero_array = np.zeros(ini_array1.shape, dtype=ini_array1.dtype)
print("Zero array", zero_array)
# code to replace all negative value with 0
ini_array2 = np.maximum(ini_array1, zero_array)
# printing result
print("New resulting array: ", ini_array2)
#Output : initial array [ 1 2 -3 4 -5 -6]
[END]
|
Replace negative value with zero in numpy array
|
https://www.geeksforgeeks.org/python-replace-negative-value-with-zero-in-numpy-array/
|
import numpy as np
# Initialize the array
arr = np.array([1, 2, -3, 4, -5, -6])
# Print the initial array
print("Initial array:", arr)
# Replace negative values with zeros using a lambda function
replace_negatives = np.vectorize(lambda x: 0 if x < 0 else x)
result = replace_negatives(arr)
# Print the resulting array
print("Resulting array:", result)
# This code is contributed by Edula Vinay Kumar Reddy
|
#Output : initial array [ 1 2 -3 4 -5 -6]
|
Replace negative value with zero in numpy array
import numpy as np
# Initialize the array
arr = np.array([1, 2, -3, 4, -5, -6])
# Print the initial array
print("Initial array:", arr)
# Replace negative values with zeros using a lambda function
replace_negatives = np.vectorize(lambda x: 0 if x < 0 else x)
result = replace_negatives(arr)
# Print the resulting array
print("Resulting array:", result)
# This code is contributed by Edula Vinay Kumar Reddy
#Output : initial array [ 1 2 -3 4 -5 -6]
[END]
|
Find indices of elements equal to zero in a NumPy array
|
https://www.geeksforgeeks.org/find-indices-of-elements-equal-to-zero-in-a-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 0, 2, 0, 3, 0, 0, 5, 6, 7, 5, 0, 8])
print("Original array:")
print(n_array)
# finding indices of null elements using np.where()
print(
"\nIndices of elements equal to zero of the \
given 1-D array:"
)
res = np.where(n_array == 0)[0]
print(res)
|
#Output : numpy.where(condition[, x, y])
|
Find indices of elements equal to zero in a NumPy array
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 0, 2, 0, 3, 0, 0, 5, 6, 7, 5, 0, 8])
print("Original array:")
print(n_array)
# finding indices of null elements using np.where()
print(
"\nIndices of elements equal to zero of the \
given 1-D array:"
)
res = np.where(n_array == 0)[0]
print(res)
#Output : numpy.where(condition[, x, y])
[END]
|
Find indices of elements equal to zero in a NumPy array
|
https://www.geeksforgeeks.org/find-indices-of-elements-equal-to-zero-in-a-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a 3-D Numpy array
n_array = np.array([[0, 2, 3], [4, 1, 0], [0, 0, 2]])
print("Original array:")
print(n_array)
# finding indices of null elements
# using np.argwhere()
print("\nIndices of null elements:")
res = np.argwhere(n_array == 0)
print(res)
|
#Output : numpy.where(condition[, x, y])
|
Find indices of elements equal to zero in a NumPy array
# importing Numpy package
import numpy as np
# creating a 3-D Numpy array
n_array = np.array([[0, 2, 3], [4, 1, 0], [0, 0, 2]])
print("Original array:")
print(n_array)
# finding indices of null elements
# using np.argwhere()
print("\nIndices of null elements:")
res = np.argwhere(n_array == 0)
print(res)
#Output : numpy.where(condition[, x, y])
[END]
|
Find indices of elements equal to zero in a NumPy array
|
https://www.geeksforgeeks.org/find-indices-of-elements-equal-to-zero-in-a-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 10, 2, 0, 3, 9, 0, 5, 0, 7, 5, 0, 0])
print("Original array:")
print(n_array)
# finding indices of null elements using
# np.nonzero()
print("\nIndices of null elements:")
res = np.nonzero(n_array == 0)
print(res)
|
#Output : numpy.where(condition[, x, y])
|
Find indices of elements equal to zero in a NumPy array
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 10, 2, 0, 3, 9, 0, 5, 0, 7, 5, 0, 0])
print("Original array:")
print(n_array)
# finding indices of null elements using
# np.nonzero()
print("\nIndices of null elements:")
res = np.nonzero(n_array == 0)
print(res)
#Output : numpy.where(condition[, x, y])
[END]
|
Find indices of elements equal to zero in a NumPy array
|
https://www.geeksforgeeks.org/find-indices-of-elements-equal-to-zero-in-a-numpy-array/
|
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 0, 2, 0, 3, 0, 0, 5, 6, 7, 5, 0, 8])
print("Original array:")
print(n_array)
# finding indices of null elements using np.extract()
print(
"\nIndices of elements equal to zero of the \
given 1-D array:"
)
res = np.extract(n_array == 0, np.arange(len(n_array)))
print(res)
|
#Output : numpy.where(condition[, x, y])
|
Find indices of elements equal to zero in a NumPy array
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 0, 2, 0, 3, 0, 0, 5, 6, 7, 5, 0, 8])
print("Original array:")
print(n_array)
# finding indices of null elements using np.extract()
print(
"\nIndices of elements equal to zero of the \
given 1-D array:"
)
res = np.extract(n_array == 0, np.arange(len(n_array)))
print(res)
#Output : numpy.where(condition[, x, y])
[END]
|
Get row numbers of NumPy array having element larger than X
|
https://www.geeksforgeeks.org/get-row-numbers-of-numpy-array-having-element-larger-than-x/
|
# importing library
import numpy
# create numpy array
arr = numpy.array(
[[1, 2, 3, 4, 5], [10, -3, 30, 4, 5], [3, 2, 5, -4, 5], [9, 7, 3, 6, 5]]
)
# declare specified value
X = 6
# view array
print("Given Array:\n", arr)
# finding out the row numbers
output = numpy.where(numpy.any(arr > X, axis=1))
# view output
print("Result:\n", output)
|
#Output : Arr = [[1,2,3,4,5],
|
Get row numbers of NumPy array having element larger than X
# importing library
import numpy
# create numpy array
arr = numpy.array(
[[1, 2, 3, 4, 5], [10, -3, 30, 4, 5], [3, 2, 5, -4, 5], [9, 7, 3, 6, 5]]
)
# declare specified value
X = 6
# view array
print("Given Array:\n", arr)
# finding out the row numbers
output = numpy.where(numpy.any(arr > X, axis=1))
# view output
print("Result:\n", output)
#Output : Arr = [[1,2,3,4,5],
[END]
|
Find a matrix or vector norm using NumPy
|
https://www.geeksforgeeks.org/find-a-matrix-or-vector-norm-using-numpy/
|
# import library
import numpy as np
# initialize vector
vec = np.arange(10)
# compute norm of vector
vec_norm = np.linalg.norm(vec)
print("Vector norm:")
print(vec_norm)
|
#Output : Vector norm:
|
Find a matrix or vector norm using NumPy
# import library
import numpy as np
# initialize vector
vec = np.arange(10)
# compute norm of vector
vec_norm = np.linalg.norm(vec)
print("Vector norm:")
print(vec_norm)
#Output : Vector norm:
[END]
|
Find a matrix or vector norm using NumPy
|
https://www.geeksforgeeks.org/find-a-matrix-or-vector-norm-using-numpy/
|
# import library
import numpy as np
# initialize matrix
mat = np.array([[1, 2, 3], [4, 5, 6]])
# compute norm of matrix
mat_norm = np.linalg.norm(mat)
print("Matrix norm:")
print(mat_norm)
|
#Output : Vector norm:
|
Find a matrix or vector norm using NumPy
# import library
import numpy as np
# initialize matrix
mat = np.array([[1, 2, 3], [4, 5, 6]])
# compute norm of matrix
mat_norm = np.linalg.norm(mat)
print("Matrix norm:")
print(mat_norm)
#Output : Vector norm:
[END]
|
Find a matrix or vector norm using NumPy
|
https://www.geeksforgeeks.org/find-a-matrix-or-vector-norm-using-numpy/
|
# import library
import numpy as np
mat = np.array([[1, 2, 3], [4, 5, 6]])
# compute matrix num along axis
mat_norm = np.linalg.norm(mat, axis=1)
print("Matrix norm along particular axis :")
print(mat_norm)
|
#Output : Vector norm:
|
Find a matrix or vector norm using NumPy
# import library
import numpy as np
mat = np.array([[1, 2, 3], [4, 5, 6]])
# compute matrix num along axis
mat_norm = np.linalg.norm(mat, axis=1)
print("Matrix norm along particular axis :")
print(mat_norm)
#Output : Vector norm:
[END]
|
Find a matrix or vector norm using NumPy
|
https://www.geeksforgeeks.org/find-a-matrix-or-vector-norm-using-numpy/
|
# import library
import numpy as np
# initialize vector
vec = np.arange(9)
# convert vector into matrix
mat = vec.reshape((3, 3))
# compute norm of vector
vec_norm = np.linalg.norm(vec)
print("Vector norm:")
print(vec_norm)
# computer norm of matrix
mat_norm = np.linalg.norm(mat)
print("Matrix norm:")
print(mat_norm)
|
#Output : Vector norm:
|
Find a matrix or vector norm using NumPy
# import library
import numpy as np
# initialize vector
vec = np.arange(9)
# convert vector into matrix
mat = vec.reshape((3, 3))
# compute norm of vector
vec_norm = np.linalg.norm(vec)
print("Vector norm:")
print(vec_norm)
# computer norm of matrix
mat_norm = np.linalg.norm(mat)
print("Matrix norm:")
print(mat_norm)
#Output : Vector norm:
[END]
|
Calculate the QR decomposition of a given matrix using NumPy
|
https://www.geeksforgeeks.org/calculate-the-qr-decomposition-of-a-given-matrix-using-numpy/
|
import numpy as np
# Original matrix
matrix1 = np.array([[1, 2, 3], [3, 4, 5]])
print(matrix1)
# Decomposition of the said matrix
q, r = np.linalg.qr(matrix1)
print("\nQ:\n", q)
print("\nR:\n", r)
|
#Output : [[1 2 3]
|
Calculate the QR decomposition of a given matrix using NumPy
import numpy as np
# Original matrix
matrix1 = np.array([[1, 2, 3], [3, 4, 5]])
print(matrix1)
# Decomposition of the said matrix
q, r = np.linalg.qr(matrix1)
print("\nQ:\n", q)
print("\nR:\n", r)
#Output : [[1 2 3]
[END]
|
Calculate the QR decomposition of a given matrix using NumPy
|
https://www.geeksforgeeks.org/calculate-the-qr-decomposition-of-a-given-matrix-using-numpy/
|
import numpy as np
# Original matrix
matrix1 = np.array([[1, 0], [2, 4]])
print(matrix1)
# Decomposition of the said matrix
q, r = np.linalg.qr(matrix1)
print("\nQ:\n", q)
print("\nR:\n", r)
|
#Output : [[1 2 3]
|
Calculate the QR decomposition of a given matrix using NumPy
import numpy as np
# Original matrix
matrix1 = np.array([[1, 0], [2, 4]])
print(matrix1)
# Decomposition of the said matrix
q, r = np.linalg.qr(matrix1)
print("\nQ:\n", q)
print("\nR:\n", r)
#Output : [[1 2 3]
[END]
|
Calculate the QR decomposition of a given matrix using NumPy
|
https://www.geeksforgeeks.org/calculate-the-qr-decomposition-of-a-given-matrix-using-numpy/
|
import numpy as np
# Create a numpy array
arr = np.array([[5, 11, -15], [12, 34, -51], [-24, -43, 92]], dtype=np.int32)
print(arr)
# Find the QR factor of array
q, r = np.linalg.qr(arr)
print("\nQ:\n", q)
print("\nR:\n", r)
|
#Output : [[1 2 3]
|
Calculate the QR decomposition of a given matrix using NumPy
import numpy as np
# Create a numpy array
arr = np.array([[5, 11, -15], [12, 34, -51], [-24, -43, 92]], dtype=np.int32)
print(arr)
# Find the QR factor of array
q, r = np.linalg.qr(arr)
print("\nQ:\n", q)
print("\nR:\n", r)
#Output : [[1 2 3]
[END]
|
Compute the condition number of a given matrix using NumPy
|
https://www.geeksforgeeks.org/compute-the-condition-number-of-a-given-matrix-using-numpy/
|
# Importing library
import numpy as np
# Creating a 2X2 matrix
matrix = np.array([[4, 2], [3, 1]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
|
#Output : numpy.linalg.cond(x, p=None)
|
Compute the condition number of a given matrix using NumPy
# Importing library
import numpy as np
# Creating a 2X2 matrix
matrix = np.array([[4, 2], [3, 1]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
#Output : numpy.linalg.cond(x, p=None)
[END]
|
Compute the condition number of a given matrix using NumPy
|
https://www.geeksforgeeks.org/compute-the-condition-number-of-a-given-matrix-using-numpy/
|
# Importing library
import numpy as np
# Creating a 3X3 matrix
matrix = np.array([[4, 2, 0], [3, 1, 2], [1, 6, 4]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
|
#Output : numpy.linalg.cond(x, p=None)
|
Compute the condition number of a given matrix using NumPy
# Importing library
import numpy as np
# Creating a 3X3 matrix
matrix = np.array([[4, 2, 0], [3, 1, 2], [1, 6, 4]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
#Output : numpy.linalg.cond(x, p=None)
[END]
|
Compute the condition number of a given matrix using NumPy
|
https://www.geeksforgeeks.org/compute-the-condition-number-of-a-given-matrix-using-numpy/
|
# Importing library
import numpy as np
# Creating a 4X4 matrix
matrix = np.array([[4, 1, 4, 2], [3, 1, 2, 0], [3, 5, 7, 1], [0, 6, 8, 4]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
|
#Output : numpy.linalg.cond(x, p=None)
|
Compute the condition number of a given matrix using NumPy
# Importing library
import numpy as np
# Creating a 4X4 matrix
matrix = np.array([[4, 1, 4, 2], [3, 1, 2, 0], [3, 5, 7, 1], [0, 6, 8, 4]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
#Output : numpy.linalg.cond(x, p=None)
[END]
|
Compute the eigenvalues and right eigenvectors of a given square array using NumPy?
|
https://www.geeksforgeeks.org/how-to-compute-the-eigenvalues-and-right-eigenvectors-of-a-given-square-array-using-numpy/
|
# importing numpy libraryimport numpy as np????????????# create numpy 2d-arraym = np.array([[1, 2],??????????????????????????????????????"Printing the Original square array:\n",????????????????????????????????????m)????????????# finding eigenvalues and eigenvectorsw, v = np.linalg.eig(m)?"Printing the Eigen values of the given square array:\n",????????????????????????????????????w)??????????"Printing Right eigenvectors of the given square array:\n"????????????v)
|
#Output : Suppose we have a matrix as:???
|
Compute the eigenvalues and right eigenvectors of a given square array using NumPy?
# importing numpy libraryimport numpy as np????????????# create numpy 2d-arraym = np.array([[1, 2],??????????????????????????????????????"Printing the Original square array:\n",????????????????????????????????????m)????????????# finding eigenvalues and eigenvectorsw, v = np.linalg.eig(m)?"Printing the Eigen values of the given square array:\n",????????????????????????????????????w)??????????"Printing Right eigenvectors of the given square array:\n"????????????v)
#Output : Suppose we have a matrix as:???
[END]
|
Compute the eigenvalues and right eigenvectors of a given square array using NumPy?
|
https://www.geeksforgeeks.org/how-to-compute-the-eigenvalues-and-right-eigenvectors-of-a-given-square-array-using-numpy/
|
# importing numpy library
import numpy as np
# create numpy 2d-array
m = np.array([[1, 2, 3], [2, 3, 4], [4, 5, 6]])
print("Printing the Original square array:\n", m)
# finding eigenvalues and eigenvectors
w, v = np.linalg.eig(m)
# printing eigen values
print("Printing the Eigen values of the given square array:\n", w)
# printing eigen vectors
print("Printing Right eigenvectors of the given square array:\n", v)
|
#Output : Suppose we have a matrix as:???
|
Compute the eigenvalues and right eigenvectors of a given square array using NumPy?
# importing numpy library
import numpy as np
# create numpy 2d-array
m = np.array([[1, 2, 3], [2, 3, 4], [4, 5, 6]])
print("Printing the Original square array:\n", m)
# finding eigenvalues and eigenvectors
w, v = np.linalg.eig(m)
# printing eigen values
print("Printing the Eigen values of the given square array:\n", w)
# printing eigen vectors
print("Printing Right eigenvectors of the given square array:\n", v)
#Output : Suppose we have a matrix as:???
[END]
|
Calculate the Euclidean distance using NumPy
|
https://www.geeksforgeeks.org/calculate-the-euclidean-distance-using-numpy/
|
# Python code to find Euclidean distance
# using linalg.norm()
import numpy as np
# initializing points in
# numpy arrays
point1 = np.array((1, 2, 3))
point2 = np.array((1, 1, 1))
# calculating Euclidean distance
# using linalg.norm()
dist = np.linalg.norm(point1 - point2)
# printing Euclidean distance
print(dist)
|
#Output : 2.23606797749979
|
Calculate the Euclidean distance using NumPy
# Python code to find Euclidean distance
# using linalg.norm()
import numpy as np
# initializing points in
# numpy arrays
point1 = np.array((1, 2, 3))
point2 = np.array((1, 1, 1))
# calculating Euclidean distance
# using linalg.norm()
dist = np.linalg.norm(point1 - point2)
# printing Euclidean distance
print(dist)
#Output : 2.23606797749979
[END]
|
Calculate the Euclidean distance using NumPy
|
https://www.geeksforgeeks.org/calculate-the-euclidean-distance-using-numpy/
|
# Python code to find Euclidean distance
# using dot()
import numpy as np
# initializing points in
# numpy arrays
point1 = np.array((1, 2, 3))
point2 = np.array((1, 1, 1))
# subtracting vector
temp = point1 - point2
# doing dot product
# for finding
# sum of the squares
sum_sq = np.dot(temp.T, temp)
# Doing squareroot and
# printing Euclidean distance
print(np.sqrt(sum_sq))
|
#Output : 2.23606797749979
|
Calculate the Euclidean distance using NumPy
# Python code to find Euclidean distance
# using dot()
import numpy as np
# initializing points in
# numpy arrays
point1 = np.array((1, 2, 3))
point2 = np.array((1, 1, 1))
# subtracting vector
temp = point1 - point2
# doing dot product
# for finding
# sum of the squares
sum_sq = np.dot(temp.T, temp)
# Doing squareroot and
# printing Euclidean distance
print(np.sqrt(sum_sq))
#Output : 2.23606797749979
[END]
|
Calculate the Euclidean distance using NumPy
|
https://www.geeksforgeeks.org/calculate-the-euclidean-distance-using-numpy/
|
# Python code to find Euclidean distance
# using sum() and square()
import numpy as np
# initializing points in
# numpy arrays
point1 = np.array((1, 2, 3))
point2 = np.array((1, 1, 1))
# finding sum of squares
sum_sq = np.sum(np.square(point1 - point2))
# Doing squareroot and
# printing Euclidean distance
print(np.sqrt(sum_sq))
|
#Output : 2.23606797749979
|
Calculate the Euclidean distance using NumPy
# Python code to find Euclidean distance
# using sum() and square()
import numpy as np
# initializing points in
# numpy arrays
point1 = np.array((1, 2, 3))
point2 = np.array((1, 1, 1))
# finding sum of squares
sum_sq = np.sum(np.square(point1 - point2))
# Doing squareroot and
# printing Euclidean distance
print(np.sqrt(sum_sq))
#Output : 2.23606797749979
[END]
|
Create a Numpy array with random values
|
https://www.geeksforgeeks.org/create-a-numpy-array-with-random-values-python/
|
# Python Program to create numpy array
# filled with random values
import numpy as geek
b = geek.empty(2, dtype=int)
print("Matrix b : \n", b)
a = geek.empty([2, 2], dtype=int)
print("\nMatrix a : \n", a)
|
#Output : -> shape : Number of rows
|
Create a Numpy array with random values
# Python Program to create numpy array
# filled with random values
import numpy as geek
b = geek.empty(2, dtype=int)
print("Matrix b : \n", b)
a = geek.empty([2, 2], dtype=int)
print("\nMatrix a : \n", a)
#Output : -> shape : Number of rows
[END]
|
Create a Numpy array with random values
|
https://www.geeksforgeeks.org/create-a-numpy-array-with-random-values-python/
|
# Python Program to create numpy array
# filled with random values
import numpy as geek
# Python Program to create numpy array
# filled with random values
import numpy as geek
c = geek.empty([3, 3])
print("\nMatrix c : \n", c)
d = geek.empty([5, 3], dtype=int)
print("\nMatrix d : \n", d)
|
#Output : -> shape : Number of rows
|
Create a Numpy array with random values
# Python Program to create numpy array
# filled with random values
import numpy as geek
# Python Program to create numpy array
# filled with random values
import numpy as geek
c = geek.empty([3, 3])
print("\nMatrix c : \n", c)
d = geek.empty([5, 3], dtype=int)
print("\nMatrix d : \n", d)
#Output : -> shape : Number of rows
[END]
|
How to choose elements from the list with different probability using NumPy?
|
https://www.geeksforgeeks.org/how-to-choose-elements-from-the-list-with-different-probability-using-numpy/
|
# import numpy library
import numpy as np
# create a list
num_list = [10, 20, 30, 40, 50]
# uniformly select any element
# from the list
number = np.random.choice(num_list)
print(number)
|
#Output : 50
|
How to choose elements from the list with different probability using NumPy?
# import numpy library
import numpy as np
# create a list
num_list = [10, 20, 30, 40, 50]
# uniformly select any element
# from the list
number = np.random.choice(num_list)
print(number)
#Output : 50
[END]
|
How to choose elements from the list with different probability using NumPy?
|
https://www.geeksforgeeks.org/how-to-choose-elements-from-the-list-with-different-probability-using-numpy/
|
# import numpy library
import numpy as np
# create a list
num_list = [10, 20, 30, 40, 50]
# choose index number-3rd element
# with 100% probability and other
# elements probability set to 0
# using p parameter of the
# choice() method so only
# 3rd index element selected
# every time in the list size of 3.
number_list = np.random.choice(num_list, 3, p=[0, 0, 0, 1, 0])
print(number_list)
|
#Output : 50
|
How to choose elements from the list with different probability using NumPy?
# import numpy library
import numpy as np
# create a list
num_list = [10, 20, 30, 40, 50]
# choose index number-3rd element
# with 100% probability and other
# elements probability set to 0
# using p parameter of the
# choice() method so only
# 3rd index element selected
# every time in the list size of 3.
number_list = np.random.choice(num_list, 3, p=[0, 0, 0, 1, 0])
print(number_list)
#Output : 50
[END]
|
How to choose elements from the list with different probability using NumPy?
|
https://www.geeksforgeeks.org/how-to-choose-elements-from-the-list-with-different-probability-using-numpy/
|
# import numpy library
import numpy as np
# create a list
num_list = [10, 20, 30, 40, 50]
# choose index number 2nd & 3rd element
# with 50%-50% probability and other
# elements probability set to 0
# using p parameter of the
# choice() method so 2nd &
# 3rd index elements selected
# every time in the list size of 3.
number_list = np.random.choice(num_list, 3, p=[0, 0, 0.5, 0.5, 0])
print(number_list)
|
#Output : 50
|
How to choose elements from the list with different probability using NumPy?
# import numpy library
import numpy as np
# create a list
num_list = [10, 20, 30, 40, 50]
# choose index number 2nd & 3rd element
# with 50%-50% probability and other
# elements probability set to 0
# using p parameter of the
# choice() method so 2nd &
# 3rd index elements selected
# every time in the list size of 3.
number_list = np.random.choice(num_list, 3, p=[0, 0, 0.5, 0.5, 0])
print(number_list)
#Output : 50
[END]
|
How to get weighted random choice in Python?
|
https://www.geeksforgeeks.org/how-to-get-weighted-random-choice-in-python/
|
import random
sampleList = [100, 200, 300, 400, 500]
randomList = random.choices(sampleList, weights=(10, 20, 30, 40, 50), k=5)
print(randomList)
|
#Output : [200, 300, 300, 300, 400]
|
How to get weighted random choice in Python?
import random
sampleList = [100, 200, 300, 400, 500]
randomList = random.choices(sampleList, weights=(10, 20, 30, 40, 50), k=5)
print(randomList)
#Output : [200, 300, 300, 300, 400]
[END]
|
How to get weighted random choice in Python?
|
https://www.geeksforgeeks.org/how-to-get-weighted-random-choice-in-python/
|
import random
sampleList = [100, 200, 300, 400, 500]
randomList = random.choices(sampleList, cum_weights=(5, 15, 35, 65, 100), k=5)
print(randomList)
|
#Output : [200, 300, 300, 300, 400]
|
How to get weighted random choice in Python?
import random
sampleList = [100, 200, 300, 400, 500]
randomList = random.choices(sampleList, cum_weights=(5, 15, 35, 65, 100), k=5)
print(randomList)
#Output : [200, 300, 300, 300, 400]
[END]
|
How to get weighted random choice in Python?
|
https://www.geeksforgeeks.org/how-to-get-weighted-random-choice-in-python/
|
from numpy.random import choice
sampleList = [100, 200, 300, 400, 500]
randomNumberList = choice(sampleList, 5, p=[0.05, 0.1, 0.15, 0.20, 0.5])
print(randomNumberList)
|
#Output : [200, 300, 300, 300, 400]
|
How to get weighted random choice in Python?
from numpy.random import choice
sampleList = [100, 200, 300, 400, 500]
randomNumberList = choice(sampleList, 5, p=[0.05, 0.1, 0.15, 0.20, 0.5])
print(randomNumberList)
#Output : [200, 300, 300, 300, 400]
[END]
|
Generate Random Numbers From The Uniform Distribution using NumPy
|
https://www.geeksforgeeks.org/generate-random-numbers-from-the-uniform-distribution-using-numpy/
|
# importing module
import numpy as np
# numpy.random.uniform() method
r = np.random.uniform(size=4)
# printing numbers
print(r)
|
#Output : numpy.random.uniform(low = 0.0, high = 1.0, size = None)
|
Generate Random Numbers From The Uniform Distribution using NumPy
# importing module
import numpy as np
# numpy.random.uniform() method
r = np.random.uniform(size=4)
# printing numbers
print(r)
#Output : numpy.random.uniform(low = 0.0, high = 1.0, size = None)
[END]
|
Generate Random Numbers From The Uniform Distribution using NumPy
|
https://www.geeksforgeeks.org/generate-random-numbers-from-the-uniform-distribution-using-numpy/
|
# importing module
import numpy as np
# numpy.random.uniform() method
random_array = np.random.uniform(0.0, 1.0, 5)
# printing 1D array with random numbers
print("1D Array with random values : \n", random_array)
|
#Output : numpy.random.uniform(low = 0.0, high = 1.0, size = None)
|
Generate Random Numbers From The Uniform Distribution using NumPy
# importing module
import numpy as np
# numpy.random.uniform() method
random_array = np.random.uniform(0.0, 1.0, 5)
# printing 1D array with random numbers
print("1D Array with random values : \n", random_array)
#Output : numpy.random.uniform(low = 0.0, high = 1.0, size = None)
[END]
|
Return a Matrix Rowix of random values from a uniform distribution
|
https://www.geeksforgeeks.org/numpy-matrix-operations-rand-function/
|
# Python program explaining
# numpy.matlib.rand() function
# importing matrix library from numpy
import numpy as geek
import numpy.matlib
# desired 3 x 4 random output matrix
out_mat = geek.matlib.rand((3, 4))
print("Output matrix : ", out_mat)
|
#Output :
|
Return a Matrix Rowix of random values from a uniform distribution
# Python program explaining
# numpy.matlib.rand() function
# importing matrix library from numpy
import numpy as geek
import numpy.matlib
# desired 3 x 4 random output matrix
out_mat = geek.matlib.rand((3, 4))
print("Output matrix : ", out_mat)
#Output :
[END]
|
Return a Matrix Rowix of random values from a uniform distribution
|
https://www.geeksforgeeks.org/numpy-matrix-operations-rand-function/
|
# Python program explaining
# numpy.matlib.rand() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# desired 1 x 5 random output matrix
out_mat = geek.matlib.rand(5)
print("Output matrix : ", out_mat)
|
#Output :
|
Return a Matrix Rowix of random values from a uniform distribution
# Python program explaining
# numpy.matlib.rand() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# desired 1 x 5 random output matrix
out_mat = geek.matlib.rand(5)
print("Output matrix : ", out_mat)
#Output :
[END]
|
Return a Matrix Rowix of random values from a uniform distribution
|
https://www.geeksforgeeks.org/numpy-matrix-operations-rand-function/
|
# Python program explaining
# numpy.matlib.rand() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# more than one argument given
out_mat = geek.matlib.rand((5, 3), 4)
print("Output matrix : ", out_mat)
|
#Output :
|
Return a Matrix Rowix of random values from a uniform distribution
# Python program explaining
# numpy.matlib.rand() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# more than one argument given
out_mat = geek.matlib.rand((5, 3), 4)
print("Output matrix : ", out_mat)
#Output :
[END]
|
Return a Matrix Rowix of random values from a Gaussian distribution
|
https://www.geeksforgeeks.org/numpy-matrix-operations-randn-function/
|
# Python program explaining
# numpy.matlib.randn() function
# importing matrix library from numpy
import numpy as geek
import numpy.matlib
# desired 3 x 4 random output matrix
out_mat = geek.matlib.randn((3, 4))
print("Output matrix : ", out_mat)
|
#Output :
|
Return a Matrix Rowix of random values from a Gaussian distribution
# Python program explaining
# numpy.matlib.randn() function
# importing matrix library from numpy
import numpy as geek
import numpy.matlib
# desired 3 x 4 random output matrix
out_mat = geek.matlib.randn((3, 4))
print("Output matrix : ", out_mat)
#Output :
[END]
|
Return a Matrix Rowix of random values from a Gaussian distribution
|
https://www.geeksforgeeks.org/numpy-matrix-operations-randn-function/
|
# Python program explaining
# numpy.matlib.randn() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# desired 1 x 5 random output matrix
out_mat = geek.matlib.randn(5)
print("Output matrix : ", out_mat)
|
#Output :
|
Return a Matrix Rowix of random values from a Gaussian distribution
# Python program explaining
# numpy.matlib.randn() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# desired 1 x 5 random output matrix
out_mat = geek.matlib.randn(5)
print("Output matrix : ", out_mat)
#Output :
[END]
|
Return a Matrix Rowix of random values from a Gaussian distribution
|
https://www.geeksforgeeks.org/numpy-matrix-operations-randn-function/
|
# Python program explaining
# numpy.matlib.randn() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# more than one argument given
out_mat = geek.matlib.randn((5, 3), 4)
print("Output matrix : ", out_mat)
|
#Output :
|
Return a Matrix Rowix of random values from a Gaussian distribution
# Python program explaining
# numpy.matlib.randn() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# more than one argument given
out_mat = geek.matlib.randn((5, 3), 4)
print("Output matrix : ", out_mat)
#Output :
[END]
|
Return a Matrix Rowix of random values from a Gaussian distribution
|
https://www.geeksforgeeks.org/numpy-matrix-operations-randn-function/
|
# Python program explaining
# numpy.matlib.randn() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# So, here mu = 3, sigma = 2
out_mat = 2 * geek.matlib.randn((3, 3)) + 3
print("Output matrix : ", out_mat)
|
#Output :
|
Return a Matrix Rowix of random values from a Gaussian distribution
# Python program explaining
# numpy.matlib.randn() function
# importing numpy and matrix library
import numpy as geek
import numpy.matlib
# So, here mu = 3, sigma = 2
out_mat = 2 * geek.matlib.randn((3, 3)) + 3
print("Output matrix : ", out_mat)
#Output :
[END]
|
How to get the indices of the sorted array using NumPy in Python?
|
https://www.geeksforgeeks.org/how-to-get-the-indices-of-the-sorted-array-using-numpy-in-python/
|
import numpy as np
# Original array
array = np.array([10, 52, 62, 16, 16, 54, 453])
print(array)
# Indices of the sorted elements of a
# given array
indices = np.argsort(array)
print(indices)
|
#Output : numpy.argsort(arr, axis=-1, kind=?????????quicksort?????????,
|
How to get the indices of the sorted array using NumPy in Python?
import numpy as np
# Original array
array = np.array([10, 52, 62, 16, 16, 54, 453])
print(array)
# Indices of the sorted elements of a
# given array
indices = np.argsort(array)
print(indices)
#Output : numpy.argsort(arr, axis=-1, kind=?????????quicksort?????????,
[END]
|
How to get the indices of the sorted array using NumPy in Python?
|
https://www.geeksforgeeks.org/how-to-get-the-indices-of-the-sorted-array-using-numpy-in-python/
|
import numpy as np
# Original array
array = np.array([1, 2, 3, 4, 5])
print(array)
# Indices of the sorted elements of
# a given array
indices = np.argsort(array)
print(indices)
|
#Output : numpy.argsort(arr, axis=-1, kind=?????????quicksort?????????,
|
How to get the indices of the sorted array using NumPy in Python?
import numpy as np
# Original array
array = np.array([1, 2, 3, 4, 5])
print(array)
# Indices of the sorted elements of
# a given array
indices = np.argsort(array)
print(indices)
#Output : numpy.argsort(arr, axis=-1, kind=?????????quicksort?????????,
[END]
|
How to get the indices of the sorted array using NumPy in Python?
|
https://www.geeksforgeeks.org/how-to-get-the-indices-of-the-sorted-array-using-numpy-in-python/
|
import numpy as np
# input 2d array
in_arr = np.array([[2, 0, 1], [5, 4, 3]])
print("Input array :\n", in_arr)
# output sorted array indices
out_arr1 = np.argsort(in_arr, kind="mergesort", axis=0)
print("\nOutput sorted array indices along axis 0:\n", out_arr1)
out_arr2 = np.argsort(in_arr, kind="heapsort", axis=1)
print("\nOutput sorted array indices along axis 1:\n", out_arr2)
|
#Output : numpy.argsort(arr, axis=-1, kind=?????????quicksort?????????,
|
How to get the indices of the sorted array using NumPy in Python?
import numpy as np
# input 2d array
in_arr = np.array([[2, 0, 1], [5, 4, 3]])
print("Input array :\n", in_arr)
# output sorted array indices
out_arr1 = np.argsort(in_arr, kind="mergesort", axis=0)
print("\nOutput sorted array indices along axis 0:\n", out_arr1)
out_arr2 = np.argsort(in_arr, kind="heapsort", axis=1)
print("\nOutput sorted array indices along axis 1:\n", out_arr2)
#Output : numpy.argsort(arr, axis=-1, kind=?????????quicksort?????????,
[END]
|
Finding the k smallest values of a NumPy array
|
https://www.geeksforgeeks.org/finding-the-k-smallest-values-of-a-numpy-array/
|
# importing the modules
import numpy as np
# creating the array
arr = np.array([23, 12, 1, 3, 4, 5, 6])
print("The Original Array Content")
print(arr)
# value of k
k = 4
# sorting the array
arr1 = np.sort(arr)
# k smallest number of array
print(k, "smallest elements of the array")
print(arr1[:k])
|
Input: [1,3,5,2,4,6]
k = 3
|
Finding the k smallest values of a NumPy array
# importing the modules
import numpy as np
# creating the array
arr = np.array([23, 12, 1, 3, 4, 5, 6])
print("The Original Array Content")
print(arr)
# value of k
k = 4
# sorting the array
arr1 = np.sort(arr)
# k smallest number of array
print(k, "smallest elements of the array")
print(arr1[:k])
Input: [1,3,5,2,4,6]
k = 3
[END]
|
Finding the k smallest values of a NumPy array
|
https://www.geeksforgeeks.org/finding-the-k-smallest-values-of-a-numpy-array/
|
# importing the module
import numpy as np
# creating the array
arr = np.array([23, 12, 1, 3, 4, 5, 6])
print("The Original Array Content")
print(arr)
# value of k
k = 4
# using np.argpartition()
result = np.argpartition(arr, k)
# k smallest number of array
print(k, "smallest elements of the array")
print(arr[result[:k]])
|
Input: [1,3,5,2,4,6]
k = 3
|
Finding the k smallest values of a NumPy array
# importing the module
import numpy as np
# creating the array
arr = np.array([23, 12, 1, 3, 4, 5, 6])
print("The Original Array Content")
print(arr)
# value of k
k = 4
# using np.argpartition()
result = np.argpartition(arr, k)
# k smallest number of array
print(k, "smallest elements of the array")
print(arr[result[:k]])
Input: [1,3,5,2,4,6]
k = 3
[END]
|
How to get the n-largest values of an array using NumPy?
|
https://www.geeksforgeeks.org/how-to-get-the-n-largest-values-of-an-array-using-numpy/
|
# import library
import numpy as np
# create numpy 1d-array
arr = np.array([2, 0, 1, 5, 4, 1, 9])
print("Given array:", arr)
# sort an array in
# ascending order
# np.argsort() return
# array of indices for
# sorted array
sorted_index_array = np.argsort(arr)
# sorted array
sorted_array = arr[sorted_index_array]
print("Sorted array:", sorted_array)
# we want 1 largest value
n = 1
# we are using negative
# indexing concept
# take n largest value
rslt = sorted_array[-n:]
# show the output
print("{} largest value:".format(n), rslt[0])
|
#Output : Given array: [2 0 1 5 4 1 9]
|
How to get the n-largest values of an array using NumPy?
# import library
import numpy as np
# create numpy 1d-array
arr = np.array([2, 0, 1, 5, 4, 1, 9])
print("Given array:", arr)
# sort an array in
# ascending order
# np.argsort() return
# array of indices for
# sorted array
sorted_index_array = np.argsort(arr)
# sorted array
sorted_array = arr[sorted_index_array]
print("Sorted array:", sorted_array)
# we want 1 largest value
n = 1
# we are using negative
# indexing concept
# take n largest value
rslt = sorted_array[-n:]
# show the output
print("{} largest value:".format(n), rslt[0])
#Output : Given array: [2 0 1 5 4 1 9]
[END]
|
How to get the n-largest values of an array using NumPy?
|
https://www.geeksforgeeks.org/how-to-get-the-n-largest-values-of-an-array-using-numpy/
|
# import library
import numpy as np
# create numpy 1d-array
arr = np.array([2, 0, 1, 5, 4, 1, 9])
print("Given array:", arr)
# sort an array in
# ascending order
# np.argsort() return
# array of indices for
# sorted array
sorted_index_array = np.argsort(arr)
# sorted array
sorted_array = arr[sorted_index_array]
print("Sorted array:", sorted_array)
# we want 3 largest value
n = 3
# we are using negative
# indexing concept
# find n largest value
rslt = sorted_array[-n:]
# show the output
print("{} largest value:".format(n), rslt)
|
#Output : Given array: [2 0 1 5 4 1 9]
|
How to get the n-largest values of an array using NumPy?
# import library
import numpy as np
# create numpy 1d-array
arr = np.array([2, 0, 1, 5, 4, 1, 9])
print("Given array:", arr)
# sort an array in
# ascending order
# np.argsort() return
# array of indices for
# sorted array
sorted_index_array = np.argsort(arr)
# sorted array
sorted_array = arr[sorted_index_array]
print("Sorted array:", sorted_array)
# we want 3 largest value
n = 3
# we are using negative
# indexing concept
# find n largest value
rslt = sorted_array[-n:]
# show the output
print("{} largest value:".format(n), rslt)
#Output : Given array: [2 0 1 5 4 1 9]
[END]
|
Sort the values in a matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-sort/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[4, 1; 12, 3]")
# applying matrix.sort() method
gfg.sort()
print(gfg)
|
#Output :
|
Sort the 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.sort() method
gfg.sort()
print(gfg)
#Output :
[END]
|
Sort the values in a matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-sort/
|
# 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.sort() method
gfg.sort()
print(gfg)
|
#Output :
|
Sort the 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.sort() method
gfg.sort()
print(gfg)
#Output :
[END]
|
Find the indices into a sorted array
|
https://www.geeksforgeeks.org/numpy-searchsorted-in-python/
|
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print("Input array : ", in_arr)
# the number which we want to insert
num = 4
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num)
print("Output indices to maintain sorted array : ", out_ind)
|
Input array : [2, 3, 4, 5, 6]
|
Find the indices into a sorted array
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print("Input array : ", in_arr)
# the number which we want to insert
num = 4
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num)
print("Output indices to maintain sorted array : ", out_ind)
Input array : [2, 3, 4, 5, 6]
[END]
|
Find the indices into a sorted array
|
https://www.geeksforgeeks.org/numpy-searchsorted-in-python/
|
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print("Input array : ", in_arr)
# the number which we want to insert
num = 4
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num, side="right")
print("Output indices to maintain sorted array : ", out_ind)
|
Input array : [2, 3, 4, 5, 6]
|
Find the indices into a sorted array
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print("Input array : ", in_arr)
# the number which we want to insert
num = 4
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num, side="right")
print("Output indices to maintain sorted array : ", out_ind)
Input array : [2, 3, 4, 5, 6]
[END]
|
Find the indices into a sorted array
|
https://www.geeksforgeeks.org/numpy-searchsorted-in-python/
|
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print("Input array : ", in_arr)
# the numbers which we want to insert
num = [4, 8, 0]
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num)
print("Output indices to maintain sorted array : ", out_ind)
|
Input array : [2, 3, 4, 5, 6]
|
Find the indices into a sorted array
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print("Input array : ", in_arr)
# the numbers which we want to insert
num = [4, 8, 0]
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num)
print("Output indices to maintain sorted array : ", out_ind)
Input array : [2, 3, 4, 5, 6]
[END]
|
How to get element-wise true division of an array using Numpy?
|
https://www.geeksforgeeks.org/how-to-get-element-wise-true-division-of-an-array-using-numpy/
|
# import library
import numpy as np
# create 1d-array
x = np.arange(5)
print("Original array:", x)
# apply true division
# on each array element
rslt = np.true_divide(x, 4)
print("After the element-wise division:", rslt)
|
#Output : Original array: [0 1 2 3 4]
|
How to get element-wise true division of an array using Numpy?
# import library
import numpy as np
# create 1d-array
x = np.arange(5)
print("Original array:", x)
# apply true division
# on each array element
rslt = np.true_divide(x, 4)
print("After the element-wise division:", rslt)
#Output : Original array: [0 1 2 3 4]
[END]
|
How to get element-wise true division of an array using Numpy?
|
https://www.geeksforgeeks.org/how-to-get-element-wise-true-division-of-an-array-using-numpy/
|
# import library
import numpy as np
# create a 1d-array
x = np.arange(10)
print("Original array:", x)
# apply true division
# on each array element
rslt = np.true_divide(x, 3)
print("After the element-wise division:", rslt)
|
#Output : Original array: [0 1 2 3 4]
|
How to get element-wise true division of an array using Numpy?
# import library
import numpy as np
# create a 1d-array
x = np.arange(10)
print("Original array:", x)
# apply true division
# on each array element
rslt = np.true_divide(x, 3)
print("After the element-wise division:", rslt)
#Output : Original array: [0 1 2 3 4]
[END]
|
How to calculate the element-wise absolute value of NumPy array?
|
https://www.geeksforgeeks.org/how-to-calculate-the-element-wise-absolute-value-of-numpy-array/
|
# import library
import numpy as np
# create a numpy 1d-array
array = np.array([1, -2, 3])
print("Given array:\n", array)
# find element-wise
# absolute value
rslt = np.absolute(array)
print("Absolute array:\n", rslt)
|
#Output : Given array:
|
How to calculate the element-wise absolute value of NumPy array?
# import library
import numpy as np
# create a numpy 1d-array
array = np.array([1, -2, 3])
print("Given array:\n", array)
# find element-wise
# absolute value
rslt = np.absolute(array)
print("Absolute array:\n", rslt)
#Output : Given array:
[END]
|
How to calculate the element-wise absolute value of NumPy array?
|
https://www.geeksforgeeks.org/how-to-calculate-the-element-wise-absolute-value-of-numpy-array/
|
# import library
import numpy as np
# create a numpy 2d-array
array = np.array([[1, -2, 3], [-4, 5, -6]])
print("Given array:\n", array)
# find element-wise
# absolute value
rslt = np.absolute(array)
print("Absolute array:\n", rslt)
|
#Output : Given array:
|
How to calculate the element-wise absolute value of NumPy array?
# import library
import numpy as np
# create a numpy 2d-array
array = np.array([[1, -2, 3], [-4, 5, -6]])
print("Given array:\n", array)
# find element-wise
# absolute value
rslt = np.absolute(array)
print("Absolute array:\n", rslt)
#Output : Given array:
[END]
|
How to calculate the element-wise absolute value of NumPy array?
|
https://www.geeksforgeeks.org/how-to-calculate-the-element-wise-absolute-value-of-numpy-array/
|
# import library
import numpy as np
# create a numpy 3d-array
array = np.array([[[1, -2, 3], [-4, 5, -6]], [[-7.5, -8.22, 9.0], [10.0, 11.5, -12.5]]])
print("Given array:\n", array)
# find element-wise
# absolute value
rslt = np.absolute(array)
print("Absolute array:\n", rslt)
|
#Output : Given array:
|
How to calculate the element-wise absolute value of NumPy array?
# import library
import numpy as np
# create a numpy 3d-array
array = np.array([[[1, -2, 3], [-4, 5, -6]], [[-7.5, -8.22, 9.0], [10.0, 11.5, -12.5]]])
print("Given array:\n", array)
# find element-wise
# absolute value
rslt = np.absolute(array)
print("Absolute array:\n", rslt)
#Output : Given array:
[END]
|
Compute the negative of the NumPy array
|
https://www.geeksforgeeks.org/numpy-negative-in-python/
|
# Python program explaining
# numpy.negative() function
import numpy as geek
in_num = 10
print("Input number : ", in_num)
out_num = geek.negative(in_num)
print("negative of input number : ", out_num)
|
Input number : 10
negative of input number : -10
|
Compute the negative of the NumPy array
# Python program explaining
# numpy.negative() function
import numpy as geek
in_num = 10
print("Input number : ", in_num)
out_num = geek.negative(in_num)
print("negative of input number : ", out_num)
Input number : 10
negative of input number : -10
[END]
|
Compute the negative of the NumPy array
|
https://www.geeksforgeeks.org/numpy-negative-in-python/
|
# Python program explaining
# numpy.negative function
import numpy as geek
in_arr = geek.array([[2, -7, 5], [-6, 2, 0]])
print("Input array : ", in_arr)
out_arr = geek.negative(in_arr)
print("negative of array elements: ", out_arr)
|
Input number : 10
negative of input number : -10
|
Compute the negative of the NumPy array
# Python program explaining
# numpy.negative function
import numpy as geek
in_arr = geek.array([[2, -7, 5], [-6, 2, 0]])
print("Input array : ", in_arr)
out_arr = geek.negative(in_arr)
print("negative of array elements: ", out_arr)
Input number : 10
negative of input number : -10
[END]
|
Multiply 2d numpy array corresponding to 1d array
|
https://www.geeksforgeeks.org/python-multiply-2d-numpy-array-corresponding-to-1d-array/
|
# Python code to demonstrate
# multiplication of 2d array
# with 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
ini_array2 = np.array([0, 2, 3])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1 * ini_array2[:, np.newaxis]
# printing result
print("New resulting array: ", result)
|
#Output :
|
Multiply 2d numpy array corresponding to 1d array
# Python code to demonstrate
# multiplication of 2d array
# with 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
ini_array2 = np.array([0, 2, 3])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1 * ini_array2[:, np.newaxis]
# printing result
print("New resulting array: ", result)
#Output :
[END]
|
Multiply 2d numpy array corresponding to 1d array
|
https://www.geeksforgeeks.org/python-multiply-2d-numpy-array-corresponding-to-1d-array/
|
# Python code to demonstrate
# multiplication of 2d array
# with 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
ini_array2 = np.array([0, 2, 3])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1 * ini_array2[:, None]
# printing result
print("New resulting array: ", result)
|
#Output :
|
Multiply 2d numpy array corresponding to 1d array
# Python code to demonstrate
# multiplication of 2d array
# with 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
ini_array2 = np.array([0, 2, 3])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = ini_array1 * ini_array2[:, None]
# printing result
print("New resulting array: ", result)
#Output :
[END]
|
Multiply 2d numpy array corresponding to 1d array
|
https://www.geeksforgeeks.org/python-multiply-2d-numpy-array-corresponding-to-1d-array/
|
# python code to demonstrate
# multiplication of 2d array
# with 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
ini_array2 = np.array([0, 2, 3])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = (ini_array1.T * ini_array2).T
# printing result
print("New resulting array: ", result)
|
#Output :
|
Multiply 2d numpy array corresponding to 1d array
# python code to demonstrate
# multiplication of 2d array
# with 1d array
import numpy as np
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
ini_array2 = np.array([0, 2, 3])
# printing initial arrays
print("initial array", str(ini_array1))
# Multiplying arrays
result = (ini_array1.T * ini_array2).T
# printing result
print("New resulting array: ", result)
#Output :
[END]
|
Computes the inner product of two arrays
|
https://www.geeksforgeeks.org/numpy-inner-in-python/
|
# Python Program illustrating
# numpy.inner() method
import numpy as geek
# Scalars
product = geek.inner(5, 4)
print("inner Product of scalar values : ", product)
# 1D array
vector_a = 2 + 3j
vector_b = 4 + 5j
product = geek.inner(vector_a, vector_b)
print("inner Product : ", product)
|
#Output : Parameters :
|
Computes the inner product of two arrays
# Python Program illustrating
# numpy.inner() method
import numpy as geek
# Scalars
product = geek.inner(5, 4)
print("inner Product of scalar values : ", product)
# 1D array
vector_a = 2 + 3j
vector_b = 4 + 5j
product = geek.inner(vector_a, vector_b)
print("inner Product : ", product)
#Output : Parameters :
[END]
|
Computes the inner product of two arrays
|
https://www.geeksforgeeks.org/numpy-inner-in-python/
|
# Python Program illustrating
# numpy.inner() method
import numpy as geek
# 1D array
vector_a = geek.array([[1, 4], [5, 6]])
vector_b = geek.array([[2, 4], [5, 2]])
product = geek.inner(vector_a, vector_b)
print("inner Product : \n", product)
product = geek.inner(vector_b, vector_a)
print("\ninner Product : \n", product)
|
#Output : Parameters :
|
Computes the inner product of two arrays
# Python Program illustrating
# numpy.inner() method
import numpy as geek
# 1D array
vector_a = geek.array([[1, 4], [5, 6]])
vector_b = geek.array([[2, 4], [5, 2]])
product = geek.inner(vector_a, vector_b)
print("inner Product : \n", product)
product = geek.inner(vector_b, vector_a)
print("\ninner Product : \n", product)
#Output : Parameters :
[END]
|
Compute the nth percentile of the NumPy array
|
https://www.geeksforgeeks.org/numpy-percentile-in-python/
|
# Python Program illustrating
# numpy.percentile() method
import numpy as np
# 1D array
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("50th percentile of arr : ", np.percentile(arr, 50))
print("25th percentile of arr : ", np.percentile(arr, 25))
print("75th percentile of arr : ", np.percentile(arr, 75))
|
#Output : arr : [20, 2, 7, 1, 34]
|
Compute the nth percentile of the NumPy array
# Python Program illustrating
# numpy.percentile() method
import numpy as np
# 1D array
arr = [20, 2, 7, 1, 34]
print("arr : ", arr)
print("50th percentile of arr : ", np.percentile(arr, 50))
print("25th percentile of arr : ", np.percentile(arr, 25))
print("75th percentile of arr : ", np.percentile(arr, 75))
#Output : arr : [20, 2, 7, 1, 34]
[END]
|
Compute the nth percentile of the NumPy array
|
https://www.geeksforgeeks.org/numpy-percentile-in-python/
|
# Python Program illustrating
# numpy.percentile() method
import numpy as np
# 2D array
arr = [
[14, 17, 12, 33, 44],
[15, 6, 27, 8, 19],
[
23,
2,
54,
1,
4,
],
]
print("\narr : \n", arr)
# Percentile of the flattened array
print("\n50th Percentile of arr, axis = None : ", np.percentile(arr, 50))
print("0th Percentile of arr, axis = None : ", np.percentile(arr, 0))
# Percentile along the axis = 0
print("\n50th Percentile of arr, axis = 0 : ", np.percentile(arr, 50, axis=0))
print("0th Percentile of arr, axis = 0 : ", np.percentile(arr, 0, axis=0))
|
#Output : arr : [20, 2, 7, 1, 34]
|
Compute the nth percentile of the NumPy array
# Python Program illustrating
# numpy.percentile() method
import numpy as np
# 2D array
arr = [
[14, 17, 12, 33, 44],
[15, 6, 27, 8, 19],
[
23,
2,
54,
1,
4,
],
]
print("\narr : \n", arr)
# Percentile of the flattened array
print("\n50th Percentile of arr, axis = None : ", np.percentile(arr, 50))
print("0th Percentile of arr, axis = None : ", np.percentile(arr, 0))
# Percentile along the axis = 0
print("\n50th Percentile of arr, axis = 0 : ", np.percentile(arr, 50, axis=0))
print("0th Percentile of arr, axis = 0 : ", np.percentile(arr, 0, axis=0))
#Output : arr : [20, 2, 7, 1, 34]
[END]
|
Compute the nth percentile of the NumPy array
|
https://www.geeksforgeeks.org/numpy-percentile-in-python/
|
# Python Program illustrating
# numpy.percentile() method
import numpy as np
# 2D array
arr = [
[14, 17, 12, 33, 44],
[15, 6, 27, 8, 19],
[
23,
2,
54,
1,
4,
],
]
print("\narr : \n", arr)
# Percentile along the axis = 1
print("\n50th Percentile of arr, axis = 1 : ", np.percentile(arr, 50, axis=1))
print("0th Percentile of arr, axis = 1 : ", np.percentile(arr, 0, axis=1))
print(
"\n0th Percentile of arr, axis = 1 : \n",
np.percentile(arr, 50, axis=1, keepdims=True),
)
print(
"\n0th Percentile of arr, axis = 1 : \n",
np.percentile(arr, 0, axis=1, keepdims=True),
)
|
#Output : arr : [20, 2, 7, 1, 34]
|
Compute the nth percentile of the NumPy array
# Python Program illustrating
# numpy.percentile() method
import numpy as np
# 2D array
arr = [
[14, 17, 12, 33, 44],
[15, 6, 27, 8, 19],
[
23,
2,
54,
1,
4,
],
]
print("\narr : \n", arr)
# Percentile along the axis = 1
print("\n50th Percentile of arr, axis = 1 : ", np.percentile(arr, 50, axis=1))
print("0th Percentile of arr, axis = 1 : ", np.percentile(arr, 0, axis=1))
print(
"\n0th Percentile of arr, axis = 1 : \n",
np.percentile(arr, 50, axis=1, keepdims=True),
)
print(
"\n0th Percentile of arr, axis = 1 : \n",
np.percentile(arr, 0, axis=1, keepdims=True),
)
#Output : arr : [20, 2, 7, 1, 34]
[END]
|
Calculate the n-th order discrete difference along the given axis
|
https://www.geeksforgeeks.org/numpy-diff-in-python/
|
# Python program explaining
# numpy.diff() method
# importing numpy
import numpy as geek
# input array
arr = geek.array([1, 3, 4, 7, 9])
print("Input array : ", arr)
print("First order difference : ", geek.diff(arr))
print("Second order difference : ", geek.diff(arr, n=2))
print("Third order difference : ", geek.diff(arr, n=3))
|
Input array : [1 3 4 7 9]
First order difference : [2 1 3 2]
|
Calculate the n-th order discrete difference along the given axis
# Python program explaining
# numpy.diff() method
# importing numpy
import numpy as geek
# input array
arr = geek.array([1, 3, 4, 7, 9])
print("Input array : ", arr)
print("First order difference : ", geek.diff(arr))
print("Second order difference : ", geek.diff(arr, n=2))
print("Third order difference : ", geek.diff(arr, n=3))
Input array : [1 3 4 7 9]
First order difference : [2 1 3 2]
[END]
|
Calculate the n-th order discrete difference along the given axis
|
https://www.geeksforgeeks.org/numpy-diff-in-python/
|
# Python program explaining
# numpy.diff() method
# importing numpy
import numpy as geek
# input array
arr = geek.array([[1, 2, 3, 5], [4, 6, 7, 9]])
print("Input array : ", arr)
print("Difference when axis is 0 : ", geek.diff(arr, axis=0))
print("Difference when axis is 1 : ", geek.diff(arr, axis=1))
|
Input array : [1 3 4 7 9]
First order difference : [2 1 3 2]
|
Calculate the n-th order discrete difference along the given axis
# Python program explaining
# numpy.diff() method
# importing numpy
import numpy as geek
# input array
arr = geek.array([[1, 2, 3, 5], [4, 6, 7, 9]])
print("Input array : ", arr)
print("Difference when axis is 0 : ", geek.diff(arr, axis=0))
print("Difference when axis is 1 : ", geek.diff(arr, axis=1))
Input array : [1 3 4 7 9]
First order difference : [2 1 3 2]
[END]
|
Calculate the sum of all columns in a 2D NumPy array
|
https://www.geeksforgeeks.org/calculate-the-sum-of-all-columns-in-a-2d-numpy-array/
|
# importing required libraries
import numpy
# explicit function to compute column wise sum
def colsum(arr, n, m):
for i in range(n):
su = 0
for j in range(m):
su += arr[j][i]
print(su, end=" ")
# creating the 2D Array
TwoDList = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
TwoDArray = numpy.array(TwoDList)
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
# printing the sum of each column
print("\nColumn-wise Sum:")
colsum(TwoDArray, len(TwoDArray[0]), len(TwoDArray))
|
#Output : 2D Array:
|
Calculate the sum of all columns in a 2D NumPy array
# importing required libraries
import numpy
# explicit function to compute column wise sum
def colsum(arr, n, m):
for i in range(n):
su = 0
for j in range(m):
su += arr[j][i]
print(su, end=" ")
# creating the 2D Array
TwoDList = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
TwoDArray = numpy.array(TwoDList)
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
# printing the sum of each column
print("\nColumn-wise Sum:")
colsum(TwoDArray, len(TwoDArray[0]), len(TwoDArray))
#Output : 2D Array:
[END]
|
Calculate the sum of all columns in a 2D NumPy array
|
https://www.geeksforgeeks.org/calculate-the-sum-of-all-columns-in-a-2d-numpy-array/
|
# importing required libraries
import numpy
# explicit function to compute column wise sum
def colsum(arr, n, m):
for i in range(n):
su = 0
for j in range(m):
su += arr[j][i]
print(su, end=" ")
# creating the 2D Array
TwoDList = [[1.2, 2.3], [3.4, 4.5]]
TwoDArray = numpy.array(TwoDList)
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
# printing the sum of each column
print("\nColumn-wise Sum:")
colsum(TwoDArray, len(TwoDArray[0]), len(TwoDArray))
|
#Output : 2D Array:
|
Calculate the sum of all columns in a 2D NumPy array
# importing required libraries
import numpy
# explicit function to compute column wise sum
def colsum(arr, n, m):
for i in range(n):
su = 0
for j in range(m):
su += arr[j][i]
print(su, end=" ")
# creating the 2D Array
TwoDList = [[1.2, 2.3], [3.4, 4.5]]
TwoDArray = numpy.array(TwoDList)
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
# printing the sum of each column
print("\nColumn-wise Sum:")
colsum(TwoDArray, len(TwoDArray[0]), len(TwoDArray))
#Output : 2D Array:
[END]
|
Calculate the sum of all columns in a 2D NumPy array
|
https://www.geeksforgeeks.org/calculate-the-sum-of-all-columns-in-a-2d-numpy-array/
|
# importing required libraries
import numpy
# creating the 2D Array
TwoDList = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
TwoDArray = numpy.array(TwoDList)
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
# printing the sum of each column
print("\nColumn-wise Sum:")
print(numpy.sum(TwoDArray, axis=0))
|
#Output : 2D Array:
|
Calculate the sum of all columns in a 2D NumPy array
# importing required libraries
import numpy
# creating the 2D Array
TwoDList = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
TwoDArray = numpy.array(TwoDList)
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
# printing the sum of each column
print("\nColumn-wise Sum:")
print(numpy.sum(TwoDArray, axis=0))
#Output : 2D Array:
[END]
|
Calculate the sum of all columns in a 2D NumPy array
|
https://www.geeksforgeeks.org/calculate-the-sum-of-all-columns-in-a-2d-numpy-array/
|
# importing required libraries
import numpy
# creating the 2D Array
TwoDList = [[1.2, 2.3], [3.4, 4.5]]
TwoDArray = numpy.array(TwoDList)
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
# printing the sum of each column
print("\nColumn-wise Sum:")
print(*numpy.sum(TwoDArray, axis=0))
|
#Output : 2D Array:
|
Calculate the sum of all columns in a 2D NumPy array
# importing required libraries
import numpy
# creating the 2D Array
TwoDList = [[1.2, 2.3], [3.4, 4.5]]
TwoDArray = numpy.array(TwoDList)
# displaying the 2D Array
print("2D Array:")
print(TwoDArray)
# printing the sum of each column
print("\nColumn-wise Sum:")
print(*numpy.sum(TwoDArray, axis=0))
#Output : 2D Array:
[END]
|
Calculate average values of two given NumPy arrays
|
https://www.geeksforgeeks.org/calculate-average-values-of-two-given-numpy-arrays/
|
# import library
import numpy as np
# create a numpy 1d-arrays
arr1 = np.array([3, 4])
arr2 = np.array([1, 0])
# find average of NumPy arrays
avg = (arr1 + arr2) / 2
print("Average of NumPy arrays:\n", avg)
|
#Output : Average of NumPy arrays:
|
Calculate average values of two given NumPy arrays
# import library
import numpy as np
# create a numpy 1d-arrays
arr1 = np.array([3, 4])
arr2 = np.array([1, 0])
# find average of NumPy arrays
avg = (arr1 + arr2) / 2
print("Average of NumPy arrays:\n", avg)
#Output : Average of NumPy arrays:
[END]
|
Calculate average values of two given NumPy arrays
|
https://www.geeksforgeeks.org/calculate-average-values-of-two-given-numpy-arrays/
|
# import library
import numpy as np
# create a numpy 2d-arrays
arr1 = np.array([[3, 4], [8, 2]])
arr2 = np.array([[1, 0], [6, 6]])
# find average of NumPy arrays
avg = (arr1 + arr2) / 2
print("Average of NumPy arrays:\n", avg)
|
#Output : Average of NumPy arrays:
|
Calculate average values of two given NumPy arrays
# import library
import numpy as np
# create a numpy 2d-arrays
arr1 = np.array([[3, 4], [8, 2]])
arr2 = np.array([[1, 0], [6, 6]])
# find average of NumPy arrays
avg = (arr1 + arr2) / 2
print("Average of NumPy arrays:\n", avg)
#Output : Average of NumPy arrays:
[END]
|
How to compute numerical negative value for all elements in a given NumPy array?
|
https://www.geeksforgeeks.org/how-to-compute-numerical-negative-value-for-all-elements-in-a-given-numpy-array/
|
# importing library
import numpy as np
# creating a array
x = np.array([-1, -2, -3, 1, 2, 3, 0])
print("Printing the Original array:", x)
# converting array elements to
# its corresponding negative value
r1 = np.negative(x)
print("Printing the negative value of the given array:", r1)
|
#Output : A = [1,2,3,-1,-2,-3,0]
|
How to compute numerical negative value for all elements in a given NumPy array?
# importing library
import numpy as np
# creating a array
x = np.array([-1, -2, -3, 1, 2, 3, 0])
print("Printing the Original array:", x)
# converting array elements to
# its corresponding negative value
r1 = np.negative(x)
print("Printing the negative value of the given array:", r1)
#Output : A = [1,2,3,-1,-2,-3,0]
[END]
|
How to compute numerical negative value for all elements in a given NumPy array?
|
https://www.geeksforgeeks.org/how-to-compute-numerical-negative-value-for-all-elements-in-a-given-numpy-array/
|
# importing library
import numpy as np
# creating a numpy 2D array
x = np.array([[1, 2], [2, 3]])
print("Printing the Original array Content:\n", x)
# converting array elements to
# its corresponding negative value
r1 = np.negative(x)
print("Printing the negative value of the given array:\n", r1)
|
#Output : A = [1,2,3,-1,-2,-3,0]
|
How to compute numerical negative value for all elements in a given NumPy array?
# importing library
import numpy as np
# creating a numpy 2D array
x = np.array([[1, 2], [2, 3]])
print("Printing the Original array Content:\n", x)
# converting array elements to
# its corresponding negative value
r1 = np.negative(x)
print("Printing the negative value of the given array:\n", r1)
#Output : A = [1,2,3,-1,-2,-3,0]
[END]
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
|
https://www.geeksforgeeks.org/how-to-get-the-floor-ceiling-and-truncated-values-of-the-elements-of-a-numpy-array/
|
# Import the numpy library
import numpy as np
# Initialize numpy array
a = np.array([1.2])
# Get floor value
a = np.floor(a)
print(a)
|
#Output : import numpy as np
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
# Import the numpy library
import numpy as np
# Initialize numpy array
a = np.array([1.2])
# Get floor value
a = np.floor(a)
print(a)
#Output : import numpy as np
[END]
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
|
https://www.geeksforgeeks.org/how-to-get-the-floor-ceiling-and-truncated-values-of-the-elements-of-a-numpy-array/
|
import numpy as np
a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0])
a = np.floor(a)
print(a)
|
#Output : import numpy as np
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
import numpy as np
a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0])
a = np.floor(a)
print(a)
#Output : import numpy as np
[END]
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
|
https://www.geeksforgeeks.org/how-to-get-the-floor-ceiling-and-truncated-values-of-the-elements-of-a-numpy-array/
|
# Import the numpy library
import numpy as np
# Initialize numpy array
a = np.array([1.2])
# Get ceil value
a = np.ceil(a)
print(a)
|
#Output : import numpy as np
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
# Import the numpy library
import numpy as np
# Initialize numpy array
a = np.array([1.2])
# Get ceil value
a = np.ceil(a)
print(a)
#Output : import numpy as np
[END]
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
|
https://www.geeksforgeeks.org/how-to-get-the-floor-ceiling-and-truncated-values-of-the-elements-of-a-numpy-array/
|
import numpy as np
a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0])
a = np.ceil(a)
print(a)
|
#Output : import numpy as np
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
import numpy as np
a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0])
a = np.ceil(a)
print(a)
#Output : import numpy as np
[END]
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
|
https://www.geeksforgeeks.org/how-to-get-the-floor-ceiling-and-truncated-values-of-the-elements-of-a-numpy-array/
|
# Import the numpy library
import numpy as np
# Initialize numpy array
a = np.array([1.2])
# Get truncate value
a = np.trunc(a)
print(a)
|
#Output : import numpy as np
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
# Import the numpy library
import numpy as np
# Initialize numpy array
a = np.array([1.2])
# Get truncate value
a = np.trunc(a)
print(a)
#Output : import numpy as np
[END]
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
|
https://www.geeksforgeeks.org/how-to-get-the-floor-ceiling-and-truncated-values-of-the-elements-of-a-numpy-array/
|
import numpy as np
a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0])
a = np.trunc(a)
print(a)
|
#Output : import numpy as np
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
import numpy as np
a = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0])
a = np.trunc(a)
print(a)
#Output : import numpy as np
[END]
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
|
https://www.geeksforgeeks.org/how-to-get-the-floor-ceiling-and-truncated-values-of-the-elements-of-a-numpy-array/
|
import numpy as np
input_arr = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0])
print(input_arr)
floor_values = np.floor(input_arr)
print("\nFloor values : \n", floor_values)
ceil_values = np.ceil(input_arr)
print("\nCeil values : \n", ceil_values)
trunc_values = np.trunc(input_arr)
print("\nTruncated values : \n", trunc_values)
|
#Output : import numpy as np
|
How to get the floor, ceiling and truncated values of the elements of a numpy array?
import numpy as np
input_arr = np.array([-1.8, -1.6, -0.5, 0.5, 1.6, 1.8, 3.0])
print(input_arr)
floor_values = np.floor(input_arr)
print("\nFloor values : \n", floor_values)
ceil_values = np.ceil(input_arr)
print("\nCeil values : \n", ceil_values)
trunc_values = np.trunc(input_arr)
print("\nTruncated values : \n", trunc_values)
#Output : import numpy as np
[END]
|
Find the round off the values of the given matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-round/
|
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[6.4, 1.3; 12.7, 32.3]")
# applying matrix.round() method
geeks = gfg.round()
print(geeks)
|
#Output :
|
Find the round off the values of the given matrix
# import the important module in python
import numpy as np
# make matrix with numpy
gfg = np.matrix("[6.4, 1.3; 12.7, 32.3]")
# applying matrix.round() method
geeks = gfg.round()
print(geeks)
#Output :
[END]
|
Find the round off the values of the given matrix
|
https://www.geeksforgeeks.org/python-numpy-matrix-round/
|
# import the important module in python
import numpy as np
# make a matrix with numpy
gfg = np.matrix("[1.2, 2.3; 4.7, 5.5; 7.2, 8.9]")
# applying matrix.round() method
geeks = gfg.round()
print(geeks)
|
#Output :
|
Find the round off the values of the given matrix
# import the important module in python
import numpy as np
# make a matrix with numpy
gfg = np.matrix("[1.2, 2.3; 4.7, 5.5; 7.2, 8.9]")
# applying matrix.round() method
geeks = gfg.round()
print(geeks)
#Output :
[END]
|
Evaluate Einsteins summation convention of two multidimensional NumPy
|
https://www.geeksforgeeks.org/evaluate-einsteins-summation-convention-of-two-multidimensional-numpy-arrays/
|
# Importing library
import numpy as np
# Creating two 2X2 matrix
matrix1 = np.array([[1, 2], [0, 2]])
matrix2 = np.array([[0, 1], [3, 4]])
print("Original matrix:")
print(matrix1)
print(matrix2)
# Output
result = np.einsum("mk,kn", matrix1, matrix2)
print("Einstein?????????s summation convention of the two m")
print(result)
|
#Output : Original matrix:
|
Evaluate Einsteins summation convention of two multidimensional NumPy
# Importing library
import numpy as np
# Creating two 2X2 matrix
matrix1 = np.array([[1, 2], [0, 2]])
matrix2 = np.array([[0, 1], [3, 4]])
print("Original matrix:")
print(matrix1)
print(matrix2)
# Output
result = np.einsum("mk,kn", matrix1, matrix2)
print("Einstein?????????s summation convention of the two m")
print(result)
#Output : Original matrix:
[END]
|
Evaluate Einsteins summation convention of two multidimensional NumPy
|
https://www.geeksforgeeks.org/evaluate-einsteins-summation-convention-of-two-multidimensional-numpy-arrays/
|
# Importing library
import numpy as np
# Creating two 3X3 matrix
matrix1 = np.array([[2, 3, 5], [4, 0, 2], [0, 6, 8]])
matrix2 = np.array([[0, 1, 5], [3, 4, 4], [8, 3, 0]])
print("Original matrix:")
print(matrix1)
print(matrix2)
# Output
result = np.einsum("mk,kn", matrix1, matrix2)
print("Einstein?????????s summation convention of the two m")
print(result)
|
#Output : Original matrix:
|
Evaluate Einsteins summation convention of two multidimensional NumPy
# Importing library
import numpy as np
# Creating two 3X3 matrix
matrix1 = np.array([[2, 3, 5], [4, 0, 2], [0, 6, 8]])
matrix2 = np.array([[0, 1, 5], [3, 4, 4], [8, 3, 0]])
print("Original matrix:")
print(matrix1)
print(matrix2)
# Output
result = np.einsum("mk,kn", matrix1, matrix2)
print("Einstein?????????s summation convention of the two m")
print(result)
#Output : Original matrix:
[END]
|
Evaluate Einsteins summation convention of two multidimensional NumPy
|
https://www.geeksforgeeks.org/evaluate-einsteins-summation-convention-of-two-multidimensional-numpy-arrays/
|
# Importing library
import numpy as np
# Creating two 4X4 matrix
matrix1 = np.array([[1, 2, 3, 5], [4, 4, 0, 2], [0, 1, 6, 8], [0, 5, 6, 9]])
matrix2 = np.array([[0, 1, 9, 2], [3, 3, 4, 4], [1, 8, 3, 0], [5, 2, 1, 6]])
print("Original matrix:")
print(matrix1)
print(matrix2)
# Output
result = np.einsum("mk,kn", matrix1, matrix2)
print("Einstein?????????s summation convention of the two m")
print(result)
|
#Output : Original matrix:
|
Evaluate Einsteins summation convention of two multidimensional NumPy
# Importing library
import numpy as np
# Creating two 4X4 matrix
matrix1 = np.array([[1, 2, 3, 5], [4, 4, 0, 2], [0, 1, 6, 8], [0, 5, 6, 9]])
matrix2 = np.array([[0, 1, 9, 2], [3, 3, 4, 4], [1, 8, 3, 0], [5, 2, 1, 6]])
print("Original matrix:")
print(matrix1)
print(matrix2)
# Output
result = np.einsum("mk,kn", matrix1, matrix2)
print("Einstein?????????s summation convention of the two m")
print(result)
#Output : Original matrix:
[END]
|
Compute the median of the flattened NumPy array
|
https://www.geeksforgeeks.org/compute-the-median-of-the-flattened-numpy-array/
|
# importing numpy as library
import numpy as np
# creating 1 D array with odd no of
# elements
x_odd = np.array([1, 2, 3, 4, 5, 6, 7])
print("\nPrinting the Original array:")
print(x_odd)
# calculating median
med_odd = np.median(x_odd)
print(
"\nMedian of the array that contains \
odd no of elements:"
)
print(med_odd)
|
#Output : numpy.median(arr, axis = None)
|
Compute the median of the flattened NumPy array
# importing numpy as library
import numpy as np
# creating 1 D array with odd no of
# elements
x_odd = np.array([1, 2, 3, 4, 5, 6, 7])
print("\nPrinting the Original array:")
print(x_odd)
# calculating median
med_odd = np.median(x_odd)
print(
"\nMedian of the array that contains \
odd no of elements:"
)
print(med_odd)
#Output : numpy.median(arr, axis = None)
[END]
|
Compute the median of the flattened NumPy array
|
https://www.geeksforgeeks.org/compute-the-median-of-the-flattened-numpy-array/
|
# importing numpy as library
import numpy as np
# creating 1 D array with even no of
# elements
x_even = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print("\nPrinting the Original array:")
print(x_even)
# calculating median
med_even = np.median(x_even)
print(
"\nMedian of the array that contains \
even no of elements:"
)
print(med_even)
|
#Output : numpy.median(arr, axis = None)
|
Compute the median of the flattened NumPy array
# importing numpy as library
import numpy as np
# creating 1 D array with even no of
# elements
x_even = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print("\nPrinting the Original array:")
print(x_even)
# calculating median
med_even = np.median(x_even)
print(
"\nMedian of the array that contains \
even no of elements:"
)
print(med_even)
#Output : numpy.median(arr, axis = None)
[END]
|
Find Mean of a List of Numpy Array
|
https://www.geeksforgeeks.org/python-find-mean-of-a-list-of-numpy-array/
|
# Python code to find mean of every numpy array in list
# Importing module
import numpy as np
# List Initialization
Input = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])]
# Output list initialization
Output = []
# using np.mean()
for i in range(len(Input)):
Output.append(np.mean(Input[i]))
# Printing output
print(Output)
|
#Output :
|
Find Mean of a List of Numpy Array
# Python code to find mean of every numpy array in list
# Importing module
import numpy as np
# List Initialization
Input = [np.array([1, 2, 3]), np.array([4, 5, 6]), np.array([7, 8, 9])]
# Output list initialization
Output = []
# using np.mean()
for i in range(len(Input)):
Output.append(np.mean(Input[i]))
# Printing output
print(Output)
#Output :
[END]
|
Find Mean of a List of Numpy Array
|
https://www.geeksforgeeks.org/python-find-mean-of-a-list-of-numpy-array/
|
# Python code to find mean of
# every numpy array in list
# Importing module
import numpy as np
# List Initialization
Input = [np.array([11, 12, 13]), np.array([14, 15, 16]), np.array([17, 18, 19])]
# Output list initialization
Output = []
# using np.mean()
for i in range(len(Input)):
Output.append(np.average(Input[i]))
# Printing output
print(Output)
|
#Output :
|
Find Mean of a List of Numpy Array
# Python code to find mean of
# every numpy array in list
# Importing module
import numpy as np
# List Initialization
Input = [np.array([11, 12, 13]), np.array([14, 15, 16]), np.array([17, 18, 19])]
# Output list initialization
Output = []
# using np.mean()
for i in range(len(Input)):
Output.append(np.average(Input[i]))
# Printing output
print(Output)
#Output :
[END]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.