Creating an ndarray from ragged nested sequences | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Creating an ndarray from ragged nested sequences

Hi, I am trying to write a code for solving a nonlinear system in python. the link to my code is: https://code.sololearn.com/c1vRyV46gUKx but when I run it, python exhibits these errors: - Traceback (most recent call last) - Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' - Result from function call is not a proper array of floats. I can't understand what are the problems...

21st May 2022, 6:45 AM
Fatemeh Hoseini
1 Answer
0
May I ask why you need these to be NumPy arrays? What are you wanting to do with them? The benefits afforded to NumPy arrays come from the fact that their elements are of at least nominally predictible type (though an “object” array approaches genericness) and size. Having removed this promise means their performance will likely be about the same level as normal Python lists. NumPy’s support for ragged arrays, therefore, is very limited. Some extension packages do exist to enhance this with more support (google if needed), but I doubt that it’s the best approach to the issue. It may then be better to store these series of data in a list - if the data is constant, tuples - and transform them into ad hoc arrays at time of use in whatever package you need them for (or not, if not formally required). Alternatively, you can use a mask and/or (though this is probably poor “form”) make all arrays equal length and fill “extra” space with NumPy’s NaN constant. I might be able to help more with more info.
6th Jun 2022, 11:27 PM
Jeremy Miller