The mathematical basis of machine learning (1)--common functions and distributions __ functions

Source: Internet
Author: User
Tags mathematical functions

Recently there is a mathematical foundation in the system, and next will share some of the most common mathematical functions in machine learning and distributed Python implementations. 1. Logarithmic function

Generally, functions Y=logax (a>0, and a≠1) are called logarithmic functions, that is, the power (true number) is the independent variable, the exponent is the dependent variable, the base is a constant function, called the logarithmic function. 1.1 Simple logarithmic functions

The implementation code looks like this:

Import Math
Import Matplotlib.pyplot as Plt

if __name__ = "__main__":
    x = [Float (i)/100 for I in range (1,300)]
    y = [Math.log (i) for i in X]
    plt.plot (x,y, ' R ', linewidth=2,label= ' Logcurve ')
    plt.legend (loc= ' upper left ') )
    Plt.grid (True)
    plt.show ()

The drawing results are shown in the following illustration:

1.2 Comparison of common logarithmic functions

The implementation code looks like this:

Import Math
Import numpy as NP
import Matplotlib.pyplot as Plt


if __name__ = "__main__":
    x = Np.arange (0. 05,3,0.01)
    #绘制log1.5 (x) curve
    y1 = [Math.log (a,1.5) for a in X]
    plt.plot (x,y1,linewidth=2,color= ' R ', label= ' log1.5 (x) ')
    y2 = [Math.log (a,2) for a in X]
    plt.plot (x,y2,linewidth=2,color= ' g ', label= ' log2 (x) ')
    y3 = [ Math.log (a,3) for a in X]
    plt.plot (x,y3,linewidth=2,color= ' b ', label= ' Log3 (x) ')
    plt.plot ([1,1],[y1[0],y1[ -1]], ' r--', linewidth=2
    #指定标签的位置
    plt.legend (loc= ' lower right ')
    Plt.grid (True)
    plt.show ()

The drawing results are shown in the following illustration:

2. Common distribution of functions

The common function distributions include 0-1 distribution , two items distribution , poisson distribution , uniform distribution , exponential distribution and normal distribution. Among them, the first three are discrete distribution , the latter three are continuous type distribution . 2.1 Two distribution

If the probability distribution of random variable X is: p{x=k}= (n!/k!) ( N-K)! P^k (1-p) ^ (n-k)
where k = 0,1,..., N (n is a positive integer greater than 0), and the range of P is 0-1, the X is said to obey the parameter of N, p two distribution, recorded as x~b (n,p).

The derivation of the two-item distribution shows that this kind of distribution can be used to describe the probability of event a occurring in N-heavy Bernoulli test . There are only two possible outcomes in each trial, and whether the two results are antagonistic to each other, and independent of each other test results, regardless of the probability of the occurrence of the event is not changed in each independent test, then this series of tests is known as the N-heavy Mr Yunus experiment, when the test time is 1 o'clock, two distribution Subject to 0-1 distribution .

The implementation code looks like this:

Import NumPy as NP
import Matplotlib.pyplot as Plt

if __name__ = "__main__":
    u = np.random.uniform ( 0.0,1.0,10000)
    plt.hist (u,100,facecolor= ' g ', alpha=0.75,label= ' binomial distribution ')
    plt.legend (loc= " Upper left ")
    Plt.grid (True)
    plt.show ()

The drawing results are shown in the following illustration:

2.2 Poisson Distribution

If the probability distribution of random variable X is: p{x=k}=λ^k/k! e^ (-λ),
where k = 0,1,..., N (n is a positive integer greater than 0), a constant of λ greater than 0 is called X to be the Poisson distribution of the parameter λ, and is recorded as x ~ P (λ).

The implementation code looks like this:

Import NumPy as NP
import Matplotlib.pyplot as Plt

if __name__ = "__main__":
    #设定λ为100
    x = Np.random.poisson (100,10000) times
    =

    plt.hist (x,bins=times,normed=true,range=[0,times],color= ' G '), Alpha=0.75,label= ' Poisson distribution ')
    plt.legend (loc= "upper left")
    Plt.grid (True)
    plt.show ()

The drawing results are shown in the following illustration:

2.3 Exponential distribution

If the probability distribution of the random variable x is P (x;λ) =λe^ (-λx), where the xϵ[0,∞],λ is a constant greater than 0, then the X is said to be the exponential distribution of the parameter λ and is recorded as x ~ E (λ).

The implementation code looks like this:

Import NumPy as NP
import Matplotlib.pyplot as Plt

if __name__ = "__main__":
    #通过赋予不同的λ值, compare the differences between different curves,
    LAM1 = 0.5
    x = Np.arange (0,20,0.01)
    y1 = lam1 * NP.EXP (-lam1*x)
    plt.plot (x,y1,color= ' R ', label= ' λ=0.5 ')

    lam2 = 0.3
    y2 = lam2 * NP.EXP (-lam2*x)
    plt.plot (x,y2,color= ' g ', label= ' λ=0.3 ')

    lam3 = 0.1 Y3
    = LAM3 * NP.EXP (-lam3*x)
    plt.plot (x,y3,color= ' b ', label= ' λ=0.1 ')

    plt.title (' expoential distribution ')
    plt.legend (loc= ' upper right ')
    Plt.grid (True)
    plt.show ()

The drawing results are shown in the following illustration:

2.4 Normal Distribution

Normal distribution is also called Gaussian distribution (Gaussian distribution) or normal distribution. The general assumption is that if the random variable x obeys a normal distribution with a mathematical expectation of μ and variance as σ^2, it is recorded as N (μ,σ^2). The expectation μ of the probability density function is the normal distribution, and its standard deviation σ determines the amplitude of distribution. When the μ= 0,σ= 1 o'clock normal distribution is the standard normal distribution.

The implementation code looks like this:

Import NumPy as NP
import Matplotlib.pyplot as Plt

if __name__ = "__main__":
    u = np.random.uniform ( 0.0,1.0,10000) times = 10000 to time in
    range (times):
        U + + np.random.uniform (0.0,1.0,10000)
    u/= Times
    plt.hist (u,100,facecolor= ' b ', alpha=0.75,label= ' Gaussian distribution ')
    plt.legend (loc= "upper left")
    Plt.grid (True)
    plt.show ()

The drawing results are shown in the following illustration:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.