Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Why am I getting a type error?
#1
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?
Quote
#2
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
>>>
Quote
#3
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
Quote
#4
(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.
Quote
#5
(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
Quote
#6
(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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#7
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.
Quote
#8
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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Error Message: TypeError: unhashable type: 'set' twinpiques 4 3,318 May-22-2019, 02:31 PM
Last Post: twinpiques
  Error:unsupported operand type(s) for ** or pow(): 'list' and 'int' mcgrim 3 548 Mar-22-2019, 01:29 PM
Last Post: buran
  Type Error Confusion Oliver 4 3,737 Dec-06-2017, 03:20 PM
Last Post: Oliver
  type error and access violation error pyopengl hsunteik 0 1,181 Nov-04-2017, 04:51 AM
Last Post: hsunteik
  AUCPR of individual features using Random Forest (Error: unhashable Type) melissa 1 1,178 Jul-10-2017, 12:48 PM
Last Post: sparkz_alot

Forum Jump:


Users browsing this thread: 1 Guest(s)