金沙官网线上:LaTex in Markdown

在线预览:http://github.lesschina.com/python/ai/math/LaTex in Markdown.html

(2b) Combining ndarray objects

np.hstack(), which allows you to combine arrays column-wise,
np.vstack(), which allows you to combine arrays row-wise.
Note that both np.hstack() and np.vstack() take in a tuple of arrays as their first argument.
To horizontally combine three arrays a, b, and c, you would run np.hstack((a, b, c)).

 

matrix math on NumPy matrices using *

1.样式系列

Part 1: NumPy

2.2.附录:数学公式大全

数学公式 LaTex公式
$displaystylesumlimits_{i=0}^n i^3$ $displaystylesumlimits_{i=0}^n i^3$
$left(begin{array}{c}a\ bend{array}right)$ $left(begin{array}{c}a\ bend{array}right)$
$left(frac{a^2}{b^3}right)$ $left(frac{a^2}{b^3}right)$
$left.frac{a^3}{3}rightlvert_0^1$ $left.frac{a^3}{3}rightlvert_0^1$
$begin{bmatrix}a & b \c & d end{bmatrix}$ $begin{bmatrix}a & b \c & d end{bmatrix}$
$begin{cases}a & x = 0\b & x > 0end{cases}$ $begin{cases}a & x = 0\b & x > 0end{cases}$
$sqrt{frac{n}{n-1} S}$ $sqrt{frac{n}{n-1} S}$
$begin{pmatrix} alpha& beta^{*}\ gamma^{*}& delta end{pmatrix}$ $begin{pmatrix} alpha& beta^{*}\ gamma^{*}& delta end{pmatrix}$
$A:xleftarrow{n+mu-1}:B$ $A:xleftarrow{n+mu-1}:B$
$B:xrightarrow[T]{npm i-1}:C$ $B:xrightarrow[T]{npm i-1}:C$
$frac{1}{k}log_2 c(f);$ $frac{1}{k}log_2 c(f);$
$iintlimits_A f(x,y);$ $iintlimits_A f(x,y);$
$x^n + y^n = z^n$ $x^n + y^n = z^n$
$E=mc^2$ $E=mc^2$
$e^{pi i} - 1 = 0$ $e^{pi i} - 1 = 0$
$p(x) = 3x^6$ $p(x) = 3x^6$
$3x + y = 12$ $3x + y = 12$
$int_0^infty mathrm{e}^{-x},mathrm{d}x$ $int_0^infty mathrm{e}^{-x},mathrm{d}x$
$sqrt[n]{1+x+x^2+ldots}$ $sqrt[n]{1+x+x^2+ldots}$
$binom{x}{y} = frac{x!}{y!(x-y)!}$ $binom{x}{y} = frac{x!}{y!(x-y)!}$
$frac{frac{1}{x}+frac{1}{y}}{y-z}$ $frac{frac{1}{x}+frac{1}{y}}{y-z}$
$f(x)=frac{P(x)}{Q(x)}$ $f(x)=frac{P(x)}{Q(x)}$
$frac{1+frac{a}{b}}{1+frac{1}{1+frac{1}{a}}}$ $frac{1+frac{a}{b}}{1+frac{1}{1+frac{1}{a}}}$
$sum_{substack{0le ile m\ 0lt jlt n}} P(i,j)$ $sum_{substack{0le ile m\ 0lt jlt n}} P(i,j)$
$lim_{x to infty} exp(-x) = 0$ $lim_{x to infty} exp(-x) = 0$
$cos (2theta) = cos^2 theta - sin^2 theta$ $cos (2theta) = cos^2 theta - sin^2 theta$

 

Note that DenseVector stores all values as np.float64

DenseVector objects exist locally and are not inherently distributed. DenseVector objects can be used in the distributed setting by either passing functions that contain them to resilient distributed dataset (RDD) transformations or by distributing them directly as RDDs.

from pyspark.mllib.linalg import DenseVector

numpyVector = np.array([-3, -4, 5])
print 'nnumpyVector:n{0}'.format(numpyVector)

# Create a DenseVector consisting of the values [3.0, 4.0, 5.0]
myDenseVector = DenseVector([3,4,5])
# Calculate the dot product between the two vectors.
denseDotProduct = DenseVector.dot(myDenseVector,numpyVector)

print 'myDenseVector:n{0}'.format(myDenseVector)
print 'ndenseDotProduct:n{0}'.format(denseDotProduct)

numpyVector:
[-3 -4 5]
myDenseVector:
[3.0,4.0,5.0]
denseDotProduct:
0.0


2.1.常用表达式

常用数学 LaTex公式
$sqrt{ab}$ $sqrt{ab}$
$sqrt[n]{ab}$ $sqrt[n]{ab}$
$log_{a}{b}$ $log_{a}{b}$
$lg{ab}$ $lg{ab}$
$a^{b}$ $a^{b}$
$a_{b}$ $a_{b}$
$x_a^b$ $x_a^b$
$int$ $int$
$int_{a}^{b}$ $int_{a}^{b}$
$oint$ $oint$
$oint_a^b$ $oint_a^b$
$sum$ $sum$
$sum_a^b$ $sum_a^b$
$coprod$ $coprod$
$coprod_a^b$ $coprod_a^b$
$prod$ $prod$
$prod_a^b$ $prod_a^b$
$bigcap$ $bigcap$
$bigcap_a^b$ $bigcap_a^b$
$bigcup$ $bigcup$
$bigcup_a^b$ $bigcup_a^b$
$bigsqcup$ $bigsqcup$
$bigsqcup_a^b$ $bigsqcup_a^b$
$bigvee$ $bigvee$
$bigvee_a^b$ $bigvee_a^b$
$bigwedge$ $bigwedge$
$bigwedge_a^b$ $bigwedge_a^b$
$widetilde{ab}$ $widetilde{ab}$
$widehat{ab}$ $widehat{ab}$
$overleftarrow{ab}$ $overleftarrow{ab}$
$overrightarrow{ab}$ $overrightarrow{ab}$
$overbrace{ab}$ $overbrace{ab}$
$underbrace{ab}$ $underbrace{ab}$
$underline{ab}$ $underline{ab}$
$overline{ab}$ $overline{ab}$
$frac{ab}{cd}$ $frac{ab}{cd}$
$frac{partial a}{partial b}$ $frac{partial a}{partial b}$
$frac{text{d}x}{text{d}y}$ $frac{text{d}x}{text{d}y}$
$lim_{a rightarrow b}$ $lim_{a rightarrow b}$

 

Lambda 是匿名函数

一些链接: Lambda Functions, Lambda Tutorial, and Python Functions.

# Example function
def addS(x):
    return x + 's'
#lambda 形式
addSLambda = lambda x: x + 's'

# 乘法
multiplyByTen = lambda x: x * 10
print multiplyByTen(5)

#lambda fewer steps than def 
# The first function should add two values, while the second function should subtract the second  value from the first value.
def plus(x, y):
    return x + y

def minus(x, y):
    return x - y

functions = [plus, minus]
print functions[0](4, 5)
print functions[1](4, 5)

# lambda
lambdaFunctions = [lambda x,y : x+y ,  lambda x,y: x-y]
print lambdaFunctions[0](4, 5)
print lambdaFunctions[1](4, 5)

Lambda expressions consist of a single expression statement and cannot contain other simple statements. In short, this means that the lambda expression needs to evaluate to a value and exist on a single logical line. If more complex logic is necessary, use def in place of lambda.
Expression statements evaluate to a value (sometimes that value is None). Lambda expressions automatically return the value of their expression statement. In fact, a return statement in a lambda would raise a SyntaxError.
The following Python keywords refer to simple statements that cannot be used in a lambda expression: assert, pass, del, print, return, yield, raise, break金沙官网线上,, continue, import, global, and exec. Also, note that assignment statements (=) and augmented assignment statements (e.g. +=) cannot be used either.

1.4.大小

$tiny 萌萌哒$

$scriptsize 萌萌哒$

$small 萌萌哒$

$normalsize 萌萌哒(正常)$

$large 萌萌哒$

$Large 萌萌哒$

$huge 萌萌哒$

$Huge 萌萌哒$

$tiny 萌萌哒$

$scriptsize 萌萌哒$

$small 萌萌哒$

$normalsize 萌萌哒(正常)$

$large 萌萌哒$

$Large 萌萌哒$

$huge 萌萌哒$

$Huge 萌萌哒$

如果是单行写,记得加换行符号:

$tiny 萌萌哒\$
$scriptsize 萌萌哒\$
$small 萌萌哒\$
$normalsize 萌萌哒(正常)\$
$large 萌萌哒\$
$Large 萌萌哒\$
$huge 萌萌哒\$
$Huge 萌萌哒\$

转置矩阵 transpose a matrix by calling numpy.matrix.transpose() or by using .T on the matrix object (e.g. myMatrix.T).

Transposing a matrix produces a matrix where the new rows are the columns from the old matrix. For example: $$ begin{bmatrix} 1 & 2 & 3 \ 4 & 5 & 6 end{bmatrix}^mathbf{top} = begin{bmatrix} 1 & 4 \ 2 & 5 \ 3 & 6 end{bmatrix} $$

1.1.换行\、空格:

$换行\萌萌哒:小明$

$换行\萌萌哒:小明$


(1c) 矩阵计算 Matrix math

1.5.颜色(有些编辑器不支持)

${color[RGB]{255,0,0} Red}\$
${color[RGB]{30,144,255} Dodg Blue}\$
${color[RGB]{0,255,255} Aqua}\$
${color[RGB]{255,165,0} Orange}\$
${color[RGB]{255,69,0} Orange red}\$
${color[RGB]{0,128,0} Green}\$
${color[RGB]{128,128,128} Gray}\$
${color[RGB]{255,0,255} Magenta}\$
${color[RGB]{128,0,128} Purple}\$
${color[RGB]{184,134,11} Dark Gold}$

${color[RGB]{255,69,0} Orange red}$

 

dot product is equivalent to performing element-wise multiplication and then summing the result。

$ w cdot x$ 也可以表示为 $ w^top x $

$$ w cdot x = sum_{i=1}^n w_i x_i $$

Element-wise multiplication use the ***** operator to multiply two ndarray objects of the same length.
Dot product you can use either np.dot() or np.ndarray.dot()

# Create a ndarray based on a range and step size.
u = np.arange(0, 5, .5)
v = np.arange(5, 10, .5)

elementWise = u * v 
dotProduct = np.dot(u,v)

print 'u: {0}'.format(u)
print 'v: {0}'.format(v)
print 'nelementWisen{0}'.format(elementWise)
print 'ndotProductn{0}'.format(dotProduct)

----
#result
u: [ 0.   0.5  1.   1.5  2.   2.5  3.   3.5  4.   4.5]
v: [ 5.   5.5  6.   6.5  7.   7.5  8.   8.5  9.   9.5]

elementWise
[  0.     2.75   6.     9.75  14.    18.75  24.    29.75  36.    42.75]

dotProduct
183.75

3.4.函数公式表

函数 公式 函数 公式 函数 公式
$sin$ $sin$ $sin^{-1}$ $sin^{-1}$ $inf$ $inf$
$cos$ $cos$ $cos^{-1}$ $cos^{-1}$ $arg$ $arg$
$tan$ $tan$ $tan^{-1}$ $tan^{-1}$ $det$ $det$
$sinh$ $sinh$ $sinh^{-1}$ $sinh^{-1}$ $dim$ $dim$
$cosh$ $cosh$ $cosh^{-1}$ $cosh^{-1}$ $gcd$ $gcd$
$tanh$ $tanh$ $tanh^{-1}$ $tanh^{-1}$ $hom$ $hom$
$csc$ $csc$ $exp$ $exp$ $ker$ $ker$
$sec$ $sec$ $lg$ $lg$ $Pr$ $Pr$
$cot$ $cot$ $ln$ $ln$ $sup$ $sup$
$coth$ $coth$ $log$ $log$ $deg$ $deg$
$hom$ $hom$ $log_{e}$ $log_{e}$ $injlim$ $injlim$
$arcsin$ $arcsin$ $log_{10}$ $log_{10}$ $varinjlim$ $varinjlim$
$arccos$ $arccos$ $lim$ $lim$ $varprojlim$ $varprojlim$
$det$ $det$ $liminf$ $liminf$ $varliminf$ $varliminf$
$arctan$ $arctan$ $limsup$ $limsup$ $projlim$ $projlim$
$textrm{arccsc}$ $textrm{arccsc}$ $max$ $max$ $varlimsup$ $varlimsup$
$textrm{arcsec}$ $textrm{arcsec}$ $min$ $min$
$textrm{arccot}$ $textrm{arccot}$ $infty$ $infty$

(2a) Slices

features = np.array([1, 2, 3, 4])
print 'features:n{0}'.format(features)

# The first three elements of features
firstThree = features[0:3]

# The last three elements of features
lastThree = features[-3:]

3.1.集合系列

运算符 公式 运算符 公式 运算符 公式
$emptyset$ $emptyset$ $in$ $in$ $notin$ $notin$
$subset$ $subset$ $supset$ $supset$ $subseteq$ $subseteq$
$nsubseteq$ $nsubseteq$ $nsupseteq$ $nsupseteq$ $nsubseteqq$ $nsubseteqq$
$nsupseteqq$ $nsupseteqq$ $subsetneq$ $subsetneq$ $supsetneq$ $supsetneq$
$subsetneqq$ $subsetneqq$ $supsetneqq$ $supsetneqq$ $varsubsetneq$ $varsubsetneq$
$varsupsetneq$ $varsupsetneq$ $varsubsetneqq$ $varsubsetneqq$ $varsupsetneqq$ $varsupsetneqq$
$bigcap$ $bigcap$ $bigcup$ $bigcup$ $bigvee$ $bigvee$
$bigwedge$ $bigwedge$ $biguplus$ $biguplus$ $bigsqcup$ $bigsqcup$
$Subset$ $Subset$ $Supset$ $Supset$ $subseteqq$ $subseteqq$
$supseteqq$ $supseteqq$ $sqsubset$ $sqsubset$ $sqsupset$ $sqsupset$

Python 基础

1.2.居中$$**$$

$$萌萌哒$$

$$萌萌哒$$


(1a) 标量相乘 Scalar multiplication

$ a $ is the scalar (constant) and $ mathbf{v} $ is the vector
$$ a mathbf{v} = begin{bmatrix} a v_1 \ a v_2 \ vdots \ a v_n end{bmatrix} $$

# Create a numpy array with the values 1, 2, 3
simpleArray = np.array([1,2,3])
# Perform the scalar product of 5 and the numpy array
timesFive = simpleArray * 5
print simpleArray
print timesFive
-----
#result
[1 2 3]
[5 10 15

3.数学符号

(2c) PySpark's DenseVector

本文由金沙官网线上发布于操作系统,转载请注明出处:金沙官网线上:LaTex in Markdown

您可能还会对下面的文章感兴趣: