Recently began to learn the knowledge of Python machine learning, in order to make subsequent learning to avoid the basic problems encountered in programming, the Python array and matrix library numpy use to summarize, in order to deepen and consolidate their previous knowledge.
Use of section One:python arrays
In Python, the concept of arrays has actually been watered down, with tuples and lists, and the following summaries and uses of lists and tuples.
Subsection One:list
The list essence is a sequence type of data structure that is somewhat similar to the one learned in C/s + +, but different. Their similarity is that each element in both is assigned an index value for access, such as:
1 # python list 2 list1 = ['physics'chemistry', 1997, +] 3print(list1[0])
View Code
and in C + + we know that arrays can also be accessed by means of the array name indexed value, not to repeat it here.
However, if you are careful, we will find that the list in Python differs from that in C + +, and it can contain different data types, but not in C + +. In addition, in the above code, we can see that the two have the same creation method.
In Python, however, there are different ways to access the list type, and the following summarizes the access to the list:
1 """List:2 --------------------------------3 """4 #Create a list5List1 = ['Physics','Chemistry', 1997, 2000]6 7 #methods to access a list as follows:8 Print "List1[0]", List1[0]9 Print "List1[1:4]", List1[1:4]Ten One Print "List1[-1]", list1[-1] A Print "List1[-2]", list1[-2] - - Print "list1[1:]", List1[1:]
View Code
From the above two ways, you can actually see that the list in Python can be "start index: Terminate index (optional)" way to directly access a string of data in the list, which is not possible in C/s + +, which gives us great convenience, coupled with the termination of the index, Represents access to all data from the starting index to the terminating index, without adding that it means accessing all data from the starting index to the end of the list.
Also, in the list, the index value is negative to access the list from the end of the list, as shown in the example in figure three or four.
In addition, in Python for lists, the following methods have been introduced to update and delete lists and to give several methods of accessing list properties.
#list1.append ('Google')print List1
The list can add new elements to the end of the list through the Append method, and a string element called Google is added to the List1 list.
del list1[2]print"afterdeleting value at index 2", List1
View Code
As you can see, using the Del function to delete the data indexed to 2, in addition, the DEL function can be combined with the previously mentioned method of accessing the array to delete the relevant elements, such as:
del list1[1:]print"afterdeleting value by list1[1:]", List1
View Code
Similarly, the start index: the way to terminate index, is also able to delete a piece of the list of data.
In addition, Python provides a Len function to get the length of a list, using the "+" operator to enable merging between different arrays, using the "*" operator implementation to create a list of n identical elements, and some loops and traversal methods to determine whether the data is in the list, Here are the related actions:
"""List:--------------------------------"""#Create a listList1 = ['Physics','Chemistry', 1997, 2000]list2= [1, 2, 3, 4]l=Len (list1)Print "Access Len function to get the length of List1"PrintL#+ operatorPrintList1 +List2list3= List1 +List2Print "List3:", List3#* operatorLIST4 = ['HHHH'] * 4PrintList4#TraversePrint "Traverse List1:" forXinchList1:Printx#confirm X in listPrint "confirm X in list"Print2inchList1Print1997inchList1
View Code
In addition, the Min and Max functions can also be used to get the maximum minimum value of the list, the Count function calculates the number of occurrences of an element in the list, remove removes the matched data, sorts the sort function, and reverse the function in reverse order. These can be queried in the official document of the list.
Subsection: tuples
The tuple creates a similar root list, but it is created in parentheses:
""" tuples------------------------------ """ "Google")print= ('flesh ' ,) Print tup2
View Code
Attentive little friends may see, I create a second tuples, although there is only one element, but I still use a comma, in fact, this is necessary, although there is no compilation error, in this part of the code. However, in other cases it is not necessary, for example, we use tuples as the return parameter, without commas, but only one data in the returned tuple, such as 26, then the computer will be unable to recognize that the element is the number 26 or tuple 26. Therefore, adding is necessary, also helps us to develop good programming habits.
Tuple access is the same as the list, because they are the most common in Python sequence structure, do not repeat here, interested in the small partners can try it out, it is worth noting that the form of access to the same as the list, not the way L (index), but L[index].
In addition, in tuples, modifications to related data are illegal, as follows:
Print " Modify the data at index 2 " tup1[2] =print tup1
View Code
So give a small group of friends in the use of tuples must pay attention to AH!
However, in tuples, you can use the "+" and "*" two operators to make changes to tuples.
The rest of the relevant methods, such as Len, are similar and can be understood by looking at the documentation.
Section two:numpy use of libraries (follow-up supplement, to eat 23333)
Python common sequence list, tuples, and matrix library numpy use