Allenfenqu/distance_to_origin.py

25 lines
748 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
def distance_to_origin(chromosome_results, data=None):
# 如果data不为空则堆叠数据
if data is not None:
chromosome_results = np.vstack((chromosome_results, data))
# 获取每个特征的范数
norms = np.linalg.norm(chromosome_results, axis=0)
# 检查是否有零值并替换它们,以避免除以零
norms[norms == 0] = 1.0 # 或使用一个非常小的数来替换零
# 归一化处理
normalized_data = chromosome_results / norms
# 计算归一化数据到原点的距离
distances = np.linalg.norm(normalized_data, axis=1)
# 如果data不为空则删除最后一行
if data is not None:
distances = distances[:-1]
return distances