Machine Learning .2

Knn handwriting algorithm:

  Before looking at knn algorithm, simply go to learn about the use of python, python simple to understand a bit of knowledge, discovery and c ++ python sequence in the array is somewhat different,

A, python learning.

Lists, tuples, support two-way index string, the first element index is 0, the second element index 1, and so on; the last element index is -1, the second to last element of the subscript -2, and so on.

String: index operator [i] obtained i the subscript character slice operator [i: J] obtained from the index i to index j-1 sub-string of the first character index is 0, the last character index is -1 plus sign (+) for string concatenation operator, the asterisk (*) is used to copy a string.

List: index operator [i] to give the subscript i of the element, the slice operator [i: J] to give the j-1 from a subset of the index i to the next, the first element index is 0, the last element index -1.

A list of commonly used methods:

 

 

 

Second, the learning algorithm handwriting knn

First of all:

iris = datasets.load_iris()
x = iris.data
y = iris.target
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=2003)

Here we import the data set is called the iris data set

x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=2003)

X is stored characteristic data, a label or classification of each sample is stored in y, train_test_split to the data into training and test sets.

 

 

 

 Insert Euclidean distance formula to calculate the distance between any two points:

def euc_dis(instance1, instance2):
diff = instance1 - instantce2
diff = diff ** 2
dist = sum(diff) ** 0.5
return dist

Also known as Euclidean distance Euclidean distance, is the most common distance metric, measures the absolute distance between two points in a multidimensional space.

It can be understood as: the real distance between two points in m-dimensional space, or a natural length of the vector (i.e., the distance from the origin to the point). Euclidean distance in two-dimensional and three-dimensional space is the actual distance between two points

The following are specific formula:

 

 

def knn_classify(x, y, testInstance, k):
    dis = []
    for i in x
        dis.apend(euc_dis(i,testInstance))
    maxIndex = map(dis.index,heapq.nsmallest(k,dis))
    maxY = []
    for i in maxIndex
        maxY.append(y[i])
    return max(maxY,key = maxY.count)

knn the algorithm, find x testInstance distance to each point of dis exist in the list, the module provides an implementation heapq heap sort algorithm. Binary heap is the maximum stack parent child node is greater than or equal to two, the minimum heap parent child nodes is less than or equal to two.

heapq.nsmallest (k, dis) obtained at the k subscript minimum distance, the minimum heap to find k. map module map ()  will be mapped according to a designated sequence of functions provided. The first parameter argument function which function to each element in the sequence 
function, each function returns a new list of function return values. Return the highest number of labels value appears.
prediction = [knn_classify(x_train, y_train, data, 3)for data in x_test]
correct = np.count_nonzero((prections == y.test) == true)

Compare handwriting knn original database and algorithm to calculate accurate rates.

 

Third, feelings and experiences

  In fact, just started watching when knn handwriting algorithm, a lot of things are not very understanding, according to information given by the students themselves simple look, learn a little, and then went to see the code, understand the meaning of each step slowly, do not know where in Baidu relevant codes, one by one to solve the problem, a basic understanding of the meaning of knn algorithm code, is actually very simple, it is based on the original database, write a budget code, the similarity estimate goals and objectives to determine whether the corresponding correct themselves for the usage of the python still relatively unknown, also need plenty of practice to be more understanding, more to learn in order to progress.

 

 

Guess you like

Origin www.cnblogs.com/ydh52/p/11932185.html