def map_replace(str_: str, key: [list, str] = None, value: [list, str] = None, maps: dict = None) -> str: """映射替换 :param str_: 字符串 :param key: 替换字符 :param value: 被替换的字符 :param maps:字符映射 :return: 替换完毕的字符串 """ if maps: key, value = '', '' for k, v in maps.items(): key += k value += v else: if len(key) != len(value): raise CoordinateLengthNotEqualError('替换的长度必须相同!') if isinstance(key, list): key = ''.join(key) if isinstance(value, list): value = ''.join(value) m = str.maketrans(key, value) return str_.translate(m)
def mahalanobis_distance(coordinate_x, coordinate_y): """ 求解马氏距离 :param coordinate_x: x轴向量 :param coordinate_y: y轴向量 :return result: 两个点的马氏距离 """ if len(coordinate_x) != len(coordinate_y): raise CoordinateLengthNotEqualError("坐标长度不一致") # 马氏距离要求样本数要大于维数,否则无法求协方差矩阵 # 此处进行转置,表示10个样本,每个样本2维 x = np.vstack([coordinate_x, coordinate_y]) x_t = x.T # 转置 s = np.cov(x) # 两个维度之间协方差矩阵 s_i = np.linalg.inv(s) # 协方差矩阵的逆矩阵 # 马氏距离计算两个样本之间的距离,此处共有4个样本,两两组合,共有6个距离。 n = x_t.shape[0] result = [] for i in range(0, n): for j in range(i + 1, n): delta = x_t[i] - x_t[j] point = x_t[i], x_t[j] distance = np.sqrt(np.dot(np.dot(delta, s_i), delta.T)) result.append((point, distance)) return result
def map_replace(str_: str, key: [list, str] = None, value: [list, str] = None, maps: dict = None) -> str: """映射替换,最好使用maps字典映射 >>> print(map_replace('[中国]', '[]', '【】')) #【中国】 >>> print(map_replace('[中国]', maps={'[': '【', ']': '】'})) #【中国】 :param str_: 字符串 :param key: 替换字符 :param value: 被替换的字符 :param maps: 字符映射 :return: 替换完毕的字符串 """ if maps: strings = str_ for k, v in maps.items(): strings = strings.replace(k, v) return strings else: if len(key) != len(value): raise CoordinateLengthNotEqualError('替换的长度必须相同!') if isinstance(key, list): key = ''.join(key) if isinstance(value, list): value = ''.join(value) m = str.maketrans(key, value) return str_.translate(m)
def manhattan_distance(coordinate_p, coordinate_q): """ 曼哈顿距离 :param coordinate_p: p坐标 :param coordinate_q: q坐标 :return: 曼哈顿距离值 """ if len(coordinate_q) != len(coordinate_p): raise CoordinateLengthNotEqualError("坐标长度不一致") point = (abs(x - y) for x, y in zip(coordinate_q, coordinate_p)) return sum(point)
def chebyshev_distance(coordinate_p, coordinate_q): """ 传入的是两个可迭代对象,每一个是一个n位坐标,比如:p=(x1,x2,x3....xn),q=(y1,y2,y3....yn) :param coordinate_p: p坐标 :param coordinate_q: q坐标 :return: 切比雪夫距离(棋盘距离) """ if len(coordinate_q) != len(coordinate_p): raise CoordinateLengthNotEqualError("坐标长度不一致") coordinate = [] for x, y in zip(coordinate_p, coordinate_q): coordinate.append(abs(x - y)) return max(coordinate)
def euclidean_distance(coordinate_p, coordinate_q): """ 欧氏距离 :param coordinate_p: p坐标 :param coordinate_q: q坐标 :return: 欧氏距离值 """ if len(coordinate_q) != len(coordinate_p): raise CoordinateLengthNotEqualError("坐标长度不一致") numerator = 0 for x, y in zip(coordinate_p, coordinate_q): numerator += (x - y)**2 return math.sqrt(numerator)
def cosine_distance(coordinate_p, coordinate_q): """ 余弦距离又叫余弦角度 :param coordinate_p:p坐标 :param coordinate_q:q坐标 :return:余弦距离 """ if len(coordinate_q) != len(coordinate_p): raise CoordinateLengthNotEqualError("坐标长度不一致") numerator, denominator = 0, 1 # 分子和父母 for x, y in zip(coordinate_p, coordinate_q): numerator += x * y q = [x**2 for x in coordinate_q] p = [y**2 for y in coordinate_p] return numerator / math.sqrt(sum(q) * sum(p))
def wraps(x, y): if not (x and y): raise ParameterNotEmptyError("参数不能为空") if len(x) != len(y): raise CoordinateLengthNotEqualError("两个参数长度不一致") return fun(x, y)