Python Forum
type error array - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: type error array (/thread-32038.html)



type error array - BrianPA - Jan-17-2021

I have a numpy 2darray called d5. I'm trying to get values from row#1 [0]. and I'm comparing them with values from row#2 [1]. If values from row#2 are not equal, then advance to row#3 and iterate until you find a match. Here is the code that is throwing an error for me, along with the error message it self. Bare with me cause i'm really a newbie, and there may be ALOT wrong with what I'm doing!
    lay_1 = 1
    a = 0
    b = lay_1
    del5_counter = 0
    draw_del5 = np.array(d5[a][1] - d5[a][0], d5[a][2] - d5[a][1], d5[a][3] - d5[a][2], d5[a][4] - d5[a][3])
    for row in d5:
        del5_1 = np.array(d5[b][1] - d5[b][0], d5[b][2] - d5[b][1], d5[b][3] - d5[b][2], d5[b][4] - d5[b][3])
        if del5_1 != draw_del5:
            b += 1
            del5_counter += 1
        else:
            break


Traceback (most recent call last):
  File "C:\Users\Brian\MyPythonScripts\working.py", line 177, in <module>
    draw_del5 = np.array(d5[a][1] - d5[a][0], d5[a][2] - d5[a][1], d5[a][3] - d5[a][2], d5[a][4] - d5[a][3])
TypeError: array() takes from 1 to 2 positional arguments but 4 were given



RE: type error array - deanhystad - Jan-17-2021

Quote:numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0)

Parameters
object : array_like
An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.
You can do np.array([1, 2, 3, 4, 5]) but you cannot do np.array(1, 2, 3, 4, 5)

You can use a list comprehension to replace all those error prone subtractions.
import numpy as np

d5 = np.array([list(range(1, 11)) for _ in range(3)])
print(d5)

a = 0
draw_del5 = np.array([x - y for x, y in zip(d5[a][1:5], d5[a])])
print(draw_del5)



RE: type error array - BrianPA - Jan-17-2021

(Jan-17-2021, 03:00 AM)deanhystad Wrote:
Quote:numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0)

Parameters
object : array_like
An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence.
You can do np.array([1, 2, 3, 4, 5]) but you cannot do np.array(1, 2, 3, 4, 5)

You can use a list comprehension to replace all those error prone subtractions.
Thanks deanhystad!
I do indeed have a lot more to learn. The list comprehension you wrote is something I definitely have to get more knowledge about.
The array initially was created like np.array([1, 2, 3, 4, 5]). I got quite a few errors when it was coded like that. Tried many different things to correct the errors, and never changed it back. I guess that is where the list comprehension would solve that. You have given me an example on how to improve upon what I was doing wrong, and also what to investigate on learning what to do in the future. For that, I thank you!