Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
type error array
#1
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
Reply
#2
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)
BrianPA likes this post
Reply
#3
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 303 Mar-26-2024, 02:18 PM
Last Post: snippsat
  GroupBy - Sum = Error [datetime64 type does not support sum operations] BSDevo 4 2,547 Oct-27-2023, 07:22 PM
Last Post: BSDevo
  Type error in Cross_val_score Vicral 0 1,808 Jul-20-2021, 12:18 PM
Last Post: Vicral
  Error binding parameter 0 - probably unsupported type. illmattic 7 10,231 Jul-18-2020, 09:32 PM
Last Post: illmattic
  How to prepare a NumPy array which include float type array elements subhash 0 1,885 Mar-02-2020, 06:46 AM
Last Post: subhash
  TensorFlow get error - array with more than one element is ambiguous vokoyo 3 5,503 Nov-07-2019, 01:12 PM
Last Post: ThomasL
  Error Message: TypeError: unhashable type: 'set' twinpiques 4 18,759 May-22-2019, 02:31 PM
Last Post: twinpiques
  Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' mcgrim 3 18,186 Mar-22-2019, 01:29 PM
Last Post: buran
  python odeint keeps giving me size of array error kiyoshi7 1 6,053 Nov-01-2018, 02:03 AM
Last Post: j.crater
  Type Error Confusion Oliver 4 14,382 Dec-06-2017, 03:20 PM
Last Post: Oliver

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020