Simple examples are used to understand what machine learning is, and examples are used to understand machine learning.

Source: Internet
Author: User

Simple examples are used to understand what machine learning is, and examples are used to understand machine learning.

 

1. What is machine learning?

What is machine learning?

Different people may have different understandings about this issue. In my personal opinion, to describe machine learning in big vernacular is to allow computers to learn and train in a certain way and select a proper model. When new input data is encountered, it can find useful information and predict potential needs. The final result is that computers or other devices are as intelligent as humans and can quickly identify and select useful information.

Generally, machine learning can be divided into three major steps: input, integration, and output.Can be used to indicate the approximate meaning:


2 machine learning example (scikit-learn)

In python, scikit-learn is an open-source machine learning library. The following uses sklearn as an example to briefly describe the process of machine learning.

2.1 load data

Generally, the first step is to obtain and process relevant data so that it can be used in subsequent processes.

from sklearn import datasets
  • Load iris dataset and view related information
# Load the dataset
iris = datasets.load_iris ()

# print (iris)
print (type (iris))
print (iris.keys ())

# View some data
print (iris.data [: 5,:])
# print (iris.data)
<class 'sklearn.datasets.base.Bunch'>
dict_keys (['DESCR', 'data', 'feature_names', 'target', 'target_names'])
[[5.1 3.5 1.4 0.2]
  [4.9 3. 1.4 0.2]
  [4.7 3.2 1.3 0.2]
  [4.6 3.1 1.5 0.2]
  [5. 3.6 1.4 0.2]]
# View data dimension size
print (iris.data.shape)

# Data attributes
print (iris.feature_names)

# Feature name
print (iris.target_names)

# Tags
print (iris.target)
(150, 4)
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)')
['setosa' 'versicolor' 'virginica']
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2
  2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
  twenty two]
2.2 select the machine learning model or Algorithm

After obtaining and organizing the data, you need to select an appropriate model or algorithm for training.
There are many types of machine learning models, which are not discussed here, and Parameter Selection for each model is also a great learning.

from sklearn import svm

svm_classifier = svm.SVC (gamma = 0.1, C = 100)

# Low prediction score
# svm_classifier = svm.SVC (gamma = 10000, C = 0.001)

# Define the data size of the test set
N = 10

# Training set
train_x = iris.data [:-N,:]
train_y = iris.target [: -N]

# Test set
test_x = iris.data [: N,:]
y_true = iris.target [: N]

# Training data model
svm_classifier.fit (train_x, train_y)
SVC (C = 100, cache_size = 200, class_weight = None, coef0 = 0.0,
   decision_function_shape = None, degree = 3, gamma = 0.1, kernel = 'rbf',
   max_iter = -1, probability = False, random_state = None, shrinking = True,
   tol = 0.001, verbose = False) 666666
  • Test the trained mode.
y_pred = svm_classifier.predict(test_x)
  • View test results
from sklearn.metrics import accuracy_scoreprint(accuracy_score(y_true, y_pred))
1.0
2.3 apply the trained model, that is, Prediction
  • Save Model
import pickle with open('svm_model_iris.pkl', 'wb') as f:
    pickle.dump(svm_classifier, f)
  • Load Model for application
import numpy as np
# np.random.seed (9)

with open ('svm_model_iris.pkl', 'rb') as f:
     model = pickle.load (f)

random_samples_index = np.random.randint (0,150,6)
random_samples = iris.data [random_samples_index,:]
random_targets = iris.target [random_samples_index]

random_predict = model.predict (random_samples)

print ('Real value:', random_targets)
print ('Predicted value:', random_predict)
Real value: [1 1 1 0 2 2]
Predicted value: [1 1 1 0 2 2] 
 Chatting 

The prediction results are good and reflect the advantages and disadvantages of the machine learning model selection. I still have a lot to learn about machine learning. Welcome to join us and make progress together.

Finally, I will share a picture on the Internet to see how to understand Machine Learning.


If you like my article, follow the public account: Python data path (ID: PyDataRoad)


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.