Python Machine learning classifier

Source: Internet
Author: User

Simple classifier
1 ImportNumPy as NP2 ImportMatplotlib.pyplot as MP3x =Np.array ([4[3, 1],5[2, 5],6[1, 8],7[6, 4],8[5, 2],9[3, 5],Ten[4, 7], One[4,-1]]) Ay = Np.array ([0, 1, 1, 0, 0, 1, 1, 0]) -  - #x[:,0] is a numpy array of a notation, for a two-dimensional array, take the two-dimensional array of all the data in the first dimension, the second dimension to take the No. 0 data, visually, x[:,0] is to take all rows of the No. 0 data, the #x[:,1] is the 1th data that takes all rows.  -L, r, h = x[:, 0].min ()-1, x[:, 0].max () + 1, 0.005 -B, T, V = x[:, 1].min ()-1, x[:, 1].max () + 1, 0.005 - #Meshgrid () generates a list of two array elements based on the two one-dimensional array parameters passed in +grid_x =Np.meshgrid (Np.arange (L, R, H), Np.arange (b, T, v)) - #do column Stitching + #Np.c_ is to connect two matrices by row, which is to add the two matrices left and right, requiring equal number of rows, similar to the merge in Pandas () Aflat_x = Np.c_[grid_x[0].ravel (), grid_x[1].ravel ()] atflat_y = Np.zeros (len (flat_x), dtype=int) -  -Flat_y[flat_x[:, 0] < flat_x[:, 1]] = 1 -Grid_y =Flat_y.reshape (Grid_x[0].shape) -  -  inMp.figure ('Simple classfication', facecolor='Lightgray') -Mp.title ('Simple classfication', fontsize=20) toMp.xlabel ('x', fontsize=14) +Mp.ylabel ('y', fontsize=14) -Mp.tick_params (labelsize=10) the #draw a grid with color *Mp.pcolormesh (Grid_x[0], grid_x[1], grid_y, cmap='Gray') $Mp.scatter (x[:, 0], x[:, 1], c=y, cmap='BRG', s=60)Panax NotoginsengMp.show ()
Simple Classification

Logical classification

J (k1,k2,b) = Sigma (-ylog (y ')-(1-y) log (1-y '))/M +m regular function (| | k1,k2,b| |) X regular intensity

Interface:

Sklearn.linear_model. Logisticregression (solver= ' liblinear ', c= regular intensity)

1 ImportNumPy as NP2 ImportSklearn.linear_model as LM3 ImportMatplotlib.pyplot as MP4x =Np.array ([5[4, 7],6[3.5, 8],7[3.1, 6.2],8[0.5, 1],9[1, 2],Ten[1.2, 1.9], One[6, 2], A[5.7, 1.5], -[5.4, 2.2]]) -y = Np.array ([0, 0, 0, 1, 1, 1, 2, 2, 2]) the #creating a logical classifier -Model = LM. Logisticregression (solver='Liblinear', -c=1000) - model.fit (x, y) +L, r, h = x[:, 0].min ()-1, x[:, 0].max () + 1, 0.005 -B, T, V = x[:, 1].min ()-1, x[:, 1].max () + 1, 0.005 +grid_x =Np.meshgrid (Np.arange (L, R, h), A Np.arange (b, T, v)) atflat_x = Np.c_[grid_x[0].ravel (), grid_x[1].ravel ()] -Flat_y =model.predict (flat_x) -Grid_y =Flat_y.reshape (Grid_x[0].shape) -Mp.figure ('Logistic Classification', -Facecolor='Lightgray') -Mp.title ('Logistic Classification', fontsize=20) inMp.xlabel ('x', fontsize=14) -Mp.ylabel ('y', fontsize=14) toMp.tick_params (labelsize=10) +Mp.pcolormesh (Grid_x[0], grid_x[1], grid_y, cmap='Gray') -Mp.scatter (x[:, 0], x[:, 1], c=y, cmap='BRG', s=60) theMp.show ()
Logic Classifier

Python Machine learning classifier

Related Article

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.