0 前言

结果展示:黑色的点是前期输入生成的,彩色是后期生成的,代表不同像素点的数值

1 代码

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d #引入scipy中的一维插值库
from scipy.interpolate import griddata#引入scipy中的二维插值库x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(-x**2/9.0)
f = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')xnew = np.linspace(0, 10, num=41, endpoint=True)plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
plt.legend(['data', 'linear', 'cubic'], loc='best')
plt.show()
'''''
def func(x, y):return x**2+y**2
'''''
grid_x, grid_y = np.mgrid[1:10:200j, 1:10:500j]
#x方向在0-1上均匀生成200个数,y方向在0-1上均匀生成500个数points = np.random.randint(1,10,(200, 2))
#随机生成(200,2)的矩阵,即200个点坐标values = np.arange(0,200)#func(points[:,0], points[:,1])
#规定坐标值的值grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
print(grid_z0[0][1])
#由于griddata返回的值是nddarry的格式的,所以可以读取他的数据,这里就是读取第[0][1]个数值的数grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear',fill_value=5)
print(grid_z1[0][1])grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic',fill_value=5)
print(grid_z1[0][1])
'''
plt.subplot(221)
plt.imshow(func(grid_x, grid_y).T, extent=(1,10,1,10), origin='lower')
plt.plot(points[:,0], points[:,1], 'k.', ms=1)plt.title('Original')
'''
plt.subplot(222)
plt.imshow(grid_z0.T, extent=(1,10,1,10), origin='lower')plt.title('Nearest')
plt.subplot(223)
plt.imshow(grid_z1.T, extent=(1,10,1,10), origin='lower')plt.title('Linear')
plt.subplot(224)
plt.imshow(grid_z2.T, extent=(1,10,1,10), origin='lower')plt.title('Cubic')
plt.gcf().set_size_inches(8, 8)
plt.show()

2 解释

python中:
scipy.interpolate.griddata(points,values,xi,method =‘linear’,fill_value = nan,rescale = False )

参数:
points:数据点坐标。可以是形状(n,D)的数组,也可以是ndim数组的元组。(已知点)

values:浮点或复数的ndarray,形状(n,)的数据值。(已知点对应的值)

xi : 浮点数的二维数组或一维数组的元组,形状(M,D)插值数据的点。(被划分后的网格)

method:‘linear’,‘nearest’,‘cubic’,可选其中的插值方法之一。(插值方式)
{
nearest 返回最接近插值点的数据点的值。
linear 将输入点设置为n维单纯形,并在每个单形上线性插值。
cubic (1-d) 返回由三次样条确定的值。
cubic (2-d) 返回由分段立方,连续可微(C1)和近似曲率最小化多项式表面确定的值。
}

fill_value : float,可选。用于填充输入点凸包外部的请求点的值。如果未提供,则默认为nan。此选项对“最近”方法无效。

rescale : bool,可选。在执行插值之前,重新缩放指向单位立方体。如果某些输入维度具有不可比较的单位并且相差很多个数量级,则这非常有用。

返回值:nddarry 内插值数组

3 优化(未完待续)

import numpy as np
import matplotlib.pyplot as plt
#from scipy.interpolate import interp1d #引入scipy中的一维插值库
from scipy.interpolate import griddata#引入scipy中的二维插值库four_boundary_point_original = np.array([[1,0],[10,2],[0,5],[9,10]])
four_boundary_point_x = four_boundary_point_original.min(axis=0)
four_boundary_point_y = four_boundary_point_original.max(axis=0)
four_boundary_point = np.concatenate([four_boundary_point_x,four_boundary_point_y])
print(four_boundary_point)'''
for i in range(4):input_x = float(input('please input 4 number:'))four_boundary_point.append(input_x)
'''
#four_boundary_point = [0,10,0,10]mesh_size_x = round((four_boundary_point[2] - four_boundary_point[0])/0.05)
mesh_size_y = round((four_boundary_point[3] - four_boundary_point[1])/0.05)
print(mesh_size_y,mesh_size_x)
#将小格子的x方向分为步长为0.05的长度def func(x, y):return x**2+y**2x = np.linspace(four_boundary_point[0],four_boundary_point[2],mesh_size_x)
y = np.linspace(four_boundary_point[1],four_boundary_point[3],mesh_size_y)
grid_x, grid_y = np.meshgrid(x,y)
#x方向在0-1上均匀生成200个数,y方向在0-1上均匀生成mesh_size个数字points = 10 * np.random.random((200,2))
#随机生成(200,2)的矩阵,即200个点坐标values = np.arange(0,200)
#规定坐标值的值grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
#print(grid_z0)
grid_z0[grid_z0<80]=0grid_z0[(grid_z0>=80)&(grid_z0<160)]=50
grid_z0[(grid_z0>=160)&(grid_z0<=200)]=100
#由于griddata返回的值是nddarry的格式的,所以可以读取他的数据,这里就是读取第[0][1]个数值的数grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear',fill_value=5)grid_z1[grid_z1<80] = 1
grid_z1[(grid_z1>=80)&(grid_z1<160)] = 50
grid_z1[(grid_z1>=160)&(grid_z1<=200)] = 100grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic',fill_value=5)grid_z2[grid_z2<80] = 1
grid_z2[(grid_z2>=80)&(grid_z2<160)] = 50
grid_z2[(grid_z2>=160)&(grid_z2<=200)] = 100plt.subplot(221)
plt.imshow(grid_z0.T, extent=(0,10,0,10), origin='lower')
plt.plot(points[:,0], points[:,1], 'k.', ms=1)
plt.title('Nearest')plt.subplot(222)
plt.imshow(grid_z1.T, extent=(0,10,0,10), origin='lower')
plt.plot(points[:,0], points[:,1], 'k.', ms=1)
plt.title('Linear')plt.subplot(223)
plt.imshow(grid_z2.T, extent=(0,10,0,10), origin='lower')
plt.plot(points[:,0], points[:,1], 'k.', ms=1)
plt.title('Cubic')plt.gcf().set_size_inches(8, 8)
plt.show()#输入坐标点得到值
while True:point_wanted = []#[5.8,7.2]for i in range(2):point_wanted_input = float(input('please input the point you want'))point_wanted.append(point_wanted_input)point_wanted_x = int(round((point_wanted[0] - four_boundary_point[0])/((four_boundary_point[2]-four_boundary_point[0])/mesh_size_x)))point_wanted_y = int(round((point_wanted[1] - four_boundary_point[1])/((four_boundary_point[3]-four_boundary_point[1])/mesh_size_y)))#将输入的点进行四舍五入得到对应的array数组的值,(x点坐标-x左边界)/((x左边界-x右边界)/分成的小格子)print(point_wanted_x,point_wanted_y)print(grid_z1[point_wanted_x][point_wanted_y])

数据处理(一):python二维插值运算相关推荐

  1. python二维插值_python实现二维插值的三维显示

    本文实例为大家分享了二维插值的三维显示具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- """ 演示二维插值. "" ...

  2. python二维向量运算模拟_【转载 Python】Numpy基础:数组和矢量运算

    Numpy基础:数组和矢量运算 目录: 尊重原创,转载出处:https://blog.csdn.net/sunyaowu315/article/details/82733249?utm_medium= ...

  3. python二维向量运算模拟_Python数学基础之向量定义与向量运算(附代码)

    患难与困苦是磨练人格的最高学府.--苏格拉底(公元前470年-公元前399年) Adversity and pinch are the highest institution of higher le ...

  4. python二维向量运算模拟_python二维向量运算_[VB.NET][C#]二维向量的基本运算

    前言 在数学中,几何向量是指具有大小和方向的几何对象. 在编程中,向量有着广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出. 第一节 构造函数 通过创建一个二维向量的类(或结构体),实现向量的表 ...

  5. python二维向量运算_python中二维数组的Elementwise与or或运算

    numpy有logical_or.logical_xor和{},它们有一个reduce方法>> np.logical_or.reduce(a, axis=0) array([ True, ...

  6. python二维向量运算,二维数组python中的矢量化计数

    像这样的东西呢,它是矢量化的,没有for循环:def moving_count(a, value, axis=0): """Return sequential count ...

  7. python二维数组画出_python二维数组画图

    matplotlib机器学习常用绘图指令_计算机软件及应用_IT/计算机_专业资料.该文档介绍了python的matplotlib库用于数据分析中的图形绘制. ... matplotlib 绘图基础 ...

  8. 数学建模——一维、二维插值模型详解Python代码

    数学建模--一维.二维插值模型详解Python代码 一.一维插值 # -*-coding:utf-8 -*- import numpy as np from scipy import interpol ...

  9. Python实现二维离散卷积运算

    Python实现二维离散卷积运算 卷积运算是图像处理中的重要操作,它可以对图像进行平滑.增强边缘等处理.在Python中,我们可以使用numpy库来实现二维离散卷积运算. 首先,我们需要定义一个二维滤 ...

最新文章

  1. AI与人类围棋士的差距到底有多大?
  2. Python 5种不为人知的高级特征
  3. java 调用服务器文档,JAVA远程调用服务器端方法
  4. oracle中的 where 1=1 和where 11
  5. 基于 websocket 实现的 im 实时通讯案例
  6. DT100pro上手体验
  7. .Net Core下使用 RSA
  8. python实战经典例子_Python入门经典实例
  9. WCF服务可靠性传输配置与编程开发(转)
  10. 【WeUI】关于jQuery WeUI和WeUI版本兼容的问题
  11. [TopCoder] SRM 587 DIV 2, 250p, 500p, 1000p, Solution
  12. vscode快捷键实现快速换行
  13. Python私有属性与私有方法
  14. 8583报文MAC验证实现过程
  15. webgl智慧楼宇发光效果算法系列之高斯模糊
  16. ARM实验板移植Linux操作系统,LCD显示汉字(名字)
  17. Excel计算个人所得税的计算公式:
  18. matlab矩阵的合并
  19. 大咖访谈 | 开源社区里各种奇怪的现状——夜天之书陈梓立tison
  20. 西门子PLC中STL语言状态字

热门文章

  1. 查出每个部门的出勤率前三位的员工
  2. 困难的是融合自己的想法和别人的想法
  3. 全面上手K8S从集群搭建到实现容器编排
  4. ERROR: Cannot determine archive format of /tmp/pip-req-build-pi0rx8q
  5. Nginx搭建域名访问(反向代理配置)
  6. 什么是用户画像,流程、方法是什么?
  7. kooboo中vue router探索
  8. 微信小程序 实现地图
  9. DNS服务器显示两个地址,dns服务器地址为什么有两个
  10. 微信改成右滑删除_微信大更新!微信支持无损发送大文件;新增“裂开”等黄脸表情|一周资讯...