仅使用numpy与Interp1d插值时间序列

发布于 2025-02-05 16:50:24 字数 1791 浏览 2 评论 0原文

我想使用标记为确切点和插值来绘制使用Numpy和Matplotlib的时间序列。基本上这(数据是虚拟的,但是功能是相同的,请注意,时间点之间的距离可能会有所不同):

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

T = [
    np.datetime64('2020-01-01T00:00:00.000000000'),
    np.datetime64('2020-01-02T00:00:00.000000000'),
    np.datetime64('2020-01-03T00:00:00.000000000'),
    np.datetime64('2020-01-05T00:00:00.000000000'),
    np.datetime64('2020-01-06T00:00:00.000000000'),
    np.datetime64('2020-01-09T00:00:00.000000000'),
    np.datetime64('2020-01-13T00:00:00.000000000'),
]
Z = [543, 234, 435, 765, 564, 235, 345]

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot()
ax.plot(T, Z, 'o-')

“在此处输入图像说明”

但是,这里完成的插值只是连接了点。我想使用Scipy的Interp1d包括样条插值和其他类型。因此,我尝试用以下内容替换最后一行:

ax.plot(T,Z, 'o')
ax.plot(T,interp1d(T, Z)(T), '-')

并且会收到以下错误:

UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('float64') and dtype('<m8[ns]')

阅读此答案,我读到,在插值期间,我应该划分t by np.timedeltelta64(1,'s'),像这样:

ax.plot(T,Z, 'o')
ax.plot(T,interp1d(T/np.timedelta64(1, 's'))(T), '-')

但是,我遇到了一个更奇怪的错误:

ufunc 'true_divide' cannot use operands with types dtype('<M8[ns]') and dtype('<m8[s]')

我该怎么办?

I want to plot a time series with numpy and matplotlib, using markers for the exact points, and interpolation. Basically this (data is dummy, but functionality is the same, note that distance between time-points may vary):

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

T = [
    np.datetime64('2020-01-01T00:00:00.000000000'),
    np.datetime64('2020-01-02T00:00:00.000000000'),
    np.datetime64('2020-01-03T00:00:00.000000000'),
    np.datetime64('2020-01-05T00:00:00.000000000'),
    np.datetime64('2020-01-06T00:00:00.000000000'),
    np.datetime64('2020-01-09T00:00:00.000000000'),
    np.datetime64('2020-01-13T00:00:00.000000000'),
]
Z = [543, 234, 435, 765, 564, 235, 345]

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot()
ax.plot(T, Z, 'o-')

enter image description here

However, the interpolation done here is just connecting the points. I want to include spline interpolation and other kinds using scipy's interp1d. So, I tried replacing the last line with the following:

ax.plot(T,Z, 'o')
ax.plot(T,interp1d(T, Z)(T), '-')

and I get the following error:

UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('float64') and dtype('<m8[ns]')

Reading this answer, I read that during interpolation I should divide T by np.timedelta64(1, 's'), like this:

ax.plot(T,Z, 'o')
ax.plot(T,interp1d(T/np.timedelta64(1, 's'))(T), '-')

however, I get an even weirder error:

ufunc 'true_divide' cannot use operands with types dtype('<M8[ns]') and dtype('<m8[s]')

What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2025-02-12 16:50:25

t中的任何元素的数据类型是np.datetime64而不是np.timedelta64

因此,通过使用datatype

T = np.array(
    np.datetime64('2020-01-01T00:00:00.000000000'),
    np.datetime64('2020-01-02T00:00:00.000000000'),
    np.datetime64('2020-01-03T00:00:00.000000000'),
    np.datetime64('2020-01-05T00:00:00.000000000'),
    np.datetime64('2020-01-06T00:00:00.000000000'),
    np.datetime64('2020-01-09T00:00:00.000000000'),
    np.datetime64('2020-01-13T00:00:00.000000000'),
    dtype='m')

​/generated/scipy.interpaly.interp1d.html“ rel =“ nofollow noreferrer”>文档建议,我们必须通过xy是可转换的要像scipy.interpaly.interp1d浮动值浮动值以获取插值功能。我们将使用此答案做到这一点:

# Get an interpolation function f
f = scipy.interpolation.interp1d(x=T/np.timedelta64(1, 's'), y=Z)

最后,我们可以使用以下方式使用以下方式来绘制 :

ax.plot(T, f(T/np.timedelta64(1, 's'), '-')

结合所有内容,我们将获得以下输出:

”在此处输入图像描述

可以重现图像的代码:

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

T = np.array([
    np.datetime64('2020-01-01T00:00:00.000000000'),
    np.datetime64('2020-01-02T00:00:00.000000000'),
    np.datetime64('2020-01-03T00:00:00.000000000'),
    np.datetime64('2020-01-05T00:00:00.000000000'),
    np.datetime64('2020-01-06T00:00:00.000000000'),
    np.datetime64('2020-01-09T00:00:00.000000000'),
    np.datetime64('2020-01-13T00:00:00.000000000'),
], dtype='m')

Z = [543, 234, 435, 765, 564, 235, 345]

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot()
ax.plot(T, Z, 'o')

f = interp1d(x=T/np.timedelta64(1, 's'), y=Z)

ax.plot(T, f(T/np.timedelta64(1, 's')), '-')
plt.show()

The data type of any element in T is np.datetime64 and not np.timedelta64.

Thus, convert the dtype of all elements of T to np.timedelta64 by creating a numpy array with datatype m:

T = np.array(
    np.datetime64('2020-01-01T00:00:00.000000000'),
    np.datetime64('2020-01-02T00:00:00.000000000'),
    np.datetime64('2020-01-03T00:00:00.000000000'),
    np.datetime64('2020-01-05T00:00:00.000000000'),
    np.datetime64('2020-01-06T00:00:00.000000000'),
    np.datetime64('2020-01-09T00:00:00.000000000'),
    np.datetime64('2020-01-13T00:00:00.000000000'),
    dtype='m')

Then, as the documentation suggests, we have to pass x and y that are convertible to float like values to scipy.interpolate.interp1d to get a interpolation function. We'll use a method suggested in this answer to do that:

# Get an interpolation function f
f = scipy.interpolation.interp1d(x=T/np.timedelta64(1, 's'), y=Z)

Finally, we can use the interpolated function as follows for plotting:

ax.plot(T, f(T/np.timedelta64(1, 's'), '-')

Combining everything, we get the following output:

enter image description here

The code that can reproduce the image:

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

T = np.array([
    np.datetime64('2020-01-01T00:00:00.000000000'),
    np.datetime64('2020-01-02T00:00:00.000000000'),
    np.datetime64('2020-01-03T00:00:00.000000000'),
    np.datetime64('2020-01-05T00:00:00.000000000'),
    np.datetime64('2020-01-06T00:00:00.000000000'),
    np.datetime64('2020-01-09T00:00:00.000000000'),
    np.datetime64('2020-01-13T00:00:00.000000000'),
], dtype='m')

Z = [543, 234, 435, 765, 564, 235, 345]

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot()
ax.plot(T, Z, 'o')

f = interp1d(x=T/np.timedelta64(1, 's'), y=Z)

ax.plot(T, f(T/np.timedelta64(1, 's')), '-')
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文