我對 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\...\csv\Test1K.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)我想要的第一個資料集是第三個攝氏度。上面的代碼是我到目前為止的進展。現在我得到錯誤代碼
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/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)
~\AppData\Roaming\Python\Python38\site-packages\scipy\interpolate\interpolate.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
~\AppData\Roaming\Python\Python38\site-packages\scipy\interpolate\polyint.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):
~\AppData\Roaming\Python\Python38\site-packages\scipy\interpolate\polyint.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)
~\AppData\Local\Temp/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 而不是我的輸入之一。如果有人可以幫助我撰寫代碼,甚至有完全不同的其他解決方案,我很感激。謝謝大家的幫助
編輯:整個錯誤資訊
uj5u.com熱心網友回復:
我認為你的問題在這里:
x = dataset.iloc[0:1420, 0:1].values
y = dataset.iloc[0:1420, 3:4].values
0:1切片的結果將是 a DataFrame,您將最終得到一個形狀為 (1420, 1) 的二維陣列。你需要一個一維陣列 for interp1d,所以你應該這樣做
x = dataset.iloc[0:1420, 0].values
y = dataset.iloc[0:1420, 3].values
的形狀x和y將是(1420,)(1D陣列)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/370342.html
上一篇:我試圖創建一個3-D的numpy矩陣,但得到錯誤:TypeError:Fieldelementsmustbe2-or3-tuples,got'5'
