cs231n (i) KNN and some Python numpy functions

Source: Internet
Author: User

The first question of the first assignment, write the KNN classifier, the principle of KNN itself is relatively simple to describe,

Some of the functions used:

(1) Numpy.flatnonzero ():

The function enters a matrix that returns the position of the non-0 element in the flattened matrix (index)

This is the official document gives the usage, very formal, input a matrix, return the position where the non 0 elements

1 >>> x = Np.arange ( -2, 3)2 >>> x3 Array ([-2,-1,  0,  1,  2]) 4 >>> np.flatnonzero (x)5 Array ([0, 1, 3, 4])

This is the usage given in the job: do not go the normal way, used to return the position of a particular element

The judgment of the vector element d==3 returns a matrix of 0/1 that is equal to the vector, and then calls the function, returning the position that corresponds to the element to be found.

D = Np.array ([1,2,3,4,4,3,5,3,6= Np.flatnonzero (d = = 3)print haa
(2) Matplotlib.pyplot.subplot (XXX):

The function input is three integers such as subplot (211), which is not very clear, can be written subplot (2,1,1) the first two numbers of the matrix composed of sub-graphs of the number of rows, such as 6 sub-graphs, arranged into 3 rows 2 columns, that is subplot (3,2,x). The last number means to draw the first X chart. Usage in the job:

#visualize some examples from the dataset.#We Show a few examples of training images from each class.classes = ['plane','Car','Bird','Cat','Deer','Dog','Frog','Horse',' Ship','Truck']#Category ListNum_classes = Len (classes)#Number of categoriesSamples_per_class = 7#number of samples per category forY, CLSinchEnumerate (classes):#loops the element position and element of the list, y represents the element position (0,num_class), the CLS element itself ' plane ', etc.IDXS = Np.flatnonzero (Y_train = = y)#find the position of the Y class in the labelIDXS = Np.random.choice (Idxs, Samples_per_class, Replace=false)#Choose from the 7 samples we need     forI, IDXinchEnumerate (IDXS):#Loops the position of the selected sample and the picture corresponding to the sample in the training setPLT_IDX = i * num_classes + y + 1#calculation of the occupied position in the sub-graphPlt.subplot (Samples_per_class, num_classes, Plt_idx)#indicates the number of the sub-graph to be drawnPlt.imshow (X_train[idx].astype ('uint8'))#DrawingPlt.axis ('if')        ifi = =0:plt.title (CLS)#write the title, that is, the category namePlt.show ()#Show

cs231n (i) KNN and some Python numpy functions

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.