from scipy.interpolate import interp1d
import numpy as np
# 定義已知的 X 和 Y 值
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
# 建立一個插值函數
f = interp1d(x, y)
# 定義需要插值的 X 值
x_new = np.array([1.5, 2.5])
# 對 X 值進行插值
y_new = f(x_new)
print(“插值結果:”, y_new)

用polyfit作外插
import numpy as np
# 定义已知的 X 和 Y 值
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
# 使用 polyfit 函数拟合多项式
coefs = np.polyfit(x, y, 2)
# 使用 numpy.poly1d 函数创建多项式对象
p = np.poly1d(coefs)
# 定义需要外插的 X 值
x_new = np.array([0, 4])
# 使用多项式对象的差值函数对 X 值进行外插
y_new = p(x_new)
print(“外插结果:”, y_new)

推薦hahow線上學習python: https://igrape.net/30afN








![Python: pandas.DataFrame()處理雙維度資料,dict跟2D list轉為DataFrame有何差別?如何用index及columns屬性客製化index跟欄位名稱?df.index = [“一”,”二”,”三”,”四”] ; df.columns = 使用.head(n) ; .tail(m) ;取首n列,尾m列; .at[index,欄位名稱] 取單一資料 ; .iat[index,欄位順序] 取單一資料 ; .loc[index,欄位名稱] 取資料 ; .iloc[index,欄位順序];df.iloc[ [0,1],[0,2]])取資料 ; df.iloc[ 0:3,0:2]切片 Python: pandas.DataFrame()處理雙維度資料,dict跟2D list轉為DataFrame有何差別?如何用index及columns屬性客製化index跟欄位名稱?df.index = [“一”,”二”,”三”,”四”] ; df.columns = 使用.head(n) ; .tail(m) ;取首n列,尾m列; .at[index,欄位名稱] 取單一資料 ; .iat[index,欄位順序] 取單一資料 ; .loc[index,欄位名稱] 取資料 ; .iloc[index,欄位順序];df.iloc[ [0,1],[0,2]])取資料 ; df.iloc[ 0:3,0:2]切片](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2022/11/20221111093547_79.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

近期留言