Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Type Error Confusion
#1


X0 = [1,2,3]
X1 = [4,5,6]
k = len(np.cov(X0))

Throws this error:

k = len(np.cov(X0))
TypeError: len() of unsized object


---
I tried using an numpy array, but got the same error.

How to get around this problem?

Thanks in advance,

- O
Reply
#2
"len() of unsized object" error generally means that the object, of which length you want to know, does not support this method - in your case it is np.cov("some_list"). You will need to convert it or use a different method of obtaining list length.
If you are using numpy array, try the "size" method, as suggested here:
https://stackoverflow.com/questions/4014...zed-object
Reply
#3
(Dec-03-2017, 04:11 PM)j.crater Wrote: "len() of unsized object" error generally means that the object, of which length you want to know, does not support this method - in your case it is np.cov("some_list"). You will need to convert it or use a different method of obtaining list length.
If you are using numpy array, try the "size" method, as suggested here:
https://stackoverflow.com/questions/4014...zed-object

That's helpful, thanks.

Actually, I just found some code online that is supposed to do the "Box's M" statistical test on co-variant matrices. I'm new to Python so maybe I'm missing something, but this code doesn't run with a simple example of two arrays:

import numpy as np

# def box_m(X0, X1):

X0 = np.array([1,2,3])
X1 = np.array([4,5,6])

global Xp

m = 2
k = (np.cov(X0).size) # returns 1, not sure if that's correct

n_1 = len(X0[0]) # ERROR >>> TypeError: object of type 'numpy.int64' has no len()
n_2 = len(X0[0]) # ERROR >>> TypeError: object of type 'numpy.int64' has no len()
n = len(X0[0]) + len(X1[0])

Xp = (((n_1 - 1) * np.cov(X0)) + ((n_2 - 1) * np.cov(X1))) / (n - m)

M = ((n - m) * np.log(np.linalg.det(Xp))) \
- (n_1 - 1) * (np.log(np.linalg.det(np.cov(X0)))) - (n_2 - 1) * (np.log(np.linalg.det(np.cov(X1))))

c = ((2 * (k ** 2) + (3 * k) - 1) / ((6 * (k + 1) * (m - 1)))) \
* ((1 / (n_1 - 1)) + (1 / (n_2 - 1)) - (1 / (n - m)))

df = (k * (k + 1) * (m - 1)) / 2

c2 = (((k - 1) * (k + 2)) / (6 * (m - 1))) \
* ((1 / ((n_1 - 1) ** 2)) + (1 / ((n_2 - 1) ** 2)) - (1 / ((n - m) ** 2)))

df2 = (df + 2) / (np.abs(c2 - c ** 2))

if (c2 > c ** 2):

a_plus = df / (1 - c - (df / df2))

F = M / a_plus

else:

a_minus = df2 / (1 - c + (2 / df2))

F = (df2 * M) / (df * (a_minus - M))

print('M = {}'.format(M))
print('c = {}'.format©)
print('c2 = {}'.format(c2))
print('-------------------')
print('df = {}'.format(df))
print('df2 = {}'.format(df2))
print('-------------------')
print('F = {}'.format(F))
Reply
#4
First, when posting code here, please follow the instructions sparkz_alot posted at the bottom of your message.

This code cannot work, since X0[0] gets first element of X0 array, and error tells you cannot use len() on the element (in this case of type numpy.int64).
Maybe you need length of the whole array instead. But my guessing won't solve the problem. Instead you need to know what data manipulation the program needs to do.
Reply
#5
(Dec-03-2017, 08:16 PM)j.crater Wrote: First, when posting code here, please follow the instructions sparkz_alot posted at the bottom of your message.

This code cannot work, since X0[0] gets first element of X0 array, and error tells you cannot use len() on the element (in this case of type numpy.int64).
Maybe you need length of the whole array instead. But my guessing won't solve the problem. Instead you need to know what data manipulation the program needs to do.

First, sorry about my posting code incorrectly. Will amend in future posts.

This was just some code the purportedly worked to compute Box's M statistic. Rather than try to debug this code (I thought it would kind of work), I'll just write my own function instead

--O.

Thanks for your reply.

(Dec-03-2017, 08:16 PM)j.crater Wrote: First, when posting code here, please follow the instructions sparkz_alot posted at the bottom of your message.

This code cannot work, since X0[0] gets first element of X0 array, and error tells you cannot use len() on the element (in this case of type numpy.int64).
Maybe you need length of the whole array instead. But my guessing won't solve the problem. Instead you need to know what data manipulation the program needs to do.

First, sorry about my posting code incorrectly. Will amend in future posts.

This was just some code the purportedly worked to compute Box's M statistic. Rather than try to debug this code (I thought it would kind of work), I'll just write my own function instead

--O.

Thanks for your reply.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GroupBy - Sum = Error [datetime64 type does not support sum operations] BSDevo 4 2,880 Oct-27-2023, 07:22 PM
Last Post: BSDevo
  Type error in Cross_val_score Vicral 0 1,838 Jul-20-2021, 12:18 PM
Last Post: Vicral
  type error array BrianPA 2 2,417 Jan-17-2021, 01:48 PM
Last Post: BrianPA
  Error binding parameter 0 - probably unsupported type. illmattic 7 10,391 Jul-18-2020, 09:32 PM
Last Post: illmattic
  Error Message: TypeError: unhashable type: 'set' twinpiques 4 18,990 May-22-2019, 02:31 PM
Last Post: twinpiques
  Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' mcgrim 3 18,355 Mar-22-2019, 01:29 PM
Last Post: buran
  type error and access violation error pyopengl hsunteik 0 3,335 Nov-04-2017, 04:51 AM
Last Post: hsunteik
  AUCPR of individual features using Random Forest (Error: unhashable Type) melissa 1 3,316 Jul-10-2017, 12:48 PM
Last Post: sparkz_alot
  Why am I getting a type error? WagmoreBarkless 7 7,560 Jan-19-2017, 10:29 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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