Task 3

from sklearn Import Datasets     # introducing a sample data 
from sklearn. model_selection Import train_test_split # divided data set, the data into training and test sets 
Import numpy AS NP
 Import heapq
iris = datasets.load_iris ()    # Import iris data set 
X-= iris.data
and = iris.target
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=2003)

# Define seek distance function 
DEF euc_dis (instance1, instance2):
    the diff = instance1-instance2    # calculating the difference between 
    the diff the diff = 2 **              # square 
    dist = SUM (the diff) ** 0.5       # summing prescribing 
    return dist

# Define the classification function knn 
DEF knn_classify (X-, Y, testInstance, K):
    dis=[]
    for i in X:
        dis.append (euc_dis (I, testInstance))   # request from each of the X testInstance vector 
    
    maxIndex = Map (dis.index, heapq.nsmallest (k, DIS))   # determined minimum distance of the k standard 
    
    maxY = []
     for I in maxIndex:
        maxY.append (Y [I])    # Add the samples corresponding to the tag array maxY 
    return max (maxY, Key = maxY.count)   # find the largest number of tag values appear 
predictions = [knn_classify (X_train, y_train , data, 3 ) for the Data in X_test]
correct = np.count_nonzero((predictions==y_test)==True)
print("Accruacy is: %.3f" %(correct/len(X_test)))
 

 

train_test_split function matrix for randomly divided into a training set and a test set, and returns the return division and divide good good training data set and test set

grammar:

X_train,X_test, y_train, y_test =cross_validation.train_test_split(X,y,test_size, random_state)

among them:

X: Sample set of features to be divided

y: sample labels to be divided

test_size: if between 0 and 1, the number of test set sample and the ratio of the number of original samples; if it is an integer, is the number of test set sample.

random_state: random number seed    

X_train: partitioned training data (return value)

X_test: dividing a test set of data (return value)

y_train: tag divided training set (return value)

y_test: dividing a tag test set (return value)

Random Seed: The random number in the group, when the need to repeat the test, guaranteed the same set of random numbers. For example, each time filling 1 , under the same other parameters random array you get is the same. But fill 0 or not fill, each will be different.

Generating a random number depends on the relationship between the seed and the random number seed comply with the following two rules:

Different Seed, produce different random number; the same seed, even if different instances also generates the same random number.

Reference blog: https://blog.csdn.net/fxlou/article/details/79189106

 

heapq module two functions --nlargest () and nsmallest () can find the maximum or minimum in a set of N elements

E.g:

>>> import heapq
>>> nums=[6,8,2,0,11,-4,-9,23,4,96,27]
>>> print(heapq.nlargest(3,nums))
[96, 27, 23]
>>> print(heapq.nsmallest(3,nums))
[-9, -4, 0]

 

Squaring diff ^ 2: ① diff ** 2 - Expression

                        ② Import the Math   - use the built-in module

                             math.pow(diff, 2)

                        POW ③ ( the diff, 2)       - the use of built-in functions

Reference blog: https://blog.csdn.net/jerry_1126/article/details/82917405

 

the python for loop is often used to traverse the strings, lists, tuples, dictionaries

grammar

for x in y:
   statements(s)

Execution flow: x represents an element y in turn, the complete cycle through all elements of the end

Reference blog: https://www.cnblogs.com/kiki5881/p/8541887.html

 

append () method is used at the end of the list to add new objects

grammar

list.append(obj)

obj - the object is added to the end of the list

 

map () will do the mapping functions provided according to a designated sequence

The first parameter argument function to call the function function of each element in the sequence, each time the function returns a new list of function return values

grammar:

map(function, iterable, ...)

Examples

DEF Square (X):             # calculates the square of 
     return X ** 2

Map (Square, [ 1,2,3,4,5])    # calculates the square of each element of the list 
[1, 4, 9, 16, 25 ]
Map ( lambda X: X ** 2, [1, 2,. 3, 4,. 5])   # Using lambda anonymous function 
[1, 4, 9, 16, 25 ]
 
# Two lists, a data list by adding the same position 
Map ( the lambda X, Y: X + Y, [. 1,. 3,. 5,. 7,. 9], [2,. 4,. 6,. 8, 10 ] )
[3, 7, 11, 15, 19]

Reference: https://www.runoob.com/python/python-func-map.html

 

Reference code look down, many do not understand, streamline ideas, knowledge has a lot to learn, but fortunately, there are Baidu this kind of thing, and she now they know, or too little

These functions are now mixed with a Lian Shu, although not very familiar with, often have the opportunity to use, you should be able to practice makes perfect

总之,There is still a long way to go.

 

Guess you like

Origin www.cnblogs.com/C-ch3-5/p/11894439.html