Python 插值类对象
我有一个简单的Python 插值类对象,python,scipy,interpolation,Python,Scipy,Interpolation,我有一个简单的点类,如下所示 class Point(object): def __init__(self, x=0.0, y=0.0, z=0.0): self.x = x self.y = y self.z = z 我想使用插值这些点作为时间的函数,例如 x,y,z = f(t) 但是,当我尝试以下小示例时 import numpy as np from scipy.interpolate import interp1d time
点
类,如下所示
class Point(object):
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
我想使用插值这些点作为时间的函数,例如
x,y,z = f(t)
但是,当我尝试以下小示例时
import numpy as np
from scipy.interpolate import interp1d
times = np.array([0.0, 0.1, 0.2])
points = np.array([Point(0.0, 0.0, 0.0),
Point(1.0, 1.0, 1.0),
Point(2.0, 2.0, 2.0)])
function = interp1d(times, points)
new_point = function(0.05)
我得到以下错误
Traceback (most recent call last):
File "D:/example.py", line 31, in <module>
function = interp1d(times, points)
File "C:\long_path\scipy\interpolate\interpolate.py", line 439, in __init__
y = y.astype(np.float_)
TypeError: float() argument must be a string or a number, not 'Point'
回溯(最近一次呼叫最后一次):
文件“D:/example.py”,第31行,在
函数=interp1d(次数、点数)
文件“C:\long\u path\scipy\interpolate\interpolate.py”,第439行,在\uuu init中__
y=y.aType(np.float_uux)
TypeError:float()参数必须是字符串或数字,而不是“点”
我还尝试过重载点
类的算术运算符(例如\uuuuuuuuuuuu添加
,\uuuuuuuu子
,\uuuuu truediv\uuuuuuuuu
),尽管这似乎没有帮助
有什么方法可以在我的类中使用
scipy.interpolate.interp1d
吗?因为python对象是内部dict而不是连续缓冲区,所以当自定义类型的对象位于numpy.ndarray
中时,numpy/scipy将无法使用某些方法
一个简单的解决方案是将所有点
放在一个内置类型的ndarray
中:
from __future__ import print_function
import numpy as np
import scipy.interpolate as sp_interp
points = np.array([[0.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
[2.0, 2.0, 2.0]], dtype='float64')
times = np.linspace(0.,.2, len(points))
fn_interp = sp_interp.interp1d(times, points, axis=0)
print(fn_interp(0.05))
如果您致力于基于类的方法,您可能希望定义自定义
dtype
或创建ndarray
的子类,正如回答的那样因为python对象是内部dict而不是连续缓冲区,所以当自定义类型的对象位于numpy.ndarray
内时,numpy/scipy将无法使用某些方法
一个简单的解决方案是将所有点
放在一个内置类型的ndarray
中:
from __future__ import print_function
import numpy as np
import scipy.interpolate as sp_interp
points = np.array([[0.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
[2.0, 2.0, 2.0]], dtype='float64')
times = np.linspace(0.,.2, len(points))
fn_interp = sp_interp.interp1d(times, points, axis=0)
print(fn_interp(0.05))
如果您致力于基于类的方法,您可能希望定义自定义的
dtype
或创建ndarray
的子类,正如回答的那样为什么不将所有点放在Nx3
float64数组中?为什么不将所有点放在Nx3
float64数组中?