我对python和数据科学完全陌生,所以请耐心等待。我感谢每一次帮助,并尽可能多地理解它。到目前为止,我得到了以下代码。
import pandas as pd
import numpy as np
from pandas import read_csv
from matplotlib import pyplot
import matplotlib.pyplot as plt
from scipy import interpolate
from scipy.interpolate import interp1d
from scipy.interpolate import interp2d
dataset = pd.read_csv(r"C:Users...csvTest1K.csv", sep=';', skiprows=0)
x = dataset.iloc[0:1420, 0:1].values
y = dataset.iloc[0:1420, 3:4].values
f = interpolate.interp1d(x, y, kind = 'cubic')
xn =270
t_on = f(xn)
print(t_on)
csv文件输出的第一行如下所示:
0 [s] [Celsius] [Celsius] [Celsius] [Celsius] [Celsius]
1 0 22.747 22.893 0.334 22.898 22.413
2 60 22.769 22.902 22.957 22.907 -0.187
3 120 22.78 22.895 25.519 22.911 -2.739
4 180 22.794 22.956 33.62 22.918 -10.827
关于我尝试做什么以及问题在哪里的简短说明。我有一个csv文件,里面有很多数据,每60秒读取一次温度读数,大约1400次。现在我想对其进行插值,这样我就可以在每60秒之间获得一个特定的数据点,甚至可能比1400次迭代更远。(可能高达1600(我想要的第一个数据集是第三个celsius数据集。上面的代码是我到目前为止的进展。现在我得到错误代码
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_8008/3128405253.py in <module>
7
8
----> 9 f = interpolate.interp1d(x, y, kind = 'cubic')
10 yn =270 # Wert auf den Interpoliert werden soll
11 t_on = f(yn)
~AppDataRoamingPythonPython38site-packagesscipyinterpolateinterpolate.py in __init__(self, x, y, kind, axis, copy, bounds_error, fill_value, assume_sorted)
434 assume_sorted=False):
435 """ Initialize a 1-D linear interpolation class."""
--> 436 _Interpolator1D.__init__(self, x, y, axis=axis)
437
438 self.bounds_error = bounds_error # used by fill_value setter
~AppDataRoamingPythonPython38site-packagesscipyinterpolatepolyint.py in __init__(self, xi, yi, axis)
52 self.dtype = None
53 if yi is not None:
---> 54 self._set_yi(yi, xi=xi, axis=axis)
55
56 def __call__(self, x):
~AppDataRoamingPythonPython38site-packagesscipyinterpolatepolyint.py in _set_yi(self, yi, xi, axis)
122 shape = (1,)
123 if xi is not None and shape[axis] != len(xi):
--> 124 raise ValueError("x and y arrays must be equal in length along "
125 "interpolation axis.")
126
ValueError: x and y arrays must be equal in length along interpolation axis.
我搜索了解决方案,得到了这个例子:
x = np.linspace(0, 4, 13)
y = np.linspace(0, 4, 13)
X, Y = np.meshgrid(x, y)
z = np.arccos(-np.cos(2*X) * np.cos(2*Y))
f = interpolate.interp2d(x, y, z, kind = 'cubic')
我读到其他问题,2d解决方案应该会有所帮助,但当我这样说时,我得到了:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_8008/1890924245.py in <module>
14 f = interpolate.interp2d(x, y, z, kind = 'cubic')
15 yn =270 # Wert auf den Interpoliert werden soll
---> 16 t_on = f(yn)
17 print(t_on)
18
TypeError: __call__() missing 1 required positional argument: 'y'
现在我明白了,我需要一些东西,但这不会毁了我想做的全部吗?我的结果应该是我的y,而不是我的一个输入。如果有人能帮助我编写代码,甚至有完全不同的其他解决方案,我将不胜感激。感谢大家对的帮助
编辑:整个错误消息
我认为您的问题就在这里:
x = dataset.iloc[0:1420, 0:1].values
y = dataset.iloc[0:1420, 3:4].values
0:1
切片的结果将是DataFrame
,最终将得到形状为(1420,1(的二维阵列。interp1d
需要一个1d数组,所以你应该只做
x = dataset.iloc[0:1420, 0].values
y = dataset.iloc[0:1420, 3].values
x
和y
的形状将是(1420,)
(1d阵列(。