numpy.polyfit显示TypeError:+不支持的操作数类型+:'numpy.ndarray'和'float'

时间:2021-11-21 21:22:44

So I'm trying to fit a curve from some data from a .csv file that has two variable (columns) called 'angle' and 'velocity' (see code). I tried using numpy.polyfit on the data arrays:

所以我试图拟合来自.csv文件的一些数据的曲线,该文件有两个变量(列),称为'angle'和'velocity'(参见代码)。我尝试在数据数组上使用numpy.polyfit:

But the code gives me this error

但代码给了我这个错误

TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'float'

TypeError:+:'numpy.ndarray'和'float'不支持的操作数类型

Both the data sets ('angle' and 'velocity') are arrays.

数据集('angle'和'velocity')都是数组。

Here's the code:

这是代码:

import csv as csv
import pylab as plt
import numpy as np

readdata = csv.reader(open("stuff.csv"))

data = []

for row in readdata:
    data.append(row)     #add(append) row stuff in a variable called 'data'

header = data[0]
data.pop(0)
angle = []
velocity = []
for i in range(len(data)):
    angle.append(data[i][0])
    velocity.append(data[i][1])

#print (angle, velocity)

"""
result = lowess(angle, velocity)
print (result)
plt.plot(angle,result, '+')
plt.show()
"""

z = np.polyfit(angle, velocity, 3)    # The problem is right here 
f = np.poly1d(z)

angle_new = np.linspace(angle[0], angle[-1], 50)
velocity_new = f(angle_new)

plt.plot(angle, velocity, angle_new, velocity_new)
plt.xlim(angle[0]-1, angle[-1]+1)
plt.show()

1 个解决方案

#1


Notice that when the NumPy array has a string dtype, addition with a float raises a TypeError:

请注意,当NumPy数组具有字符串dtype时,使用float添加会引发TypeError:

In [12]: np.asarray([3], dtype='|S1') + 0.0
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'float'

When you read the data from the CSV file, you must convert the strings to numerical values, otherwise you will be loading strings into the NumPy array.

从CSV文件中读取数据时,必须将字符串转换为数值,否则将将字符串加载到NumPy数组中。

You could fix the problem by using float here:

你可以在这里使用float修复问题:

for i in range(len(data)):
    angle.append(float(data[i][0]))
    velocity.append(float(data[i][1]))

A more efficient solution would be to use np.loadtxt or np.genfromtxt to load the csv data directly into a NumPy array instead of using csv.reader. The details of how to do that depends on the format of the csv file.

更有效的解决方案是使用np.loadtxt或np.genfromtxt将csv数据直接加载到NumPy数组中,而不是使用csv.reader。有关如何执行此操作的详细信息取决于csv文件的格式。

#1


Notice that when the NumPy array has a string dtype, addition with a float raises a TypeError:

请注意,当NumPy数组具有字符串dtype时,使用float添加会引发TypeError:

In [12]: np.asarray([3], dtype='|S1') + 0.0
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'float'

When you read the data from the CSV file, you must convert the strings to numerical values, otherwise you will be loading strings into the NumPy array.

从CSV文件中读取数据时,必须将字符串转换为数值,否则将将字符串加载到NumPy数组中。

You could fix the problem by using float here:

你可以在这里使用float修复问题:

for i in range(len(data)):
    angle.append(float(data[i][0]))
    velocity.append(float(data[i][1]))

A more efficient solution would be to use np.loadtxt or np.genfromtxt to load the csv data directly into a NumPy array instead of using csv.reader. The details of how to do that depends on the format of the csv file.

更有效的解决方案是使用np.loadtxt或np.genfromtxt将csv数据直接加载到NumPy数组中,而不是使用csv.reader。有关如何执行此操作的详细信息取决于csv文件的格式。