Python Forum
Why am I getting a type error? - 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: Why am I getting a type error? (/thread-1670.html)



Why am I getting a type error? - WagmoreBarkless - Jan-19-2017

This should be simple.

I am using NumPy's ndarray, and taking the shape() of the 1st element like this:X.shape[0]. As expected, this is an integer that evaluates to 100.

When I try to add that to integer literal, I get a type error.
type(X.shape[0])
Out[87]: 
int
type(1+X.shape[0])
Out[88]: 
int
type(X)
Out[89]: 
numpy.ndarray
Error:
Traceback (most recent call last):   File "C:\Users\WB\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code     exec(code_obj, self.user_global_ns, self.user_ns)   File "<ipython-input-86-b745a6cdd684>", line 1, in <module>     ppn.fit(X,y)   File "C:\Users\wb\PycharmProjects\untitled8\Perceptron.py", line 9, in fit     zz=1 + X.shape[0] TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
As you can see from the trace, I should be adding two integers. Why does it think one of them is a tuple?


RE: Why am I getting a type error? - Mekire - Jan-19-2017

Can you provide the code that causes this error; a small snippet that reproduces this error when we run it.

As you said, and as you have shown it, your are right; this shouldn't be an error.

For instance:
>>> a = np.ndarray((4,5,6), dtype=int)
>>> a
array([[[  0,   1,   2,   3,   4,   5],
        [  6,   7,   8,   9,  10,  11],
        [ 12,  13,  14,  15,  16,  17],
        [ 18,  19,  20,  21,  22,  23],
        [ 24,  25,  26,  27,  28,  29]],

       [[ 30,  31,  32,  33,  34,  35],
        [ 36,  37,  38,  39,  40,  41],
        [ 42,  43,  44,  45,  46,  47],
        [ 48,  49,  50,  51,  52,  53],
        [ 54,  55,  56,  57,  58,  59]],

       [[ 60,  61,  62,  63,  64,  65],
        [ 66,  67,  68,  69,  70,  71],
        [ 72,  73,  74,  75,  76,  77],
        [ 78,  79,  80,  81,  82,  83],
        [ 84,  85,  86,  87,  88,  89]],

       [[ 90,  91,  92,  93,  94,  95],
        [ 96,  97,  98,  99, 100, 101],
        [102, 103, 104, 105, 106, 107],
        [108, 109, 110, 111, 112, 113],
        [114, 115, 116, 117, 118, 119]]])
>>> a.shape
(4, 5, 6)
>>> a.shape[0]
4
>>> 5 + a.shape[0]
9
>>>



RE: Why am I getting a type error? - WagmoreBarkless - Jan-19-2017

I knew it! Python is broken!

But you fixed it…

import  pandas as pd
import  numpy as np 
df=pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header = None)
df.tail()

X=df.iloc[0:100, [0,2]].values
type(X)
zz=1 + (X.shape[1])
print(zz)
This works now.
You are a genius. You fixed Python. All Python users are grateful.

Thank you


RE: Why am I getting a type error? - Mekire - Jan-19-2017

(Jan-19-2017, 02:49 AM)WagmoreBarkless Wrote: This works now.
You are a genius. You fixed Python. All Python users are grateful.
Um... okay.
Honestly can't tell if this is misplaced sarcastic vitriol or not.
Does your code actually work now?

If so a little detail on what the issue was would help others who stumble across this.


RE: Why am I getting a type error? - WagmoreBarkless - Jan-19-2017

(Jan-19-2017, 02:56 AM)Mekire Wrote:
(Jan-19-2017, 02:49 AM)WagmoreBarkless Wrote: This works now.
You are a genius. You fixed Python. All Python users are grateful.
Um... okay.
Honestly can't tell if this is misplaced sarcastic vitriol or not.
Does your code actually work now?

If so a little detail on what the issue was would help others who stumble across this.

It is not vitriol, no. Maybe misplaced sarcastic gratitude. Tongue

That little code fragment worked fine, so I posted my thanks. Turns out the main program is still broken, so I spoke too soon. I still don't know what is going on. Huh


RE: Why am I getting a type error? - Skaperen - Jan-19-2017

(Jan-19-2017, 02:49 AM)WagmoreBarkless Wrote: You fixed Python.
that's why he has that pager.  we call him every time it breaks.


RE: Why am I getting a type error? - WagmoreBarkless - Jan-19-2017

I found the big problem, which of course just raised more questions. I am working with PyCharm, using a class that I coded into a Python file and calling it from a console window that I opened within PyCharm. It seems that editing the code in the file does not cause it to be refreshed within the console window. I called "import Perceptron" and reinitialized the object from within the console window, but it seems to keep calling stale code, even though the debug traces within the console window show new code.

This is puzzling, but it appears to be a problem with my coding environment and not with Python. If you know offhand how to get around this, I would like to know. In the meantime I will flounder forward.

I still don't know why Python confused an integer for a tuple, but that problem has disappeared.

Wags

Thank you.


RE: Why am I getting a type error? - ichabod801 - Jan-19-2017

Python only imports once. Once a module is imported, further imports only point back to the original import. This can be efficient when running complicated programs. However, if you are in the interactive session, and change a module to fix a bug, it prevents you from importing. Use the reload function to solve this problem:

>>> import spam
# change spam.py
>>> reload(spam)