python的常见矩阵运算

python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入numpy的包。

1.numpy的导入和使用

<code class="hljs python has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> numpy <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> *;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#导入numpy的库函数</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> numpy <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">as</span> np; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#这个方式使用numpy的函数时,需要以np.开头。</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

2.矩阵的创建

由一维或二维数据创建矩阵

<code class="hljs python has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> numpy <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> *;
a1=array([<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]);
a1=mat(a1);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

创建常见的矩阵

<code class="hljs vala has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">data1=mat(zeros((<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)));
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#创建一个3*3的零矩阵,矩阵这里zeros函数的参数是一个tuple类型(3,3)</span>
data2=mat(ones((<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>)));
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#创建一个2*4的1矩阵,默认是浮点型的数据,如果需要时int类型,可以使用dtype=int</span>
data3=mat(random.rand(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>));
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#这里的random模块使用的是numpy中的random模块,random.rand(2,2)创建的是一个二维数组,需要将其转换成#matrix</span>
data4=mat(random.randint(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>,size=(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)));
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#生成一个3*3的0-10之间的随机整数矩阵,如果需要指定下界则可以多加一个参数</span>
data5=mat(random.randint(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>,size=(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>));
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#产生一个2-8之间的随机整数矩阵</span>
data6=mat(eye(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,dtype=<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>));
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#产生一个2*2的对角矩阵</span>

a1=[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>];
a2=mat(diag(a1));
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#生成一个对角线为1、2、3的对角矩阵</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>

3.常见的矩阵运算

1. 矩阵相乘

<code class="hljs lua has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a1=mat([<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>]);      
a2=mat(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[1],[2]]</span>);
a3=a1*a2;
#<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>的矩阵乘以<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>的矩阵,得到<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>的矩阵</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

2. 矩阵点乘

矩阵对应元素相乘

<code class="hljs ini has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-setting" style="box-sizing: border-box;">a1=<span class="hljs-value" style="box-sizing: border-box;">mat([<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]);</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">a2=<span class="hljs-value" style="box-sizing: border-box;">mat([<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>]);</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">a3=<span class="hljs-value" style="box-sizing: border-box;">multiply(a1,a2);</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

矩阵点乘

<code class="hljs ini has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-setting" style="box-sizing: border-box;">a1=<span class="hljs-value" style="box-sizing: border-box;">mat([<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>]);</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">a2=<span class="hljs-value" style="box-sizing: border-box;">a1*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>;</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

3.矩阵求逆,转置 
矩阵求逆

<code class="hljs lua has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a1=mat(eye(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)*<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0.5</span>);
a2=a1.I;
#求矩阵matrix(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[0.5,0],[0,0.5]]</span>)的逆矩阵</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

矩阵转置

<code class="hljs lua has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a1=mat(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[1,1],[0,0]]</span>);
a2=a1.T;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

4.计算矩阵对应行列的最大、最小值、和。

<code class="hljs lua has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a1=mat(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[1,1],[2,3],[4,2]]</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

计算每一列、行的和

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a2=a1.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sum</span>(axis=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//列和,这里得到的是1*2的矩阵</span>
a3=a1.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sum</span>(axis=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//行和,这里得到的是3*1的矩阵</span>
a4=<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sum</span>(a1[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,:])<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;//计算第一行所有列的和,这里得到的是一个数值</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

计算最大、最小值和索引

<code class="hljs glsl has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a1.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span>();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算a1矩阵中所有元素的最大值,这里得到的结果是一个数值</span>
a2=<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span>(a1[:,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算第二列的最大值,这里得到的是一个1*1的矩阵</span>
a1[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,:].<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span>();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算第二行的最大值,这里得到的是一个一个数值</span>

np.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span>(a1,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算所有列的最大值,这里使用的是numpy中的max函数</span>
np.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span>(a1,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算所有行的最大值,这里得到是一个矩阵</span>

np.argmax(a1,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算所有列的最大值对应在该列中的索引</span>
np.argmax(a1[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,:]);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算第二行中最大值对应在改行的索引</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

5.矩阵的分隔和合并 
矩阵的分隔,同列表和数组的分隔一致。

<code class="hljs ini has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-setting" style="box-sizing: border-box;">a=<span class="hljs-value" style="box-sizing: border-box;">mat(ones((<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)));</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">b=<span class="hljs-value" style="box-sizing: border-box;">a[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>:,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>:];//分割出第二行以后的行和第二列以后的列的所有元素</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

矩阵的合并

<code class="hljs ini has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-setting" style="box-sizing: border-box;">a=<span class="hljs-value" style="box-sizing: border-box;">mat(ones((<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>)));</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">b=<span class="hljs-value" style="box-sizing: border-box;">mat(eye(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>));</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">c=<span class="hljs-value" style="box-sizing: border-box;">vstack((a,b));//按列合并,即增加行数</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">d=<span class="hljs-value" style="box-sizing: border-box;">hstack((a,b));//按行合并,即行数不变,扩展列数</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

4.矩阵、列表、数组的转换

列表可以修改,并且列表中元素可以使不同类型的数据,如下:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">l1=[[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>],<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'hello'</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

numpy中数组,同一个数组中所有元素必须为同一个类型,有几个常见的属性:

<code class="hljs lua has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a=array(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[2],[1]]</span>);
dimension=a.ndim;
m,n=a.shape;
number=a.size;//元素总个数
str=a.dtype;//元素的类型</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

numpy中的矩阵也有与数组常见的几个属性。 
它们之间的转换:

<code class="hljs lua has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a1=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[1,2],[3,2],[5,2]]</span>;//列表
a2=array(a1);//将列表转换成二维数组
a3=array(a1);//将列表转化成矩阵
a4=array(a3);//将矩阵转换成数组
a5=a3.tolist();//将矩阵转换成列表
a6=a2.tolist();//将数组转换成列表</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

这里可以发现三者之间的转换是非常简单的,这里需要注意的是,当列表是一维的时候,将它转换成数组和矩阵后,再通过tolist()转换成列表是不相同的,需要做一些小小的修改。如下:

<code class="hljs lua has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">a1=[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>];
a2=array(a1);
a3=mat(a1);
a4=a2.tolist();//这里得到的是[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]
a5=a3.tolist();//这里得到的是<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">[[1,2,3]]</span>
a6=(a4 == a5);//a6=False
a7=(a4 is a5[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]);//a7=True,a5[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]=[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>]</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

矩阵转换成数值,存在以下一种情况:

<code class="hljs fsharp has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">dataMat=mat([<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">val</span>=dataMat[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>];<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//这个时候获取的就是矩阵的元素的数值,而不再是矩阵的类型</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值