python:获取两个列表中数值最接近的元素(元素匹配算法)


获取两个列表中数值最接近的元素:


  • 在处理数据中,经常需要匹配两个数据集合,找到最接近的那两个元素。

例如:

  • 在处理pcd点云数据和图像的时候,需要在点云和图像两个集合中找到时间戳相对应的那一帧数据

  • 以下为代码实例:
def pcd_match_img(pcd_path, img_path):
	pcd_file = os.listdir(pcd_path)
	img_file = os.listdir(img_path)
	for pcd in pcd_file:
		pcd_prefix = os.path.splitext(pcd)[0]
		dict = {k:eval(pcd_prefix) - eval(os.path.splitext(k)[0] for k in img_file}
		value = 0
		img_key, diff_val = min(dict.items(), key=lambda x: abs(value - x[1]))
		src_img = os.path.jion(src_path, "jpg", img_key)
		new_img_path = src_path + "/img_new"
		if not os.path.exists(new_img_path):
			os.makedirs(new_img_path)
		new_img = os.path.jion(new_img_path, pcd_prefix + ".jpg")
		shutil.copyfile(src_img, new_img)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这是一个比较复杂的算法,需要一些时间来编写。以下是我编写的代码,希望能够帮到你: ```python import tensorflow as tf from tensorflow.keras import layers import numpy as np # 定义MI互信息匹配层 class MILayer(layers.Layer): def __init__(self, **kwargs): super().__init__(**kwargs) def call(self, inputs): # 获取输入 x1, x2 = inputs # 将输入转换为概率分布 p1 = tf.nn.softmax(x1, axis=-1) p2 = tf.nn.softmax(x2, axis=-1) # 计算互信息 mi = tf.reduce_sum(p1 * tf.math.log(tf.clip_by_value(p1 / (tf.reduce_sum(p1, axis=-1, keepdims=True) * tf.reduce_sum(p2, axis=-2, keepdims=True)), 1e-8, 1.0)), axis=-1) # 返回结果 return mi # 定义模型 class CNN_MI(tf.keras.Model): def __init__(self): super().__init__() # 定义卷积层 self.conv1 = layers.Conv2D(32, (3, 3), activation='relu') self.conv2 = layers.Conv2D(64, (3, 3), activation='relu') self.conv3 = layers.Conv2D(128, (3, 3), activation='relu') # 定义全连接层 self.fc1 = layers.Dense(128, activation='relu') self.fc2 = layers.Dense(64, activation='relu') self.fc3 = layers.Dense(32, activation='relu') # 定义输出层 self.out = layers.Dense(2, activation='softmax') # 定义MI互信息匹配层 self.mi_layer = MILayer() def call(self, inputs): # 获取输入 x1, x2 = inputs # 第一层卷积 x1 = self.conv1(x1) x2 = self.conv1(x2) # 第二层卷积 x1 = self.conv2(x1) x2 = self.conv2(x2) # 第三层卷积 x1 = self.conv3(x1) x2 = self.conv3(x2) # 展开成一维向量 x1 = layers.Flatten()(x1) x2 = layers.Flatten()(x2) # 全连接层 x1 = self.fc1(x1) x2 = self.fc1(x2) x1 = self.fc2(x1) x2 = self.fc2(x2) x1 = self.fc3(x1) x2 = self.fc3(x2) # 输出层 out1 = self.out(x1) out2 = self.out(x2) # 计算互信息 mi = self.mi_layer([out1, out2]) # 返回结果 return mi # 定义数据生成器 def data_generator(batch_size=32): while True: # 随机生成两张图片 x1 = np.random.random((batch_size, 28, 28, 1)) x2 = np.random.random((batch_size, 28, 28, 1)) # 随机生成标签 y = np.random.randint(2, size=batch_size) # 返回数据 yield [x1, x2], y # 训练模型 model = CNN_MI() model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(data_generator(), epochs=10, steps_per_epoch=100) ``` 这个模型包含了三层卷积层,三层全连接层和一个输出层。其,输出层的输出经过了一个MI互信息匹配层计算得出。数据生成器会不断生成两张随机图片和随机标签,用于模型的训练。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ywfwyht

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值