Python Basics 2-Drawing

Source: Internet
Author: User
Tags cos sin

?

#!/usr/bin/env python#-*-coding:utf-8-*-# Created by xuehz on 2017/4/9import NumPy as Npimport matplotlib as Mplimport Matplotlib.pyplot as Pltfrom scipy.stats import norm, poissonimport timefrom scipy.optimize import leastsqfrom scipy impo RT Statsimport Scipy.optimize as Optimport matplotlib.pyplot as Pltfrom scipy.stats import norm, Poissonfrom Scipy.interpo Late import barycentricinterpolatorfrom scipy.interpolate import cubicsplinefrom scipy import Statsimport mathmpl.rcparams[' Font.sans-serif ' = [u ' simhei '] #FangSong/blackbody fangsong/kaitimpl.rcparams[' axes.unicode_minus '] = Falsedef f (x): y = np.ones_like (x) i = x > 0 Y[i] = np.power (X[i], x[i]) i = x < 0 Y[i] = np.power (-X [i],-x[i]) return ydef residual (t, x, y): Return y-(t[0] * x * * 2 + t[1] * x + t[2]) def residual2 (t, x, y): PR int t[0], t[1] return y-(T[0]*np.sin (t[1]*x) + t[2]) if __name__ = = ' __main__ ': #绘制正态分布概率密度函数 # mu = 0 # Sigm A = 1 # x = Np.linspace (mu-3 * Sigma, mu + 3 * Sigma, Wuyi) # y = Np.exp (-(X-MU) * * 2/(2 * sigma * * 2))/(MATH.SQRT (2 * math.pi) * sigma) # Print X.shape # print ' x = \ n ', x # print Y.shape # print ' y = \ n ', y # #plt. Plot (x, y, ' ro-', linewidth=2) # plt.figure (f Acecolor= ' W ') # Plt.plot (x, Y, ' R ', x, y, ' go ', linewidth=2, markersize=8) # Plt.xlabel (' x ', fontsize=15) # PLT. Ylabel (' Y ', fontsize=15) # plt.title (U ' Gaussian distribution function ', fontsize=18) # Plt.grid (True) # plt.show () #损失函数: Logistic loss (-1 , 1)/SVM hinge loss/0/1 loss # x = Np.array (Np.linspace (start=-2, stop=3, num=1001, Dtype=np.float)) # y_logit = Np.log (1 + np.exp (-X))/Math.log (2) # y_boost = Np.exp (-x) # y_01 = x < 0 # y_hinge = 1.0-x # Y_hinge[y_hinge &lt ; 0] = 0 # plt.plot (x, Y_logit, ' R ', label= ' Logistic Loss ', linewidth=2) # Plt.plot (x, y_01, ' G ', label= ' 0/1 Loss ', linewidth=2) # Plt.plot (x, Y_hinge, ' B ', label= ' hinge Loss ', linewidth=2) # Plt.plot (x, Y_boost, ' m--', label= ' Adab Oost Loss ', linewidth=2) # Plt.grid () # plt.legend (loc= ' upper Right ') # # Plt.savefig (' 1.png ') # plt.show () #x ^x # x = np.lins Pace ( -1.3, 1.3, 101) # y = f (x) # Plt.plot (x, Y, ' g ', label= ' x^x ', linewidth=2) # Plt.grid () # plt.legend (Loc = ' upper left ') # plt.show () # # Chest-line # x = np.arange (1, 0, -0.001) # y = ( -3 * x * np.log (x) + Np.exp (-(40 * ( X-1/NP.E) * * 4)/2 # Plt.figure (figsize= (5,7), facecolor= ' W ') # Plt.plot (y, x, ' R ', linewidth=2) # p    Lt.grid (True) # plt.title (U ' bust line ', fontsize=20) # # Plt.savefig (' breast.png ') # plt.show () # # # # Heart-shaped line # t = np.linspace (0, 2*np.pi, +) # x = * Np.sin (t) * * 3 # y = 5 * np.cos (t)-2 * NP.COS (* * T)-Np.cos (4*t) # Plt.plot (x, Y, ' R ', linewidth=2) # Plt.grid (True) # plt.show () # # # involute # t = NP.L InSpace (0, num=1000) # x = T*np.sin (t) + Np.cos (t) # y = Np.sin (t)-T*np.cos (t) # Plt.plot (x, Y, ' R ', linew idth=2) # Plt.grid ()   # plt.show () # # # Bar # x = Np.arange (0, ten, 0.1) # y = Np.sin (x) # Plt.bar (x, Y, width=0.04, linewidth    =0.2) # Plt.plot (x, y, ' r--', linewidth=2) # plt.title (U ' Sin curve ') # plt.xticks (rotation=-60) # Plt.xlabel (' x ') # Plt.ylabel (' Y ') # Plt.grid () # plt.show () # # # 6.  Probability distribution # # 6.1 Evenly distributed # x = Np.random.rand (10000) # t = Np.arange (len (x)) # #plt. hist (x, A, color= ' m ', alpha=0.5,     Label=u ' evenly distributed ') # Plt.plot (t, X, ' R ', Label=u ' evenly distributed ') # plt.legend (loc= ' upper left ') # Plt.grid () # plt.show ()  # 6.2 Verification Center limit theorem # t = # a = Np.zeros (10000) # for I in range (T): # A + = Np.random.uniform (-5, 5, 10000) # a/= t # plt.hist (A, bins=30, color= ' G ', alpha=0.5, normed=true, label=u ' evenly Distributed overlay ') # plt.legend (loc= ' upp  Er left ') # Plt.grid () # plt.show () # # #6.21 Central limit theorem for other distributions # LAMDA = ten # p = Stats.poisson (lamda) # y = P.rvs (size=1000) # mx = # r = (0, mx) # bins = r[1]-r[0] # PLT.figure (figsize= (8), facecolor= ' W ') # Plt.subplot (121) # plt.hist (y, Bins=bins, range=r, color= ' G ', alpha=0.8, normed=true) # t = Np.arange (0, mx+1) # Plt.plot (T, P.PMF (t), ' ro-', lw=2) # Plt.grid (True) # N = $ # M = 10000 # Plt.subplot (122) # a = Np.zeros (M, dtype=np.float) # p = Stats.poisson (LAMDA) # for I in Np.arange (N    ): # y = P.rvs (size=m) # A + = y # a/= N # plt.hist (A, bins=20, color= ' G ', alpha=0.8, Normed=true) # Plt.grid (b=true) # plt.show () # # 6.3 Poisson distribution # x = Np.random.poisson (lam=5, size=10000) # print x # PI Llar = # a = plt.hist (x, Bins=pillar, Normed=true, range=[0, Pillar], color= ' G ', alpha=0.5) # Plt.grid () # PLT . Show () # Print a # print a[0].sum () # # # 6.4 Use of the histogram # mu = 2 # sigma = 3 # data = mu + sigma * NP.R ANDOM.RANDN (+) # h = plt.hist (data, normed=1, color= ' #a0a0ff ') # x = h[1] # y = norm.pdf (x, Loc=mu, scale= Sigma) # Plt.plot (x,Y, ' r--', x, y, ' ro ', linewidth=2, markersize=4) # Plt.grid () # plt.show () # # 6.5 interpolation # RV = Poisson (5) # X 1 = a[1] # y1 = RV.PMF (x1) # ITP = Barycentricinterpolator (x1, y1) # Center of gravity interpolation # x2 = Np.linspace (X.min (), X.max (), 5 0) # y2 = ITP (x2) # cs = scipy.interpolate.CubicSpline (x1, y1) # three times spline interpolation # Plt.plot (x2, CS (x2), ' m--', linew   idth=5, label= ' Cubicspine ') # three cubic spline interpolation # plt.plot (x2, y2, ' G ', linewidth=3, label= ' Barycentricinterpolator ') # CG Interpolation # Plt.plot (x1, y1, ' R ', linewidth=1, label= ' actural value ') # Original value # plt.legend (loc= ' upper right  ') # Plt.grid () # plt.show () # 8.1 scipy #线性回归例1 x = Np.linspace ( -2, 2, A) A, B, C = 2, 3,-1 y = (A * x * * * 2 + B * x + C) + Np.random.rand (len (x)) *0.75 t = leastsq (residual, [0, 0, 0], args= (x, y)) theta = T[0] P  Rint ' true Value: ', A, B, C print ' predicted value: ', theta y_hat = theta[0] * x * * 2 + theta[1] * x + theta[2] Plt.plot (x, Y, ' R ', linewidth=2, Label=u ' AcTual ') plt.plot (x, Y_hat, ' G ', linewidth=2, Label=u ' Predict ') plt.legend (loc= ' upper left ') Plt.grid () Plt.sho W () # Linear Regression Example 2 x = Np.linspace (0, 5, +) A = 5 W = 1.5 phi =-2 y = A * Np.sin (w*x) + phi + Np.random.ran D (Len (x)) *0.5 t = leastsq (Residual2, [3, 5, 1], args= (x, y)) theta = t[0] print ' True value: ', A, W, phi print ' predicted value: ' , theta Y_hat = theta[0] * Np.sin (theta[1] * x) + theta[2] Plt.plot (x, Y, ' R ', linewidth=2, label= ' Actual ') plt.     Plot (x, Y_hat, ' G ', linewidth=2, label= ' Predict ') plt.legend (loc= ' lower left ') Plt.grid () plt.show () # marker   Description # "." Point # "," Pixel # "O" Circle # "V" Triangle_down # "^" Triangle_up # "<" Triangle_left #    ">" Triangle_right # "1" Tri_down # "2" TRI_UP # "3" Tri_left # "4" Tri_right # "8" octagon # "S" Square # "P" Pentagon # "*" Star # "H" Hexagon1 # "H" Hexagon2 # "+" plus # "X"  X # "D" Diamond # "D" Thin_diamond # "|"  VLine # "_" Hline # tickleft Tickleft # tickright Tickright # tickup Tickup # tickdown Tickdown # Caretleft Caretleft # caretright Caretright # caretup Caretup # Caretdown Caretdown

?

?

Python Basics 2-Drawing

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.