The problem is you are performing scalar addition with an empty list
.
The reason you have an empty list
is because you try to perform scalar multiplication with a python list rather than with a numpy.array
. The scalar is converted to an integer, 0
, and creates a zero length list
.
We'll explore this below, but to fix it you just need your data in numpy arrays instead of in lists. Either create it originally, or convert the lists to arrays:
>>> x = numpy.array([24.0, 13.0, 12.0, 22.0, 21.0, 10.0, 9.0, 12.0, 7.0, 14.0,
... 18.0, 1.0, 18.0, 15.0, 13.0, 13.0, 12.0, 19.0, 13.0]
An explanation of what was going on follows:
Let's unpack the expression yfit = p1[0] * x + p1[1]
.
The component parts are:
>>> p1[0]
-0.58791208791208893
p1[0]
isn't a float however, it's a numpy
data type:
>>> type(p1[0])
<class 'numpy.float64'>
x
is as given above.
>>> p1[1]
20.230769230769241
Similar to p1[0]
, the type of p1[1]
is also numpy.float64
:
>>> type(p1[0])
<class 'numpy.float64'>
Multiplying a list by a non-integer interpolates the number to be an integer, so p1[0]
which is -0.58791208791208893
becomes 0
:
>>> p1[0] * x
[]
as
>>> 0 * [1, 2, 3]
[]
Finally you are adding the empty list to p[1]
, which is a numpy.float64
.
This doesn't try to append the value to the empty list. It performs scalar addition, i.e. it adds 20.230769230769241
to each entry in the list.
However, since the list is empty there is no effect, other than it returns an empty numpy
array
with the type numpy.float64
:
>>> [] + p1[1]
array([], dtype=float64)
An example of a scalar addition having an effect:
>>> [10, 20, 30] + p1[1]
array([ 30.23076923, 40.23076923, 50.23076923])