0% found this document useful (0 votes)
83 views

SciPy Cheat Sheet

The document discusses linear algebra functions available in SciPy. It provides examples of how to create and manipulate matrices using NumPy and SciPy, and perform common linear algebra operations like addition, multiplication, inverse, determinant, and solving linear systems. Key matrix and linear algebra routines in SciPy include matrix exponentials, logarithms, trigonometric functions, and generalized inverses. SciPy also supports creating and operating on sparse matrices for large, sparse data sets.

Uploaded by

cuneyt noksan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

SciPy Cheat Sheet

The document discusses linear algebra functions available in SciPy. It provides examples of how to create and manipulate matrices using NumPy and SciPy, and perform common linear algebra operations like addition, multiplication, inverse, determinant, and solving linear systems. Key matrix and linear algebra routines in SciPy include matrix exponentials, logarithms, trigonometric functions, and generalized inverses. SciPy also supports creating and operating on sparse matrices for large, sparse data sets.

Uploaded by

cuneyt noksan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

> Linear Algebra Also see NumPy

Python For Data Science


You’ll use the linalg and sparse modules.
Matrix Functions
Note that scipy.linalg contains and expands on numpy.linalg.
Addition
>>> from scipy import linalg, sparse
SciPy Cheat Sheet >>> np.add(A,D) #Addition
Subtraction
Creating Matrices
Learn SciPy online at www.DataCamp.com >>> np.subtract(A,D) #Subtraction

>>> A = np.matrix(np.random.random((2,2)))
vs
Di i ion

>>> B = np.asmatrix(b)
>>> np.divide(A,D) #Division
>>> C = np.mat(np.random.random((10,5)))

>>> D = np.mat([[3,4], [5,6]]) Multiplication


>>> np.multiply(D,A) #Multiplication

>>> np.dot(A,D) #Dot product

SciPy Basic Matrix Routines >>> np.vdot(A,D) #Vector dot product

>>> np.inner(A,D) #Inner product

Inverse >>> np.outer(A,D) #Outer product

The SciPy library is one of the core packages for


>>> np.tensordot(A,D) #Tensor dot product

>>> A.I #Inverse


>>> np.kron(A,D) #Kronecker product
scientific computing that provides mathematical
>>> linalg.inv(A) #Inverse

algorithms and convenience functions built on the


>>> A.T #Tranpose matrix
Exponential Functions
>>> A.H #Conjugate transposition
>>> linalg.expm(A) #Matrix exponential

NumPy extension of Python. >>> np.trace(A) #Trace >>> linalg.expm2(A) #Matrix exponential (Taylor Series)

Norm >>> linalg.expm3(D) #Matrix exponential (eigenvalue decomposition)

>>> linalg.norm(A) #Frobenius norm


Logarithm Function
Also see NumPy >>> linalg.norm(A,1) #L1 norm (max column sum)
>>> linalg.logm(A) #Matrix logarithm
> Interacting With NumPy >>> linalg.norm(A,np.inf) #L inf norm (max row sum)
Trigonometric Functions
Rank >>> linalg.sinm(D) Matrix sine

>>> import numpy as np

>>> a = np.array([1,2,3])
>>> np.linalg.matrix_rank(C) #Matrix rank >>> linalg.cosm(D) Matrix cosine

>>> b = np.array([(1+5j,2j,3j), (4j,5j,6j)])


>>> linalg.tanm(A) Matrix tangent
Deter minant
>>> c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]]) Hyperbolic Trigonometric Functions
>>> linalg.det(A) #Determinant
>>> linalg.sinhm(D) #Hypberbolic matrix sine

So lving linear problems


Index Tricks >>> linalg.coshm(D) #Hyperbolic matrix cosine

>>> linalg.solve(A,b) #Solver for dense matrices


>>> linalg.tanhm(A) #Hyperbolic matrix tangent
>>> E = np.mat(a).T #Solver for dense matrices

>>> np.mgrid[0:5,0:5] #Create a dense meshgrid


>>> linalg.lstsq(D,E) #Least-squares solution to linear matrix equation Matrix Sign Function
>>> np.ogrid[0:2,0:2] #Create an open meshgrid
>>> np.sigm(A) #Matrix sign function
>>> np.r_[[3,[0]*5,-1:1:10j] #Stack arrays vertically (row-wise)
Generalized inverse
>>> np.c_[b,c] #Create stacked column-wise arrays >>> linalg.pinv(C) #Compute the pseudo-inverse of a matrix (least-squares solver)
Matrix Square Root
>>> linalg.pinv2(C) #Compute the pseudo-inverse of a matrix (SVD) >>> linalg.sqrtm(A) #Matrix square root
Arbitrary Functions
Shape Manipulation
Creating Sparse Matrices >>> linalg.funm(A, lambda x: x*x) #Evaluate matrix function
>>> np.transpose(b) #Permute array dimensions

>>> b.flatten() #Flatten the array


>>> F = np.eye(3, k=1) #Create a 2X2 identity matrix

Decompositions
>>> np.hstack((b,c)) #Stack arrays horizontally (column-wise)
>>> G = np.mat(np.identity(2)) #Create a 2x2 identity matrix

>>> np.vstack((a,b)) #Stack arrays vertically (row-wise)


>>> C[C > 0.5] = 0

>>> np.hsplit(c,2) #Split the array horizontally at the 2nd index


>>> H = sparse.csr_matrix(C) #Compressed Sparse Row matrix
Eigenvalues and Eigenvectors
>>> np.vpslit(d,2) #Split the array vertically at the 2nd index >>> I = sparse.csc_matrix(D) #Compressed Sparse Column matrix
>>> la, v = linalg.eig(A) #Solve ordinary or generalized eigenvalue problem for square matrix

>>> J = sparse.dok_matrix(A) #Dictionary Of Keys matrix


>>> l1, l2 = la #Unpack eigenvalues

>>> E.todense() #Sparse matrix to full matrix


>>> v[:,0] #First eigenvector

Polynomials >>> sparse.isspmatrix_csc(A) #Identify sparse matrix >>> v[:,1] #Second eigenvector

>>> linalg.eigvals(A) #Unpack eigenvalues


>>> from numpy import poly1d
gular Value Decomposition
>>> p = poly1d([3,4,5]) #Create a polynomial object Sparse Matrix Routines Sin

>>> U,s,Vh = linalg.svd(B) #Singular Value Decomposition (SVD)

Inverse >>> M,N = B.shape

Vectorizing Functions >>> sparse.linalg.inv(I) #Inverse >>> Sig = linalg.diagsvd(s,M,N) #Construct sigma matrix in SVD

Norm LU Decomposition
>>> def myfunc(a): if a < 0:
>>> P,L,U = linalg.lu(C) #LU Decomposition
return a*2
>>> sparse.linalg.norm(I) #Norm
else:
Solving linear problems
return a/2

>>> sparse.linalg.spsolve(H,I) #Solver for sparse matrices


>>> np.vectorize(myfunc) #Vectorize functions

Sparse Matrix Functions


Type Handling

>>> sparse.linalg.expm(I) #Sparse matrix exponential


>>> np.real(c) #Return the real part of the array elements

>>> np.imag(c) #Return the imaginary part of the array elements

>>> np.real_if_close(c,tol=1000) #Return a real array if complex parts close to 0

Sparse Matrix Decompositions


>>> np.cast['f'](np.pi) #Cast object to a data type

>>> la, v = sparse.linalg.eigs(F,1) #Eigenvalues and eigenvectors

Other Useful Functions >>> sparse.linalg.svds(H, 2) #SVD

>>> np.angle(b,deg=True) #Return the angle of the complex argument

>>>
>>>
>>>
g = np.linspace(0,np.pi,num=5) #Create an array of evenly spaced values(number of samples)

g [3:] += np.pi

np.unwrap(g) #Unwrap

> A sking For Help Learn Data S ill k s Online at


www.DataCamp.com
>>> np.logspace(0,10,3) #Create an array of evenly spaced values (log scale)

>>> np.select([c<4],[c*2]) #Return values from a list of arrays depending on conditions


>>> help(scipy.linalg.diagsvd)

>>> misc.factorial(a) #Factorial


>>> np.info(np.matrix)
>>> misc.comb(10,3,exact=True) #Combine N things taken at k time

>>> misc.central_diff_weights(3) #Weights for Np-point central derivative

>>> misc.derivative(myfunc,1.0) #Find the n-th derivative of a function at a point

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy