• 特征值和特征向量


    对于n阶方阵A,如果存在数a和非零n维列向量x,使得Ax=ax,则称a是矩阵A的一个特征值,x是矩阵A属于特征值a的特征向量

    #已知n阶方阵A, 求特征值与特征数组
    # eigvals: 特征值数组
    # eigvecs: 特征向量数组 
    eigvals, eigvecs = np.linalg.eig(A)
    #已知特征值与特征向量,求方阵
    S = np.mat(eigvecs) * np.mat(np.diag(eigvals)) * np.mat(eigvecs逆) 

    案例:

    """
     特征值提取
    """
    import numpy as np
    
    A = np.mat('1 3 6; 2 8 4; 7 9 2')
    print(A)
    """
    [[1 3 6]
     [2 8 4]
     [7 9 2]]
    """
    # 提取特征值与特征向量
    eigvals, eigvecs = np.linalg.eig(A)
    print(eigvals)#[14.21001548 -5.47304086  2.26302539]
    print(eigvecs)
    """
    [[ 0.44215443  0.6411674   0.72030524]
     [ 0.58199892  0.12939322 -0.54780134]
     [ 0.68247836 -0.75641375  0.42552797]]
    """
    # 逆向推导原矩阵
    M = eigvecs * np.diag(eigvals) * eigvecs.I
    print(M)
    """
    [[1. 3. 6.]
     [2. 8. 4.]
     [7. 9. 2.]]
    """
    # 抹掉部分特征值,生成原矩阵
    eigvals[2:] = 0
    M = eigvecs * np.diag(eigvals) * eigvecs.I
    print(M)
    """
    [[0.08157038 4.34154338 5.45098762]
     [2.69847746 6.97973912 4.41753093]
     [6.45742795 9.792531   1.67566511]]
    """
    import numpy as np
    A = np.mat('3 -2; 1 0')
    print(A)
    """
    [[ 3 -2]
     [ 1  0]]
    """
    eigvals, eigvecs = np.linalg.eig(A)
    print(eigvals)#[2. 1.]
    print(eigvecs)
    """
    [[0.89442719 0.70710678]
     [0.4472136  0.70710678]]
    """
    print(A * eigvecs[:, 0])    # 方阵*特征向量
    """
    [[1.78885438]
     [0.89442719]]
    """
    print(eigvals[0] * eigvecs[:, 0])    #特征值*特征向量
    """
    [[1.78885438]
     [0.89442719]]
    """
    S = np.mat(eigvecs) * np.mat(np.diag(eigvals)) * np.mat(eigvecs.I)
    print(S)
    """
    [[ 3.00000000e+00 -2.00000000e+00]
     [ 1.00000000e+00 -4.05861454e-17]]
    """

    案例:读取图片的亮度矩阵,提取特征值与特征向量,保留部分特征值,重新生成新的亮度矩阵,绘制图片。

    # 图片特征提取
    import numpy as np
    import scipy.misc as sm
    import matplotlib.pyplot as mp
    
    # 读取图片数据,True提取项目矩阵
    img = sm.imread('lily.jpg', True)
    print(img, img.shape)
    """
    [[36.742 35.97  34.97  ... 54.102 54.33  51.042]
     [37.742 37.742 37.97  ... 54.102 53.33  51.042]
     [40.34  41.112 40.34  ... 51.732 51.846 50.857]
     ...
     [ 5.228  6.228  5.929 ... 71.666 68.954 68.242]
     [ 6.228  6.228  4.929 ... 73.182 72.182 71.242]
     [ 6.228  6.228  4.929 ... 69.709 71.709 74.242]] (512, 512)
    """
    # 提取特征值
    eigvals, eigvecs = np.linalg.eig(np.mat(img))
    #逆向推到原矩阵
    eigvals[50:] = 0
    img2 = eigvecs*np.diag(eigvals)*eigvecs.I
    # 绘制图片
    mp.figure('Lily', facecolor='lightgray')
    mp.subplot(121)
    mp.imshow(img,cmap='gray')
    mp.xticks([])
    mp.yticks([])
    mp.subplot(122)
    mp.imshow(img2.real,cmap='gray')
    mp.xticks([])
    mp.yticks([])
    mp.tight_layout()
    mp.show()

  • 相关阅读:
    Android 通用获取Ip的方法(判断手机是否联网的方法)!!!
    android intent和intent action大全
    手机信息
    Android 获取信号强度
    android 基站定位
    Android 读取SIM卡参数
    Android 获取手机SIM资料详解
    Android手机中获取手机号码和运营商信息
    java中的url 编码与解码
    java.util.Scanner的日常用法
  • 原文地址:https://www.cnblogs.com/maplethefox/p/11489819.html
Copyright © 2020-2023  润新知