Scikit-learn study Diary (1)

Source: Internet
Author: User
Tags svm

Recently began to learn to use Scikit-learn, every day to write about what you learned, not only can remind yourself what to learn a day, but also convenient to review.

Install Scikit-learn on my virtual machine Ubuntu, the installation process is simple. Since I often use Python, my Ubuntu virtual machine has already installed Pip, NumPy, scipy,matplotlib, Cython dependent libraries.

Source Address: Https://github.com/scikit-learn/scikit-learn

After installing the dependent library, download Scikit-learn source on GitHub, CD to source directory python setup.py install. Then enter the Python interface, import Sklearn without an error that the installation is complete.

First experience the use of Scikit-learn to do SVM classification, it is recommended to use Scikit-learn before the machine learning methods have a basic understanding, so as to change the parameters to improve the training model.

 1 from sklearn import datasets< Span data-mce-= "" >2 from sklearn import SVM 3 iris=datasets.load_iris () 4 digits= Datasets.load_digits () 5 CLF=SVM. SVC (gamma=0.001,c=100.) #c is the error term for penalty parameters, gamma for learning rate 6 Clf.fit (digits.data[:-1), DIGITS.TARGET[:-1]) # training SVM model 7 clf.predict (digits.data[: -1]) # use SVM model to predict         

Iris and digits are Scikit-learn data sets, Digits.data is an image feature of the handwritten numeral 0-9, Digits.target is the number 0-9, and the last trained SVM model uses handwritten numerals to predict the numbers represented by the image features. Model training may not be used more than once, parameters can be used to make multiple predictions, so after training the model needs to record parameters for future predictions, there are two ways to record parameters, the first is the string storage parameters:

  1 from sklearn import Svm 2 from sklearn import datasets  3 Clf=SVM. SVC ()  4 Iris=datasets.load_iris ()  5 x,y= iris.data,iris.target 6 clf.fit (x, y)  7 import Pickle 8 S=pickle.dumps (CLF) " Span data-mce-= "" > 9 Clf2=pickle.loads (s) 10 clf2.predict (x[0:1 11 print Y[0]          

The CLF parameters are stored in S, Clf2 loaded s after the CLF parameters are obtained, using CLF2 and CLF to predict the effect is the same. The second method is to use the file storage parameters:

Import joblib2 joblib.dump (CLF,'filename.pkl')3 clf=joblib.load ('  Filename.pkl')         

The data in the array that is generally entered in Scikit-learn is required to be float64 bit, and if not, it should be converted to float64 using the following procedure.

1ImportNumPy as NP2From SklearnImport Random_projection 3  4 Rng= Np.random.RandomState (0)  5 X=rng.rand (10,2000)  6 x= Np.array (X,dtype= float32 ' )  7 print X.dtype 8 #dtype (' float32 ')  9 Transformer=random_projection. Gaussianrandomproject () 10 x_new=transformer.fit_transform (X) 11 print X_new.dtype12 #dtype (' float64 ')           

changing and updating parameters The code below is a demonstration of changing the kernel space in the SVM training model, first changing from RBF to linear and then back

1ImportNumPy as NP2From SKLEARN.SVMImportSvc 3  4 Rng=np.random.randomstate (0)  5 X= Rng.rand (100,10 6 y=rng.binomial (1,0.5,100)  7 #binomial generate two distributions, parameters are N, p, Size 8 X_ Test=rng.rand (5,10)  9 Clf=svc () 10 Clf.set_params (Kernal= linear ' ). Fit (x, y) 11 clf.predict (x_test) 12 clf.set_params (kernel= rbf ' ). Fit (x, y)  Clf.predict (x_test)             

Scikit-learn study Diary (1)

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.