Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 插值类对象_Python_Scipy_Interpolation - Fatal编程技术网

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数组中?