最小二乘法矩陣
創(chuàng)新互聯(lián)成立與2013年,先為陽(yáng)高等服務(wù)建站,陽(yáng)高等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為陽(yáng)高企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。#! /usr/bin/env python # -*- coding: utf-8 -*- import numpy as np def calc_left_k_mat(k): """ 獲得左側(cè)k矩陣 :param k: :return: """ k_mat = [] for i in range(k + 1): now_line = [] for j in range(k + 1): now_line.append(j + i) k_mat.append(now_line) return k_mat def calc_right_k_mat(k): """ 計(jì)算右側(cè)矩陣 :param k: :return: """ k_mat = [] for i in range(k + 1): k_mat.append([i, i + 1]) return k_mat def pow_k(x, k): """ 計(jì)算x列表中的k次方和 :param x: 點(diǎn)集合的x坐標(biāo) :param k: k值 :return: """ sum = 0 for i in x: sum += i ** k return sum def get_left_mat_with_x(k_mat, k): """ 將 左側(cè)k矩陣運(yùn)算得到左側(cè)新的矩陣 :param k_mat: :param k: :return: """ left_mat = [] for kl in k_mat: now_data = [] for k in kl: now_data.append(pow_k(x, k)) left_mat.append(now_data) return left_mat def get_right_mat_with(right_k_mat): """ 將 右側(cè)k矩陣運(yùn)算得到右側(cè)新的矩陣 :param right_k_mat: :return: """ right_mat = [] for i in range(len(right_k_mat)): sum = 0 for xL, yL in zip(x, y): a = (xL ** right_k_mat[i][0]) * (yL ** right_k_mat[i][1]) sum += a right_mat.append(sum) return right_mat def fuse_mat(left, right): """ 融合兩個(gè)矩陣 :param left: :param right: :return: """ new_mat = [] for i in range(len(left)): asd = np.append(left[i], right[i]) new_mat.append(list(asd)) return new_mat if __name__ == '__main__': k = 3 x = [1, 2, 3] y = [1, 2, 3] # 計(jì)算原始左側(cè)K矩陣 left_k_mat = calc_left_k_mat(k) print("原始左側(cè)K矩陣") print(left_k_mat) # 計(jì)算原始右側(cè)K矩陣 right_k_mat = calc_right_k_mat(k) print("原始右側(cè)k矩陣") print(right_k_mat) # 計(jì)算左側(cè) k 矩陣 new_left_mat = get_left_mat_with_x(k_mat=left_k_mat, k=k) # 計(jì)算右側(cè) k 矩陣 new_right_mat = get_right_mat_with(right_k_mat=right_k_mat) print("計(jì)算后左側(cè)K矩陣") print(new_left_mat) print("計(jì)算后右側(cè)側(cè)K矩陣") print(new_right_mat) print("-----" * 10) # 融合兩個(gè)矩陣 左側(cè) 矩陣每一行增加 右側(cè)矩陣的對(duì)應(yīng)行 new_all = fuse_mat(new_left_mat, new_right_mat) print("完整矩陣") print(new_all)