存档在 2017年11月

Do I need to install pip?

2017年11月27日

Installation

Do I need to install pip?

pip is already installed if you’re using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloaded from python.org, but you’ll need to upgrade pip.

Additionally, pip will already be installed if you’re working in a Virtual Environment created byvirtualenv or pyvenv.

Installing with get-pip.py

To install pip, securely download get-pip.py[2]

Then run the following:

python get-pip.py

Warning

Be cautious if you’re using a Python install that’s managed by your operating system or another package manager. get-pip.py does not coordinate with those tools, and may leave your system in an inconsistent state.

get-pip.py will also install setuptools [3] and wheel, if they’re not already. setuptools is required to install source distributions. Both are required to be able to build a Wheel Cache (which improves installation speed), although neither are required to install pre-built wheels.

Note

The get-pip.py script is supported on the same python version as pip. For the now unsupported Python 3.2, an alternate script is available here.

get-pip.py options

--no-setuptools
If set, don’t attempt to install setuptools

--no-wheel
If set, don’t attempt to install wheel

Additionally, get-pip.py supports using the pip install options and the general options. Below are some examples:

Install from local copies of pip and setuptools:

python get-pip.py --no-index --find-links=/local/copies

Install to the user site [4]:

python get-pip.py --user

Install behind a proxy:

python get-pip.py --proxy="[user:passwd@]proxy.server:port"

Upgrading pip

On Linux or macOS:

pip install -U pip

On Windows [5]:

python -m pip install -U pip

Python and OS Compatibility

pip works with CPython versions 2.6, 2.7, 3.3, 3.4, 3.5 and also pypy.

This means pip works on the latest patch version of each of these minor versions (i.e. 2.6.9 for 2.6, etc). Previous patch versions are supported on a best effort approach.

pip works on Unix/Linux, macOS, and Windows.


[1] For Python 2, see https://docs.python.org/2/installing, and for Python3, see https://docs.python.org/3/installing.
[2] “Secure” in this context means using a modern browser or a tool like curl that verifies SSL certificates when downloading from https URLs.
[3] Beginning with pip v1.5.1, get-pip.py stopped requiring setuptools to be installed first.
[4] The pip developers are considering making --user the default for all installs, including get-pip.pyinstalls of pip, but at this time, --user installs for pip itself, should not be considered to be fully tested or endorsed. For discussion, see Issue 1668.

矩阵的迹 特征值

2017年11月25日

矩阵的迹(trace)

X∈P(n×n,X=(xii)的主对角线上的所有元素之和称之为X的迹,记为tr(X),即tr(X)=∑xii

性质:

(1)

设有N阶矩阵A,那么矩阵A的迹(用tr(A)表示)就等于A的特征值的总和,也即A矩阵的主对角线元素的总和。

1.迹是所有对角元的和

2.迹是所有特征值的和

3.某些时候也利用tr(AB)=tr(BA)来求迹

(2)

奇异值分解(Singular value decomposition )

奇异值分解非常有用,对于矩阵A(p*q),存在U(p*p),V(q*q),B(p*q)(由对角阵与增广行或列组成),满足A = U*B*V

U和V中分别是A的奇异向量,而B是A的奇异值。AA’的特征向量组成U,特征值组成B’B,A’A的特征向量组成V,特征值(与AA’相同)组成BB’。因此,奇异值分解和特征值问题紧密联系。

如果A是复矩阵,B中的奇异值仍然是实数。

SVD提供了一些关于A的信息,例如非零奇异值的数目(B的阶数)和A的阶数相同,一旦阶数确定,那么U的前k列构成了A的列向量空间的正交基。

 

 

矩阵的特征值(eigenvalue)

设 A 是n阶方阵,如果存在数m和非零n维列向量 x,使得 Ax=mx 成立,则称 m 是A的一个特征值(characteristic value)或本征值(eigenvalue)。

求解矩阵特征值的方法:

Ax=mx,等价于求m,使得(mE-A)x=0,其中E是单位矩阵,0为零矩阵。

|mE-A|=0,求得的m值即为A的特征值。|mE-A| 是一个n次多项式,它的全部根就是n阶方阵A的全部特征值,这些根有可能相重复,也有可能是复数。

如果n阶矩阵A的全部特征值为m1 m2 … mn,则|A|=m1*m2*…*mn

同时矩阵A的迹是特征值之和:tr(A)=m1+m2+m3+…+mn[1]

如果n阶矩阵A满足矩阵多项式方程g(A)=0, 则矩阵A的特征值m一定满足条件g(m)=0;特征值m可以通过解方程g(m)=0求得