# Linear Algebra and Python Basics

by Rob Hicks http://rlhick.people.wm.edu/stories/linear-algebra-python-basics.html

In this chapter, I will be discussing some linear algebra basics that will provide sufficient linear algebra background for effective programming in Python for our purposes.  We will be doing very basic linear algebra that by no means covers the full breadth of this topic.  Why linear algebra?  Linear algebra allows us to express relatively complex linear expressions in a very compact way.

Being comfortable with the rules for scalar and matrix addition, subtraction, multiplication, and division (known as inversion) is important for our class.

Before we can implement any of these ideas in code, we need to talk a bit about python and how data is stored.

## Python Primer

There are numerous ways to run python code.  I will show you two and both are easily accessible after installing Anaconda:

1. The Spyder integrated development environment.  The major advantages of Spyder is that it provides a graphical way for viewing matrices, vectors, and other objects you want to check as you work on a problem.  It also has the most intuitive way of debugging code.

    Spyder looks like this:
    
    Code can be run by clicking the green arrow (runs the entire file) or by blocking a subset and running it.
    In Windows or Mac, you can launch the Spyder by looking for the icon in the newly installed Program Folder Anaconda.  
    
2. The Ipython Notebook (now called Jupyter).  The major advantages of this approach is that you use your web browser for all of your python work and you can mix code, videos, notes, graphics from the web, and mathematical notation to tell the whole story of your python project. In fact, I am using the ipython notebook for writing these notes. 
    The Ipython Notebook looks like this:
    
    In Windows or Mac, you can launch the Ipython Notebook by looking in the newly installed Program Folder Anaconda.

In my work flow, I usually only use the Ipython Notebook, but for some coding problems where I need access to the easy debugging capabilities of Spyder, I use it.  We will be using the Ipython Notebook interface (web browser) mostly in this class.

### Loading libraries

The python universe has a huge number of libraries that extend the capabilities of python. Nearly all of these are open source, unlike packages like stata or matlab where some key libraries are proprietary (and can cost lots of money).  In lots of my code, you will see this at the top:


```python
%matplotlib inline
##import sympy as sympy
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sbn
from scipy import *
```

This code sets up Ipython Notebook environments (lines beginning with `%`), and loads several libraries and functions.  The core scientific stack in python consists of a number of free libraries.  The ones I have loaded above include:

1. sympy: provides for symbolic computation (solving algebra problems)
2. numpy: provides for linear algebra computations
3. matplotlib.pyplot: provides for the ability to graph functions and draw figures
4. scipy: scientific python provides a plethora of capabilities
5. seaborn: makes matplotlib figures even pretties (another library like this is called bokeh).  This is entirely optional and is purely for eye candy.

### Creating arrays, scalars, and matrices in Python

Scalars can be created easily like this:


```python
x = .5
print x
```

    0.5


#### Vectors and Lists

The numpy library (we will reference it by np) is the workhorse library for linear algebra in python.  To creat a vector simply surround a python list ($[1,2,3]$) with the np.array function:


```python
x_vector = np.array([1,2,3])
print x_vector
```

    [1 2 3]


We could have done this by defining a python list and converting it to an array:


```python
c_list = [1,2]
print "The list:",c_list
print "Has length:", len(c_list)

c_vector = np.array(c_list)
print "The vector:", c_vector
print "Has shape:",c_vector.shape
```

    The list: [1, 2]
    Has length: 2
    The vector: [1 2]
    Has shape: (2,)



```python
z = [5,6]
print "This is a list, not an array:",z
print type(z)
```

    This is a list, not an array: [5, 6]
    <type 'list'>



```python
zarray = np.array(z)
print "This is an array, not a list",zarray
print type(zarray)
```

    This is an array, not a list [5 6]
    <type 'numpy.ndarray'>


#### Matrices


```python
b = zip(z,c_vector)
print b
print "Note that the length of our zipped list is 2 not (2 by 2):",len(b)
```

    [(5, 1), (6, 2)]
    Note that the length of our zipped list is 2 not (2 by 2): 2



```python
print "But we can convert the list to a matrix like this:"
A = np.array(b)
print A
print type(A)
print "A has shape:",A.shape
```

    But we can convert the list to a matrix like this:
    [[5 1]
     [6 2]]
    <type 'numpy.ndarray'>
    A has shape: (2, 2)


## Matrix Addition and Subtraction

### Adding or subtracting a scalar value to a matrix

To learn the basics, consider a small matrix of dimension $2 \times 2$, where $2 \times 2$ denotes the number of rows $\times$ the number of columns.  Let $A$=$\bigl( \begin{smallmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{smallmatrix} \bigr)$.  Consider adding a scalar value (e.g. 3) to the A.
$$
\begin{equation}
	A+3=\begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} 	
	\end{bmatrix}+3
	=\begin{bmatrix}
	  a_{11}+3 & a_{12}+3 \\
	  a_{21}+3 & a_{22}+3 	
	\end{bmatrix}
\end{equation}
$$
The same basic principle holds true for A-3:
$$
\begin{equation}
	A-3=\begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} 	
	\end{bmatrix}-3
	=\begin{bmatrix}
	  a_{11}-3 & a_{12}-3 \\
	  a_{21}-3 & a_{22}-3 	
	\end{bmatrix}
\end{equation}
$$
Notice that we add (or subtract) the scalar value to each element in the matrix A.  A can be of any dimension.

This is trivial to implement, now that we have defined our matrix A:


```python
result = A + 3
#or
result = 3 + A
print result
```

    [[8 4]
     [9 5]]


### Adding or subtracting two matrices
Consider two small $2 \times 2$ matrices, where $2 \times 2$ denotes the \# of rows $\times$ the \# of columns.  Let $A$=$\bigl( \begin{smallmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{smallmatrix} \bigr)$ and $B$=$\bigl( \begin{smallmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{smallmatrix} \bigr)$.  To find the result of $A-B$, simply subtract each element of A with the corresponding element of B:

$$
\begin{equation}
	A -B =
	\begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} 	
	\end{bmatrix} -
	\begin{bmatrix} b_{11} & b_{12} \\
	  b_{21} & b_{22}
	\end{bmatrix}
	=
	\begin{bmatrix}
	  a_{11}-b_{11} & a_{12}-b_{12} \\
	  a_{21}-b_{21} & a_{22}-b_{22} 	
	\end{bmatrix}
\end{equation}
$$

Addition works exactly the same way:

$$
\begin{equation}
	A + B =
	\begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} 	
	\end{bmatrix} +
	\begin{bmatrix} b_{11} & b_{12} \\
	  b_{21} & b_{22}
	\end{bmatrix}
	=
	\begin{bmatrix}
	  a_{11}+b_{11} & a_{12}+b_{12} \\
	  a_{21}+b_{21} & a_{22}+b_{22} 	
	\end{bmatrix}
\end{equation}
$$

An important point to know about matrix addition and subtraction is that it is only defined when $A$ and $B$ are of the same size.  Here, both are $2 \times 2$.  Since operations are performed element by element, these two matrices must be conformable- and for addition and subtraction that means they must have the same numbers of rows and columns.  I like to be explicit about the dimensions of matrices for checking conformability as I write the equations, so write

$$
A_{2 \times 2} + B_{2 \times 2}= \begin{bmatrix}
  a_{11}+b_{11} & a_{12}+b_{12} \\
  a_{21}+b_{21} & a_{22}+b_{22} 	
\end{bmatrix}_{2 \times 2}
$$

Notice that the result of a matrix addition or subtraction operation is always of the same dimension as the two operands.

Let's define another matrix, B, that is also $2 \times 2$ and add it to A:


```python
B = np.random.randn(2,2)
print B
```

    [[ 2.5056974   0.37029763]
     [ 0.94461604 -0.23399752]]



```python
result = A + B
result
```




    array([[ 7.5056974 ,  1.37029763],
           [ 6.94461604,  1.76600248]])



##Matrix Multiplication

###Multiplying a scalar value times a matrix

As before, let $A$=$\bigl( \begin{smallmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{smallmatrix} \bigr)$.  Suppose we want to multiply A times a scalar value (e.g. $3 \times A$)

$$
\begin{equation}
	3 \times A = 3 \times \begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} 	
	\end{bmatrix}
	=
	\begin{bmatrix}
	  3a_{11} & 3a_{12} \\
	  3a_{21} & 3a_{22} 	
	\end{bmatrix}
\end{equation}
$$

is of dimension (2,2).  Scalar multiplication is commutative, so that $3 \times A$=$A \times 3$.  Notice that the product is defined for a matrix A of any dimension.

Similar to scalar addition and subtration, the code is simple:


```python
A * 3
```




    array([[15,  3],
           [18,  6]])



### Multiplying two matricies

Now, consider the $2 \times 1$ vector $C=\bigl( \begin{smallmatrix} c_{11} \\
  c_{21}
\end{smallmatrix} \bigr)$  

Consider multiplying matrix $A_{2 \times 2}$ and the vector $C_{2 \times 1}$.  Unlike the addition and subtraction case, this product is defined.  Here, conformability depends not on the row **and** column dimensions, but rather on the column dimensions of the first operand and the row dimensions of the second operand.  We can write this operation as follows

$$
\begin{equation}
	A_{2 \times 2} \times C_{2 \times 1} = 
	\begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} 	
	\end{bmatrix}_{2 \times 2}
    \times
    \begin{bmatrix}
	c_{11} \\
	c_{21}
	\end{bmatrix}_{2 \times 1}
	=
	\begin{bmatrix}
	  a_{11}c_{11} + a_{12}c_{21} \\
	  a_{21}c_{11} + a_{22}c_{21} 	
	\end{bmatrix}_{2 \times 1}
\end{equation}
$$

Alternatively, consider a matrix C of dimension $2 \times 3$ and a matrix A of dimension $3 \times 2$

$$
\begin{equation}
	A_{3 \times 2}=\begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} \\
	  a_{31} & a_{32} 	
	\end{bmatrix}_{3 \times 2}
	,
	C_{2 \times 3} = 
	\begin{bmatrix}
		  c_{11} & c_{12} & c_{13} \\
		  c_{21} & c_{22} & c_{23} \\
	\end{bmatrix}_{2 \times 3}
	\end{equation}
$$

Here, A $\times$ C is

$$
\begin{align}
	A_{3 \times 2} \times C_{2 \times 3}=&
	\begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} \\
	  a_{31} & a_{32} 	
	\end{bmatrix}_{3 \times 2}
	\times
	\begin{bmatrix}
	  c_{11} & c_{12} & c_{13} \\
	  c_{21} & c_{22} & c_{23} 
	\end{bmatrix}_{2 \times 3} \\
	=&
	\begin{bmatrix}
	  a_{11} c_{11}+a_{12} c_{21} & a_{11} c_{12}+a_{12} c_{22} & a_{11} c_{13}+a_{12} c_{23} \\
	  a_{21} c_{11}+a_{22} c_{21} & a_{21} c_{12}+a_{22} c_{22} & a_{21} c_{13}+a_{22} c_{23} \\
	  a_{31} c_{11}+a_{32} c_{21} & a_{31} c_{12}+a_{32} c_{22} & a_{31} c_{13}+a_{32} c_{23}
	\end{bmatrix}_{3 \times 3}	
\end{align}
$$

So in general, $X_{r_x \times c_x} \times Y_{r_y \times c_y}$ we have two important things to remember: 

* For conformability in matrix multiplication, $c_x=r_y$, or the columns in the first operand must be equal to the rows of the second operand.
* The result will be of dimension $r_x \times c_y$, or of dimensions equal to the rows of the first operand and columns equal to columns of the second operand.

Given these facts, you should convince yourself that matrix multiplication is not generally commutative, that the relationship $X \times Y = Y \times X$ does **not** hold in all cases.
For this reason, we will always be very explicit about whether we are pre multiplying ($X \times Y$) or post multiplying ($Y \times X$) the vectors/matrices $X$ and $Y$.

For more information on this topic, see this
http://en.wikipedia.org/wiki/Matrix_multiplication.


```python
# Let's redefine A and C to demonstrate matrix multiplication:
A = np.arange(6).reshape((3,2))
C = np.random.randn(2,2)

print A.shape
print C.shape
```

    (3, 2)
    (2, 2)


We will use the numpy dot operator to perform the these multiplications.  You can use it two ways to yield the same result:


```python
print A.dot(C)
print np.dot(A,C)
```

    [[ 0.48080757  0.43511698]
     [ 1.47915018  0.72999774]
     [ 2.47749278  1.0248785 ]]
    [[ 0.48080757  0.43511698]
     [ 1.47915018  0.72999774]
     [ 2.47749278  1.0248785 ]]


Suppose instead of pre-multiplying C by A, we post-multiply.  The product doesn't exist because we don't have conformability as described above:


```python
C.dot(A)
```

## Matrix Division
The term matrix division is actually a misnomer.  To divide in a matrix algebra world we first need to invert the matrix.  It is useful to consider the analog case in a scalar work.  Suppose we want to divide the $f$ by $g$.  We could do this in two different ways:
$$
\begin{equation}
	\frac{f}{g}=f \times g^{-1}.
\end{equation}
$$
In a scalar seeting, these are equivalent ways of solving the division problem.  The second one requires two steps: first we invert g and then we multiply f times g.  In a matrix world, we need to think about this second approach.  First we have to invert the matrix g and then we will need to pre or post multiply depending on the exact situation we encounter (this is intended to be vague for now).

###Inverting a Matrix

As before, consider the square $2 \times 2$ matrix $A$=$\bigl( \begin{smallmatrix} a_{11} & a_{12} \\ a_{21} & a_{22}\end{smallmatrix} \bigr)$.  Let the inverse of matrix A (denoted as $A^{-1}$) be 

$$
\begin{equation}
	A^{-1}=\begin{bmatrix}
             a_{11} & a_{12} \\
		     a_{21} & a_{22} 
           \end{bmatrix}^{-1}=\frac{1}{a_{11}a_{22}-a_{12}a_{21}}	\begin{bmatrix}
		             a_{22} & -a_{12} \\
				     -a_{21} & a_{11} 
		           \end{bmatrix}
\end{equation}
$$

The inverted matrix $A^{-1}$ has a useful property:
$$
\begin{equation}
	A \times A^{-1}=A^{-1} \times A=I
\end{equation}
$$
where I, the identity matrix (the matrix equivalent of the scalar value 1), is
$$
\begin{equation}
	I_{2 \times 2}=\begin{bmatrix}
             1 & 0 \\
		     0 & 1 
           \end{bmatrix}
\end{equation}
$$
furthermore, $A \times I = A$ and $I \times A = A$.

An important feature about matrix inversion is that it is undefined if (in the $2 \times 2$ case), $a_{11}a_{22}-a_{12}a_{21}=0$.  If this relationship is equal to zero the inverse of A does not exist.  If this term is very close to zero, an inverse may exist but $A^{-1}$ may be poorly conditioned meaning it is prone to rounding error and is likely not well identified computationally.  The term $a_{11}a_{22}-a_{12}a_{21}$ is the determinant of matrix A, and for square matrices of size greater than $2 \times 2$, if equal to zero indicates that you have a problem with your data matrix (columns are linearly dependent on other columns).  The inverse of matrix A exists if A is square and is of full rank (ie. the columns of A are not linear combinations of other columns of A).

For more information on this topic, see this
http://en.wikipedia.org/wiki/Matrix_inversion, for example, on inverting matrices.


```python
# note, we need a square matrix (# rows = # cols), use C:
C_inverse = np.linalg.inv(C)
print C_inverse
```

    [[ 2.97399042  1.966247  ]
     [-3.28628201  0.12551463]]


Check that $C\times C^{-1} = I$:


```python
print C.dot(C_inverse)
print "Is identical to:"
print C_inverse.dot(C)
```

    [[  1.00000000e+00  -4.61031414e-18]
     [ -6.43302442e-18   1.00000000e+00]]
    Is identical to:
    [[  1.00000000e+00   6.11198607e-17]
     [  6.54738800e-18   1.00000000e+00]]


## Transposing a Matrix

At times it is useful to pivot a matrix for conformability- that is in order to matrix divide or multiply, we need to switch the rows and column dimensions of matrices.  Consider the matrix
$$
\begin{equation}
	A_{3 \times 2}=\begin{bmatrix}
	  a_{11} & a_{12} \\
	  a_{21} & a_{22} \\
	  a_{31} & a_{32} 	
	\end{bmatrix}_{3 \times 2}	
\end{equation}
$$
The transpose of A (denoted as $A^{\prime}$) is
$$
\begin{equation}
   A^{\prime}=\begin{bmatrix}
	  a_{11} & a_{21} & a_{31} \\
	  a_{12} & a_{22} & a_{32} \\
	\end{bmatrix}_{2 \times 3}
\end{equation}
$$


```python
A = np.arange(6).reshape((3,2))
B = np.arange(8).reshape((2,4))
print "A is"
print A

print "The Transpose of A is"
print A.T
```

    A is
    [[0 1]
     [2 3]
     [4 5]]
    The Transpose of A is
    [[0 2 4]
     [1 3 5]]


One important property of transposing a matrix is the transpose of a product of two matrices.  Let matrix A be of dimension $N \times M$ and let B of of dimension $M \times P$.  Then
$$
\begin{equation}
	(AB)^{\prime}=B^{\prime}A^{\prime}
\end{equation}
$$
For more information, see this http://en.wikipedia.org/wiki/Matrix_transposition on matrix transposition.  This is also easy to implement:


```python
print B.T.dot(A.T)
print "Is identical to:"
print (A.dot(B)).T
```

    [[ 4 12 20]
     [ 5 17 29]
     [ 6 22 38]
     [ 7 27 47]]
    Is identical to:
    [[ 4 12 20]
     [ 5 17 29]
     [ 6 22 38]
     [ 7 27 47]]


## More python tools

### Indexing

Python begins indexing at 0 (not 1), therefore the first row and first column is referenced by 0,0 **not** 1,1.

### Slicing  

Accessing elements of numpy matrices and arrays. This code grabs the first column of A:


```python
print A
A[:,0]
```

    [[0 1]
     [2 3]
     [4 5]]





    array([0, 2, 4])



or, we could grab a particular element (in this case, the second column, last row):


```python
A[2,1]
```




    5



### Logical Checks to extract values from matrices/arrays:


```python
print A
```

    [[0 1]
     [2 3]
     [4 5]]



```python
print A[:,1]>4

A[A[:,1]>4]
```

    [False False  True]





    array([[4, 5]])



### For loops

Create a $12 \times 2$ matrix and print it out:


```python
A = np.arange(24).reshape((12,2))
print A
print A.shape
```

    [[ 0  1]
     [ 2  3]
     [ 4  5]
     [ 6  7]
     [ 8  9]
     [10 11]
     [12 13]
     [14 15]
     [16 17]
     [18 19]
     [20 21]
     [22 23]]
    (12, 2)


The code below loops over the rows (12 of them) of our matrix A.  For each row, it slices A and prints the row values across all columns.  Notice the form of the for loop.  The colon defines the statement we are looping over.  For each iteration of the loop **idented** lines will be executed:


```python
for rows in A:
    print rows
```

    [0 1]
    [2 3]
    [4 5]
    [6 7]
    [8 9]
    [10 11]
    [12 13]
    [14 15]
    [16 17]
    [18 19]
    [20 21]
    [22 23]



```python
for rows in A:
    print rows
```

    [0 1]
    [2 3]
    [4 5]
    [6 7]
    [8 9]
    [10 11]
    [12 13]
    [14 15]
    [16 17]
    [18 19]
    [20 21]
    [22 23]



```python
for cols in A.T:
    print cols
```

    [ 0  2  4  6  8 10 12 14 16 18 20 22]
    [ 1  3  5  7  9 11 13 15 17 19 21 23]


### If/then/else

The code below checks the value of x and categorizes it into one of three values.  Like the for loop, each logical if check is ended with a colon, and any commands to be applied to that particular if check (if true) must be indented.


```python
x=.4

if x<.5:
    print "Heads"
    print 100
elif x>.5:
    print "Tails"
    print 0
else:
    print "Tie"
    print 50
```

    Heads
    100


### While loops

Again, we have the same basic form for the statement (note the colons and indents).  Here we use the shorthand notation `x+=1` for performing the calculation `x = x + 1`:


```python
x=0
while x<10:
    x+=1 
    print x<10

print x
```

    True
    True
    True
    True
    True
    True
    True
    True
    True
    False
    10


# Some more


```python
v = np.random.beta(56, 23, 100)
```


```python
plt.hist(v)
```


```python
np.savetxt("myvextor.txt", v.reshape(20,5))
```


```sh
%%sh
head -10 myvextor.txt
```

    7.248008818950451015e-01 6.532244850357984411e-01 6.536199234633465194e-01 7.334928646986760281e-01 5.398165955580682684e-01
    7.139216071922678264e-01 6.728443837682190898e-01 6.494566164553140508e-01 6.683509000127559885e-01 7.644556371656708871e-01
    7.215153654452582943e-01 6.911006139808743010e-01 6.734786066554359074e-01 7.319844378166059373e-01 6.986996622331494988e-01
    6.978137082576147954e-01 6.825797326701168455e-01 7.569021032715560482e-01 6.154717554965444259e-01 7.482810964973004575e-01
    6.908519676137282461e-01 7.373778586062508245e-01 6.414907513325304178e-01 7.671835956420999247e-01 6.534701870485098985e-01
    6.679163640763395859e-01 7.410336013997532723e-01 7.564381573540180925e-01 7.741644043396176400e-01 7.560739681550309177e-01
    6.611087863698059675e-01 6.944074001813811403e-01 6.107849058765042471e-01 7.698996963306875552e-01 7.171169314025702679e-01
    7.722202225663886699e-01 7.263728335966201932e-01 7.350093378174409331e-01 7.552882740562755215e-01 6.027107542833861631e-01
    6.953737452564848764e-01 7.343557137075048535e-01 7.700578612228643482e-01 7.458161986630932327e-01 7.763693151512383039e-01
    6.891582596380744219e-01 7.441501789777352771e-01 7.231777325956401103e-01 6.755128569529142979e-01 5.962737198081802248e-01



```python
w = np.loadtxt("myvextor.txt")
```


```python
(w*v).sum()/(w.)
```




    49.145946056843869




```python
a = v.reshape(10,10)
```


```python
type(a)
```




    numpy.ndarray




```python
np.matrix(a)
```




    matrix([[ 0.72480088,  0.65322449,  0.65361992,  0.73349286,  0.5398166 ,
              0.71392161,  0.67284438,  0.64945662,  0.6683509 ,  0.76445564],
            [ 0.72151537,  0.69110061,  0.67347861,  0.73198444,  0.69869966,
              0.69781371,  0.68257973,  0.7569021 ,  0.61547176,  0.7482811 ],
            [ 0.69085197,  0.73737786,  0.64149075,  0.7671836 ,  0.65347019,
              0.66791636,  0.7410336 ,  0.75643816,  0.7741644 ,  0.75607397],
            [ 0.66110879,  0.6944074 ,  0.61078491,  0.7698997 ,  0.71711693,
              0.77222022,  0.72637283,  0.73500934,  0.75528827,  0.60271075],
            [ 0.69537375,  0.73435571,  0.77005786,  0.7458162 ,  0.77636932,
              0.68915826,  0.74415018,  0.72317773,  0.67551286,  0.59627372],
            [ 0.67069764,  0.6087569 ,  0.71437864,  0.78013279,  0.62574326,
              0.64370709,  0.71247713,  0.67404966,  0.67993396,  0.66871352],
            [ 0.66840764,  0.69701147,  0.68111859,  0.72275825,  0.75494115,
              0.66595713,  0.71336278,  0.6192563 ,  0.70614289,  0.71749587],
            [ 0.72002589,  0.72817938,  0.70982128,  0.75395697,  0.72369636,
              0.67164351,  0.68503003,  0.67223346,  0.72685012,  0.68745557],
            [ 0.682259  ,  0.72198239,  0.77744477,  0.71248248,  0.73918039,
              0.75032694,  0.62653126,  0.67802253,  0.71520116,  0.62238738],
            [ 0.65828396,  0.61692964,  0.72643121,  0.79209095,  0.74067574,
              0.66903045,  0.69260704,  0.71129395,  0.6608246 ,  0.66332163]])




```python
v = 
type(v)
```




    numpy.ndarray




```python
v = np.matrix(np.random.rand(10,1))
w = np.matrix(np.random.rand(10,1))

```


```python
a = v.dot(w.transpose())
```


```python
a.shape
```




    (10, 10)




```python
type(a)
```




    numpy.matrixlib.defmatrix.matrix




```python
lag = np.linalg
```


```python

```




    matrix([[  1.29562817e+17,  -6.77337552e+16,  -1.06075205e+17,
               2.38896852e+16,  -2.35331525e+17,   8.74792833e+16,
              -1.06547937e+17,  -1.95293482e+16,  -4.33592894e+16,
               6.57006219e+16],
            [ -0.00000000e+00,  -0.00000000e+00,   8.00000000e+00,
              -1.47117588e+17,   1.44115188e+17,   3.12249574e+17,
              -1.15292150e+18,   9.00719925e+16,  -9.24714206e+16,
               7.20575940e+16],
            [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
               1.80143985e+16,   0.00000000e+00,  -4.50359963e+16,
               1.44115188e+17,  -9.00719925e+15,   8.09096783e+15,
               0.00000000e+00],
            [  6.00479950e+15,  -1.80143985e+16,  -1.20095990e+16,
               6.75539944e+15,  -1.20095990e+16,  -6.00479950e+15,
               9.60767921e+16,   1.50119988e+15,   6.94501266e+14,
               0.00000000e+00],
            [ -1.20095990e+16,  -0.00000000e+00,   6.00479950e+15,
               1.50119988e+15,   2.40191980e+16,   9.00719925e+15,
              -4.80383960e+16,   6.00479950e+15,  -6.31845975e+15,
              -0.00000000e+00],
            [ -6.00479950e+15,  -0.00000000e+00,   1.20095990e+16,
              -1.05083991e+16,  -2.40191980e+16,   1.80143985e+16,
              -9.60767921e+16,   3.00239975e+15,   2.66842553e+15,
              -0.00000000e+00],
            [  0.00000000e+00,   0.00000000e+00,  -1.80143985e+16,
               1.80143985e+16,   0.00000000e+00,  -4.50359963e+16,
               1.44115188e+17,  -9.00719925e+15,   1.61044132e+16,
               0.00000000e+00],
            [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
              -9.00719925e+15,   0.00000000e+00,   1.80143985e+16,
               0.00000000e+00,   0.00000000e+00,  -1.77888496e+15,
               0.00000000e+00],
            [ -6.00479950e+15,   3.60287970e+16,   1.20095990e+16,
              -1.65131986e+16,  -2.40191980e+16,   1.20095990e+16,
              -9.60767921e+16,   3.00239975e+15,   3.85207751e+14,
              -0.00000000e+00],
            [  6.00479950e+15,   0.00000000e+00,   2.40191980e+16,
               2.55203979e+16,   2.40191980e+16,  -4.80383960e+16,
               9.60767921e+16,  -2.10167983e+16,   8.13151685e+15,
              -3.60287970e+16]])




```python

```

0919795db7f5667f032b679b69750950be6f458b
ipynb
Jupyter Notebook
DataScienceProgramming/03-NumPy-and-Linear-Algebra/linear-algebra-python-basics.ipynb
cartermin/MSA8090
c8d95fb7c71682b2197a391995b76f6043a905cc
CC0-1.0
2017-08-21T23:23:59.000Z
2020-12-16T23:57:11.000Z
DataScienceProgramming/03-NumPy-and-Linear-Algebra/linear-algebra-python-basics.ipynb
cartermin/MSA8090
c8d95fb7c71682b2197a391995b76f6043a905cc
CC0-1.0
DataScienceProgramming/03-NumPy-and-Linear-Algebra/linear-algebra-python-basics.ipynb
cartermin/MSA8090
c8d95fb7c71682b2197a391995b76f6043a905cc
CC0-1.0
2017-08-22T00:39:01.000Z
2019-09-09T01:46:29.000Z
Qwen/Qwen-72B
1. YES
2. YES