python 笔记:将不同长度2D矩阵线性插值至相同长度(scipy.interpolate)

1 问题描述

我现在有三个2D矩阵,每一行是两个元素,代表经纬度;不同矩阵的行数不同

现在希望通过线性插补,使得每个2D矩阵行数相同

python 复制代码
pth_lst=[[[1,2],
          [1,3],
          [3,4]],
         [[3,4],
          [5,6],
          [7,8],
          [9,10],
          [11,12],
          [13,14],
          [15,16]],
         [[2,4],
          [5,8]]]     

2 解决方法

2.1 导入库

python 复制代码
import numpy as np
from scipy.interpolate import interp1d

2.2 计算各个2D矩阵需要插补到的行数

python 复制代码
max_len=0
for i in pth_lst:
    max_len=max(max_len,len(i))
max_len
#7

2.3 进行插补

python 复制代码
inter_lst=[]
#结果列表

for i in pth_lst:
    tmp=np.array(i)
    current_l=tmp.shape[0]

    new_index=np.linspace(0,current_l-1,max_len)
    #目标自变量坐标(首末位置和原来的2D矩阵一样,间隔不再是1)

    interpolate_path=interp1d(np.arange(current_l),i,axis=0)
    #对当前2D矩阵进行线性插补

    inter_lst.append(interpolate_path(new_index).tolist())
inter_lst
'''
[[[1.0, 2.0],
  [1.0, 2.3333333333333335],
  [1.0, 2.6666666666666665],
  [1.0, 3.0],
  [1.6666666666666665, 3.333333333333333],
  [2.333333333333333, 3.6666666666666665],
  [3.0, 4.0]],

 [[3.0, 4.0],
  [5.0, 6.0],
  [7.0, 8.0],
  [9.0, 10.0],
  [11.0, 12.0],
  [13.0, 14.0],
  [15.0, 16.0]],

 [[2.0, 4.0],
  [2.5, 4.666666666666667],
  [3.0, 5.333333333333333],
  [3.5, 6.0],
  [4.0, 6.666666666666666],
  [4.5, 7.333333333333333],
  [5.0, 8.0]]]
'''
相关推荐
qingyulee4 分钟前
python redis
开发语言·redis·python
是上好佳佳佳呀8 分钟前
【前端(十三)】JavaScript 数组与字符串笔记
前端·javascript·笔记
互联网时光机1 小时前
我用 UniApp + 腾讯云 IAI 做了一个“明星脸比对“小程序,零后台延迟
经验分享·python·人脸识别
l1t1 小时前
DeepSeek总结的Python 3.14.5 发布候选版本
开发语言·python
Cyber4K1 小时前
【Python专项】进阶语法-日志分类与分析(2)
开发语言·前端·python
風清掦1 小时前
【STM32学习笔记-12】Unix 时间戳、BKP 备份寄存器与 RTC 实时时钟
笔记·stm32·单片机·嵌入式硬件·学习·实时音视频·unix
lbb 小魔仙1 小时前
Python + LangChain 环境搭建完全指南:从零构建本地 RAG 知识库(附 Ollama 本地模型集成)
开发语言·python·langchain
风落无尘1 小时前
Python 包发布全流程:从项目结构到 PyPI 上线,以及我踩过的那些坑
开发语言·python·pip
Lenyiin1 小时前
《LeetCode 顺序刷题》61 - 70
java·c++·python·算法·leetcode·lenyiin
岁岁的O泡奶1 小时前
NSSCTF_crypto_[LitCTF 2023]babyLCG
经验分享·python·算法·密码学·crypto·流密码