77
f=np.loadtxt('Single Small Angle 1.txt',unpack=True,skiprows=2)
g=np.loadtxt('Single Small Angle 5.txt',unpack=True,skiprows=2)

x = f-g[:,:11944]
t=range(len(x))
m=math.log10(abs(x))

np.polyfit(t,m)

plt.plot(t,abs(x))
plt.show()

I'm just not sure on how to fix my issue. It keeps saying:

m=math.log10(abs(x))
TypeError: only length-1 arrays can be converted to Python scalars
1
  • 18
    When you feed a native python list like [1,2,3] to a numpy method that expects a numpy array, you will get this error. That numpy method takes your native python list, and tries to digest it, and way deep down it pukes up this response. A more user friendly error message would have been: TypeError: Don't feed native python lists into numpy functions that expect numpy arrays. Either convert your python list to a numpy array or package your python lists into a tuple. Python numpy really dropped the ball on that one, that error message is terrible. Nov 8, 2016 at 21:28

2 Answers 2

100

Non-numpy functions like math.abs() or math.log10() don't play nicely with numpy arrays. Just replace the line raising an error with:

m = np.log10(np.abs(x))

Apart from that the np.polyfit() call will not work because it is missing a parameter (and you are not assigning the result for further use anyway).

2
  • 1
    Great answer. In addition to that, I think none of the math functions work with numpy arrays. Better change them with numpy functions.
    – endertunc
    Mar 22, 2016 at 17:52
  • Thanks a lot! I was facing a similar problem with math.power in combination with other numpy functions. Replacing math.power with numpy.power solved the problem. Mar 4, 2018 at 23:04
65

Here is another way to reproduce this error in Python2.7 with numpy:

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.concatenate(a,b)   #note the lack of tuple format for a and b
print(c) 

The np.concatenate method produces an error:

TypeError: only length-1 arrays can be converted to Python scalars

If you read the documentation around numpy.concatenate, then you see it expects a tuple of numpy array objects. So surrounding the variables with parens fixed it:

import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.concatenate((a,b))  #surround a and b with parens, packaging them as a tuple
print(c) 

Then it prints:

[1 2 3 4 5 6]

What's going on here?

That error is a case of bubble-up implementation - it is caused by duck-typing philosophy of python. This is a cryptic low-level error python guts puke up when it receives some unexpected variable types, tries to run off and do something, gets part way through, the pukes, attempts remedial action, fails, then tells you that "you can't reformulate the subspace responders when the wind blows from the east on Tuesday".

In more sensible languages like C++ or Java, it would have told you: "you can't use a TypeA where TypeB was expected". But Python does it's best to soldier on, does something undefined, fails, and then hands you back an unhelpful error. The fact we have to be discussing this is one of the reasons I don't like Python, or its duck-typing philosophy.

5
  • 5
    still occurs in python 3 and killed me mysteriously.
    – Christophe
    Oct 11, 2016 at 17:53
  • Try using the pdb debugger to give you more information about the error: docs.python.org/3/library/pdb.html Oct 11, 2016 at 17:57
  • 1
    oh sorry my post was unclear. I meant that I had the same problem with concatenate and your answer solved it for me. You mentioned it was an error (feature?) with Python2.7 and numpy, it still exists with python3 and numpy :)
    – Christophe
    Oct 12, 2016 at 1:02
  • 6
    "You can't refromulate the subspace responders when the wind blows from the east on tuesday" I like you
    – rosstex
    Jan 13, 2018 at 15:26
  • 1
    Look bro, I'm not going to pay you 70 smidgens for a broken defrakulator. youtube.com/watch?v=Xk5A4QWyl1U Jan 13, 2018 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.