How to Solve Error Message VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences in Python NumPy Array

Posted on

Introduction

As in the article exist, there is some discussion about how to solve some error message appear. Basically, the error message appear upon the declaration of a variable. Specifically, declaring or defining a variable which has the data type of NumPy array. It happens by the help of the NumPy library. After importing the NumPy library, just execute the statement for creating a NumPy array. But unfortunately, the process for creating a NumPy array ends in a failure. That failure exist as part of the title exist in this article. Actually, the error appear upon the process for defining NumPy array as it exist in the previous article. If the process for defining or declaring NumPy array is a success for checking th shape, it will be as it exist in the previous article. That article is an article with the title of ‘How to Show Shape of a NumPy Array’ which exist in this link.

Apparently, the process in one of them trigger an error. The following is the actual appearance of the error message :

Microsoft Windows [Version 10.0.22000.856]
(c) Microsoft Corporation. All rights reserved.
C:\Users\Personal>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> numpy_array_variable_three_dimension = np.array([[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16],[17,18,19,20]]])
<stdin>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.

How to Solve VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences

So, the actual solution for the above error message is actually exist in the previous article. The reason of the error message is because the shapes of the array is different. The first array has two elements which is ‘[1,2,3,4],[5,6,7,8]’. The second array has three elements ‘[9,10,11,12],[13,14,15,16],[17,18,19,20]’. In that case, the solution is to define the array into the same shape. Just change the shapes of the elements by removing one of the elements or adding the other elements. Below is a solution where the step is to remove the element. By removing the element, it will have the same number of elements as follows :

Microsoft Windows [Version 10.0.22000.856]
(c) Microsoft Corporation. All rights reserved.
C:\Users\Personal>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> numpy_array_variable_three_dimension = np.array([[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]]])
>>> print(numpy_array_variable_three_dimension.shape);
(2, 2, 4)
>>>

The above is actually a solution which is part from the previous articles. It is by removing ‘,[17,18,19,20]’ so that it will the same number of element.

Leave a Reply