Use Python to implement machine awareness (python Machine Learning 1 ).

Source: Internet
Author: User

Use Python to implement machine awareness (python Machine Learning 1 ).
0x01 Sensor

A sensor is a linear classifier of the second-class Classification and belongs to a discriminant model (another is to generate a model ). Simply put, the objective is divided into two categories by using the input feature and the hyperplane. Sensor machines are the foundation of neural networks and SVM.

Assume that the input space is, and the output space is. It is a feature vector ,.

Defines the function from the input space to the output space: it is a sensor. Is the weight of the sensor, which is the offset ,.

In the end, the sensor splits the positive instance points and the negative instance points in a hyperplane. Corresponds to a two-dimensional plane, that is, by determining a straight line to distinguish the two points distributed in the plane coordinate system, the coordinates of a point (feature vector) have been given ), you can determine the category of the vertex.

0x02 Algorithm

Supervised Learning has three elements: model, strategy, and algorithm. We use the sensor machine learning model. Let's talk about algorithms and learning strategies.

Procedure:

In step 2, how to update and match is determined by the learning strategy. Here, our loss function uses the total distance from all misclassified points to the superplane, that is, the set of misclassified points. According to the minimum gradient descent method ,,. This is all the algorithms of the sensor machine learning model. The following uses python to implement the sensor machine.

0x03 code implementation

First, define the symbol function:

1 # symbol function 2 def sign (v): 3 if v> 0: 4 return 15 else: 6 return-1

Training functions:

1 def training (): 2 train_data1 = [[1, 3, 1], [2, 5, 1], [3, 8, 1], [2, 6, 1] # positive sample 3 train_data2 = [[3, 1,-1], [4, 1,-1], [6, 2,-1], [7, 3,-1] # negative Sample 4 train_datas = train_data1 + train_data2 # sample set 5 6 weight = [0, 0] # weight 7 bias = 0 # offset 8 learning_rate = 0.5 # learning rate 9 10 train_num = int (raw_input ("train num :")) # iterations 11 12 for I in range (train_num): 13 train = random. choice (train_datas) 14x1, x2, y = train15 predict = sign (weight [0] * x1 + weight [1] * x2 + bias) # output 16 print ("train data: x: (% d, % d) y: % d ==> predict: % d" % (x1, x2, y, predict) 17 if y * predict <= 0: # false classification points 18 weight [0] = weight [0] + learning_rate * y * x1 # update weight 19 weight [1] = weight [1] + learning_rate * y * x220 bias = bias + learning_rate * y # update offset 21 print ("update weight and bias: "), 22 print (weight [0], weight [1], bias) 23 24 print (" stop training: "), 25 print (weight [0], weight [1], bias) 26 27 return weight, bias

Test function:

1 # test function 2 def test (): 3 weight, bias = training () 4 while True: 5 test_data = [] 6 data = raw_input ('enter test data (x1, x2): ') 7 if data = 'q': break 8 test_data + = [int (n) for n in data. split (',')] 9 predict = sign (weight [0] * test_data [0] + weight [1] * test_data [1] + bias) 10 print ("predict ==> % d" % predict)

 

0x04 Summary

As the basis of neural networks and support vector machines, perception machines are not difficult to implement, but they involve many ideas. Next, we will write the k-Nearest Neighbor Algorithm.

I am also a newbie in machine learning. The above introduction is my summary during the learning process. If there is any incorrect information, please correct me.

 

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.