Question

In: Computer Science

1 Linear Algebra in Numpy (1) Create a random 100-by-100 matrix M, using numpy method "np.random.randn(100,...

1 Linear Algebra in Numpy
(1) Create a random 100-by-100 matrix M, using numpy method "np.random.randn(100, 100)", where
each element is drawn from a random normal distribution.
(2) Calculate the mean and variance of all the elements in M;
(3) Use "for loop" to calculate the mean and variance of each row of M.
(4) Use matrix operation instead of "for loop" to calculate the mean of each row of M, hint: create a vector
of ones using np.ones(100, 1)?
(5) Calculate the inverse matrix M−1
(6) Verify that M−1M = MM−1 = I. Are the off-diagnoal elements exactly 0, why?

Solutions

Expert Solution

The code will be


# coding: utf-8

# In[10]:


import numpy as np
M=np.random.randn(100,100)


# In[31]:


mean=np.mean(M)
variance=np.var(M)


# In[32]:


means=[];#stores mean for each row
variances = []#stores variance for each row
for i in range(len(M)):
means.append(np.mean(M[i]))
variances.append(np.var(M[i]))


# In[16]:


means_with_ops=np.mean(M,axis=1)


# In[26]:


M1=np.linalg.inv(M)


# In[27]:


M1M=np.dot(M1,M)


# In[28]:


MM1=np.dot(M,M1)


# In[29]:


np.allclose(MM1,M1M,atol=1e-5)#to check whether the two matrix are same or not

The output and code snippet is

DO give a thumbs up and in case there are doubts leave a comment.


Related Solutions

Using Python: Normalize a 5x5 random matrix hints: use numpy create a random matrix X apply...
Using Python: Normalize a 5x5 random matrix hints: use numpy create a random matrix X apply Normalization: (X - Mean) / Deviation
Linear algebra Matrix
Let A ∈ Mn(R) such that I + A is invertible. Suppose that                                     B = (I − A)(I + A)-1(a) Show that B = (I + A)−1(I − A)(b) Show that I + B is invertible and express A in terms of B.
Linear algebra matrix
Exercise 13. Let A = (aij)n ∈ Mn(R) where aij = cos(i + j) for i, j = 1, 2, . . . ,n. Find rank(A).
Linear algebra Matrix
Exercise 11. Find the rank of matrix A where A, B and C
Linear algebra Matrix
excerses. Find the matrix X ∈ M2(R) satisfies the equation                 
Linear algebra matrix
Exercise 14. Find the inverse of each matrix (if exists) below: 
Linear algebra matrix
Exercise 15. Solve the system of linear equation unknow
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python Script (ie n=user input) Ask (user input ) and (randomly generated) row and column location Assign Q to (known row and column location ) and 0 to all others location Please show compile script working as well
Consider the general linear model ? = ?? + ?. Use matrix algebra to show that...
Consider the general linear model ? = ?? + ?. Use matrix algebra to show that ?̂ is an unbiased estimator of ?. the last ? has bar
Linear Algebra Carefully prove the following statement: Let A be an n×n matrix. Assume that there...
Linear Algebra Carefully prove the following statement: Let A be an n×n matrix. Assume that there exists an integer k ≥ 1 such that Ak = I . Prove that A is invertible.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT