- Don't use
isto compare if two objects are equal, use==instead:
In [110]: [] is [] Out[110]: False In [111]: []==[] Out[111]: True
- Your
gis a 1D numpy array, andg[j]gets you an element, whilstcurr_gmight be an array. To reproduce your error:
In [117]: l=np.arange(3) ...: l[0]=[1,2] #assigning an element of a numpy array to as a python list --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-117-817e72d77bd0> in <module>() 1 l=np.arange(3) ----> 2 l[0]=[1,2] ValueError: setting an array element with a sequence.
np.array([no_classes, no_features, no_classes])gives you a 1D array of length 3. If you want to initializegas a 3D array of shape(no_classes, no_features, no_classes), usenp.zeros, e.g.:
In [124]: np.zeros((2,3,2)) Out[124]: array([[[ 0., 0.], [ 0., 0.], [ 0., 0.]], [[ 0., 0.], [ 0., 0.], [ 0., 0.]]])
- To check if a subarray of
gis unassigned(namely, all are 0s here IIUC), usenp.allclose:
Answer from zhangxaochen on Stack OverflowIn [128]: l=np.zeros((2,3,2)) In [129]: np.allclose(l[0], 0) Out[129]: True
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.4 Manual
Return a new array of given shape and type, without initializing entries · Shape of the empty array, e.g., (2, 3) or 2
Top answer 1 of 2
2
- Don't use
isto compare if two objects are equal, use==instead:
In [110]: [] is [] Out[110]: False In [111]: []==[] Out[111]: True
- Your
gis a 1D numpy array, andg[j]gets you an element, whilstcurr_gmight be an array. To reproduce your error:
In [117]: l=np.arange(3) ...: l[0]=[1,2] #assigning an element of a numpy array to as a python list --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-117-817e72d77bd0> in <module>() 1 l=np.arange(3) ----> 2 l[0]=[1,2] ValueError: setting an array element with a sequence.
np.array([no_classes, no_features, no_classes])gives you a 1D array of length 3. If you want to initializegas a 3D array of shape(no_classes, no_features, no_classes), usenp.zeros, e.g.:
In [124]: np.zeros((2,3,2)) Out[124]: array([[[ 0., 0.], [ 0., 0.], [ 0., 0.]], [[ 0., 0.], [ 0., 0.], [ 0., 0.]]])
- To check if a subarray of
gis unassigned(namely, all are 0s here IIUC), usenp.allclose:
In [128]: l=np.zeros((2,3,2)) In [129]: np.allclose(l[0], 0) Out[129]: True
2 of 2
1
In Python is checks for object identity, so whether to objects are actually the same.
>>>a=[]
>>>a is []
False
>>>
So g[j] is [] is never True.
As suggested here you could check the size attribute.
Videos
NumPy Arrays in Python | 0-D Arrays, Zeros, Empty Arrays ...
Python 2D arrays and lists - YouTube
21:00
How To Create a Two Dimensional Array in Python? - YouTube
06:30
Convert an Array Into a 2D Array With Conditions - Leetcode 2610 ...
03:51
How to create empty NumPy arrays in Python - YouTube
Eileen's Lounge
eileenslounge.com › viewtopic.php
Check if 2D array is empty or not - Eileen's Lounge
I'd use a Boolean variable instead. In the loop, set the variable to True when an element is NOT empty, and immediately exit the loop. If the variable is still False after the loop, all array elements were empty.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.2 Manual
Return a new array of given shape and type, without initializing entries · Shape of the empty array, e.g., (2, 3) or 2
DaniWeb
daniweb.com › programming › software-development › threads › 160559 › empty-2d-array
python - Empty 2D Array [SOLVED] | DaniWeb
So hoe to write a generic code for creating a empty 2D array and dynamically insert values in it. ... When your row lengths vary, think of a 2D structure as a list of independent rows (a ragged list). You can start empty and append each row as its length becomes known, initializing with a placeholder such as None so you can fill values later: row_lengths = [len1, len2, ...] # discovered at runtime grid = [] for row_len in row_lengths: grid.append([None] * row_len) # each row is a distinct list
Sling Academy
slingacademy.com › article › numpy-test-if-an-array-is-empty-or-not-3-examples
NumPy: Test if an array is empty or not (3 examples) - Sling Academy
This method is particularly useful for multi-dimensional arrays. import numpy as np # Creating a 2D empty array empty_2d_array = np.empty((0,2)) # Checking for emptiness if np.size(empty_2d_array) == 0: print('This 2D array is empty.') else: print('This 2D array has elements.')
Python Guides
pythonguides.com › check-if-numpy-array-is-empty
Check If NumPy Array Is Empty In Python
May 16, 2025 - # Using any() with 0 in shape (more reliable) is_empty_better = 0 in empty_array.shape print(f"Is empty (using 0 in shape): {is_empty_better}") # True # For multidimensional arrays empty_2d = np.zeros((0, 5)) is_empty_2d = 0 in empty_2d.shape print(f"Is 2D array empty: {is_empty_2d}") # True · Be careful with the direct use of any() or all() as they can be tricky with one-dimensional arrays, checking for zero in the shape is more reliable. Read NumPy Reset Index of an Array in Python ·
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.1 Manual
Return a new array of given shape and type, without initializing entries · Shape of the empty array, e.g., (2, 3) or 2
TutorialsPoint
tutorialspoint.com › what-is-the-preferred-method-to-check-for-an-empty-array-in-numpy
What is the preferred method to check for an empty array in NumPy?
In this method, we used the inputArray.size attribute to determine whether or not the array is empty. This operator returns the array size, which in this example is 0, resulting in the expected result. This is a numpy array attribute that gives a tuple containing the array's shape. This can be used to see if the array is empty.
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-95.php
NumPy: Check whether the numpy array is empty or not - w3resource
# Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating a NumPy array 'x' containing integers [2, 3] x = np.array([2, 3]) # Creating an empty NumPy array 'y' y = np.array([]) # Printing the size of array 'x' # As 'x' contains 2 elements, its size is 2 print(x.size) # Printing the size of array 'y' # 'y' is an empty array, so its size is 0 print(y.size)
w3resource
w3resource.com › numpy › array-creation › empty.php
NumPy: numpy.empty() function - w3resource
Explanation: This code demonstrates how to use np.empty() function in NumPy to create empty arrays with specified data types. The dtype parameter in the np.empty() function can be used to specify the data type of the empty array.
NumPy
numpy.org › devdocs › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.5.dev0 Manual
Return a new array of given shape and type, without initializing entries · Shape of the empty array, e.g., (2, 3) or 2
Python Pool
pythonpool.com › home › blog › 5 ways to check if the numpy array is empty
5 Ways to Check if the NumPy Array is Empty - Python Pool
January 1, 2024 - As the array is empty, the flag variable’s value becomes True, so the output ‘Array is empty’ is displayed. The limitation to this function is that it does not work if the array contains the value 0 in it. We use the numpy.size() function in Python to count the number of elements along a given axis.
GeeksforGeeks
geeksforgeeks.org › python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
Hence the better way to declare a 2d array is ... This method creates 5 separate list objects, unlike method 2a. One way to check this is by using the 'is' operator which checks if the two operands refer to the same object.
Published June 20, 2024
IncludeHelp
includehelp.com › python › how-to-check-whether-a-numpy-array-is-empty-or-not.aspx
How to check whether a NumPy array is empty or not?
The numpy.any() method checks whether any array element along a given axis evaluates to True. If the given array is not empty it returns True; False, otherwise. numpy.any(a, axis=None, out=None, keepdims=<no value>, *, where=<no value>) Consider the below Python program to check NumPy array ...