Python Forum
Type Error Confusion - 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 Confusion (/thread-6694.html)



Type Error Confusion - Oliver - Dec-03-2017



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


RE: Type Error Confusion - j.crater - Dec-03-2017

"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/40142166/how-to-fix-typeerror-len-of-unsized-object


RE: Type Error Confusion - Oliver - Dec-03-2017

(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/40142166/how-to-fix-typeerror-len-of-unsized-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))


RE: Type Error Confusion - j.crater - Dec-03-2017

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.


RE: Type Error Confusion - Oliver - Dec-06-2017

(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.