大数据,机器学习,人工智能人工智能/模式识别/机器学习精华专题

线性代数2-线性组合

2019-12-10  本文已影响0人  赵阳_c149

线性组合定义

一般来说,线性组合是指将标量与向量相乘,并将这些项相加。

当某个向量可以定义为其他向量的线性组合时,它们是一组线性相关的向量。

当一组向量中的每个向量都无法定义为其他向量的线性组合时,它们是一组线性不相关的向量。

判断一组向量是否为线性相关集合的最简单方式是采用行列式

张成

如果 \vec{v_1}, \vec{v_2},....., \vec{v_n} \in \mathbb{R} 那么,这些向量的张成(有时候也称为线性张成)是指这些向量的所有可能的线性组合。

从数学角度来讲,向量集合\vec{v_1}, \vec{v_2},....., \vec{v_n}的张成表示为:

Sp(\vec{v_1}, \vec{v_2},....., \vec{v_n})

确定向量的张成

假设有一对向量\vec{v}\vec{w},我们想要判断它们的张成中是否存在第三个向量\vec{t},表示\vec{t}可以写成向量对\vec{v}\vec{w}的线性组合。

可以表示为:

𝑎\vec{v} + 𝑏\vec{w} =\vec{t}, 其中\vec{v}\vec{w}分别乘以标量 𝑎 和 𝑏 ,然后相加生成向量\vec{t}

如果我们能够找到一组使上式成立的标量 𝑎 和 𝑏 ,那么\vec{t}位于\vec{v}\vec{w} 的张成内。否则,如果没有使上式成立的标量 𝑎 和 𝑏 ,那么\vec{t} 不在它们的张成内。

实例

如果向量的值如下所示:​
\vec{v}=\begin{bmatrix} 1\\3 \end{bmatrix}
\vec{w}=\begin{bmatrix} 2\\5 \end{bmatrix}
\vec{t}=\begin{bmatrix} 4\\11 \end{bmatrix}

可以将 𝑎𝑣⃗ +𝑏𝑤⃗ =𝑡⃗ 重新写成:​
𝑎\begin{bmatrix} 1\\3 \end{bmatrix}+𝑏\begin{bmatrix} 2\\5 \end{bmatrix}=\begin{bmatrix} 4\\11 \end{bmatrix}

在线性代数课程中,可以手动求解这个问题:使用简化的梯阵形式,并将上式重写为增广矩阵。我们在下面提供了上式的增广矩阵:
\begin{bmatrix}1\ 2 \ |4\\ 3 \ 5 \ |11 \end{bmatrix}

注意,增广矩阵的右侧包含向量\vec{t}。我们要判断此向量是否位于另外两个向量\vec{v}\vec{w}的张成内。我们要检查其张成的另外两个向量组成了增广矩阵的左侧。

求解简化的方程组

向量与标量的线性组合将延伸出以下这个重要概念: 线性方程组
在这里,仅介绍有两个方程和两个变量的方程组。

在更宽泛的线性代数课程中,可以详细了解 含有n 个线性方程的方程组,其中n可以是任何数字。

python演示

# Makes Python package NumPy available using import method
import numpy as np

# Creates matrix t (right side of the augmented matrix).
t = np.array([4, 11])
# Creates matrix vw (left side of the augmented matrix).
vw = np.array([[1, 2], [3, 5]])

# Prints vw and t
print("\nMatrix vw:", vw, "\nVector t:", t, sep="\n")
def check_vector_span(set_of_vectors, vector_to_check):
    # Creates an empty vector of correct size
    vector_of_scalars = np.asarray([None]*set_of_vectors.shape[0])
    
    # Solves for the scalars that make the equation true if vector is within the span
    try:
        # TODO: Use np.linalg.solve() function here to solve for vector_of_scalars
        vector_of_scalars = np.linalg.solve(set_of_vectors, vector_to_check)
        if not (vector_of_scalars is None):
            print("\nVector is within span.\nScalars in s:", vector_of_scalars)
    # Handles the cases when the vector is NOT within the span   
    except Exception as exception_type:
        if str(exception_type) == "Singular matrix":
            print("\nNo single solution\nVector is NOT within span")
        else:
            print("\nUnexpected Exception Error:", exception_type)
    return vector_of_scalars

# Call to check_vector_span to check vectors in Equation 1
print("\nEquation 1:\n Matrix vw:", vw, "\nVector t:", t, sep="\n")
s = check_vector_span(vw,t)

# Call to check a new set of vectors vw2 and t2
vw2 = np.array([[1, 2], [2, 4]]) 
t2 = np.array([6, 12])
print("\nNew Vectors:\n Matrix vw2:", vw2, "\nVector t2:", t2, sep="\n")    
# Call to check_vector_span
s2 = check_vector_span(vw2,t2)

# Call to check a new set of vectors vw3 and t3
vw3 = np.array([[1, 2], [1, 2]]) 
t3 = np.array([6, 10])
print("\nNew Vectors:\n Matrix vw3:", vw3, "\nVector t3:", t3, sep="\n")    
# Call to check_vector_span
s3 = check_vector_span(vw3,t3)

方程组的解

方程组的解始终是三种潜在情况之一。当向量位于张成内时,有一个解,如上面的示例所示。当向量位于张成内时,有一个解或无穷多解;当向量不在张成内时,无解。

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([4,0],[0,2],'b',linewidth=3)
plt.plot([3.6667,0],[0,2.2],'c-.',linewidth=3)
plt.plot([2],[1],'ro',linewidth=3)
plt.xlabel('Single Solution')
plt.show()
import matplotlib.pyplot as plt
plt.plot([6,0],[0,3],'b',linewidth=5)
plt.plot([1,4,6,0],[2.5,1,0,3],'c-.',linewidth=2)
plt.xlabel('Redundant Equations')
plt.show()
import matplotlib.pyplot as plt
plt.plot([10,0],[0,5],'b',linewidth=3)
plt.plot([0,6],[3,0],'c-.',linewidth=3)
plt.xlabel('No Solution')
plt.show()

参考:
linalg

上一篇 下一篇

猜你喜欢

热点阅读