php-Arrays 函数-array_diff_uassoc-用回调函数做索引检查来计算数组的差值

本文详细介绍了PHP中的array_diff_uassoc函数,包括其功能、使用范围、语法、示例和注意事项。重点阐述了如何通过回调函数进行索引检查来计算数组的差集,适用于PHP5及以上版本。
array_diff_uassoc() 函数 用回调函数做索引检查来计算数组的差集

【功能】

         该函数将返回一个数组,

         该数组包含了所有在array1中但是不在其他任何参数数组中的值。

         如果认为第一个参数小于、等于、或大于第二个参数时,必须返回

         一个小于零、等于零,或大于零的整数

【使用范围】 

         php5(我用5.1.6测试过该函数,是不正确的。5.3.3是正确的,具体哪些版本好使有待验证)

【使用】

         array array_diff_uassoc( array array1, array array2[,array...,callback key_compare_func]  ) 

         array1/必需/数组1

         array2/必需/比较的数组 最少得有一个

         array.../可选/用来比较的数组

         key_compare_func.../必需/为用户提供作为比较标准的回调函数

【示例】

<?php 
function myfunction($v1,$v2) { if ($v1===$v2) { return 0; 
} if ($v1>$v2) { return 1; 
} else { return -1; 
} } $a1=array(0=>"Dog",1=>"Cat",2=>"Horse"); 
$a2=array(3=>"Dog",1=>"Cat",5=>"Horse"); 
print_r(array_diff_uassoc($a1,$a2,"myfunction")); 
?>
Array ( [0] => Dog [2] => Horse )

# 主循环优化 def batch_weighted_prediction(recipe_id_batch, normalized_base_centers, loaded_models, result, center_idx=0): batch_size = len(recipe_id_batch) num_features = recipe_id_batch.shape[1] # 1. 计算距离和最近中心索引 distances = manhattan_distances(recipe_id_batch, normalized_base_centers) # (batch_size, num_centers) nearest_indices = np.argmin(distances, axis=1) # (batch_size,) # 2. 计算权重(批量处理) weights_batch = compute_batch_weights(distances) # (batch_size, num_models) # 3. 批量计算diff_x nearest_centers = normalized_base_centers[nearest_indices] # (batch_size, num_features) diff_x = recipe_id_batch - nearest_centers # (batch_size, num_features) # 4. 构建每个 batch 的模型到权重的映射 name_to_weight_list = [ { name: weights[idx] for name, idx in result['recipe_index_map'].items() } for weights in weights_batch ] # 可选:构建平均权重的映射(用于全局参考) avg_weights = np.mean(weights_batch, axis=0) name_to_weight_avg = { name: avg_weights[idx] for name, idx in result['recipe_index_map'].items() } # 5. 初始化预测字典 predictions = {key: np.zeros((batch_size, 1)) for key in loaded_models.keys()} # 遍历每个 batch,分别使用其对应的权重进行预测 for batch_idx in range(batch_size): current_weights = name_to_weight_list[batch_idx] for key, models in loaded_models.items(): model_weight_list = [] for model in models: base_name = model['name'].replace(f'#{key}', '') if base_name in current_weights: model_weight = current_weights[base_name] model_weight_list.append((model, model_weight)) # 排序并取 top5 模型 top5_models = sorted(model_weight_list, key=lambda x: x[1], reverse=True)[:5] # 对 top5 模型进行预测并加权累加 for model, model_weight in top5_models: pred = model['model'].predict(diff_x[batch_idx:batch_idx + 1]).reshape(1) # (1,) predictions[key][batch_idx] += pred[0] * model_weight # 6. 按顺序整理结果 ordered_predictions = [] for spec_key in result['spec_name']: cleaned_key = spec_key.replace(' ', '') ordered_predictions.append(predictions[cleaned_key]) # 7. 计算最终结果 base_arrays = np.array(result['specMatrix'])[nearest_indices] # (batch_size, output_dim) final_results = base_arrays + np.hstack(ordered_predictions) # (batch_size, output_dim + len(spec_name)) return final_results 帮我参考以上代码实现将每个输入点的差值计算和模型预测逻辑从基于固定基准点(base_point)改为基于最近邻点作为基准点: # 预计算基准点 base_point = normalized_base_centers[recipe_index].reshape(1, -1) def batch_process_points(grid_points_batch, pair_indices, base_point, loaded_models, result, recipe_index): """ 批量处理多个网格点的预测任务 :param grid_points_batch: array-like, shape=(batch_size, n_features) :param pair_indices: list of indices for selected features :param base_point: 基准点 :param loaded_models: 模型列表 :param result: 结果结构 :param recipe_index: 当前配方索引 :return: 预测结果 array, shape=(batch_size, output_dim) """ batch_size = len(grid_points_batch) # 计算差值 diff_x = grid_points_batch - base_point # 批量预测 predicted_gbr_separated = {} for model in loaded_models: prediction = model['model'].predict(diff_x) predicted_gbr_separated[model['name'].replace('#','/')] = prediction # 验证并重排结果 assert set(predicted_gbr_separated) == set(result['minMatrix']), "模型输出键值不匹配" predicted_gbr_separated_reordered = {k: predicted_gbr_separated[k] for k in result['minMatrix']} # 拼接预测结果 predicted_gbr_all = np.column_stack(list(predicted_gbr_separated_reordered.values())) # 添加基础值 base_array = np.array(result['specMatrix'][recipe_index]).reshape(1, -1) predicted_results = np.repeat(base_array, batch_size, axis=0) + predicted_gbr_all return predicted_results
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值