Python-Machine Learning Notes-Project Actual Combat Record-20201130-HK

Python sort function, array splicing function

import numpy as np

  • Problems encountered in the project will be recorded at any time.
    The specific details of the project will be a separate note-taking blog at the end of the defense next year to systematically summarize the knowledge points. Some
    codes related to knowledge points will be open sourced.
  1. Sorting function sorted()
sorted(d.items(), key=lambda x: x[1])

Parameter_1 is the item to be sorted, and parameter_2 is the variable used as the order basis;
here is x[1], which is the value of the second dimension variable of d.items() as the order basis;

  1. Array concatenation function np.concatenate
test_train = np.concatenate([X_train, X_test])

Used to splice the training set and test set, and then randomly split them into new training set and test set.
When you need to specify the splicing direction, the axis defaults to 0, which means that the splicing is performed along the longitudinal direction by default;
if axis=1, the splicing is performed along the horizontal direction (pay attention to the dimension alignment along the axis)

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])

np.concatenate((a, b), axis=0) #沿着纵向
#输出
array([[1, 2],
       [3, 4],
       [5, 6]])

Along the horizontal direction, if it is directly spliced, the length of b is 1x2, and the length of a is 2x2, and an error will be reported, so you need to transpose b to 2x1 to splice along this axis.

np.concatenate((a,b.T),axis = 1)#沿着横向
#输出
array([[1, 2, 5],
       [3, 4, 6]])

Guess you like

Origin blog.csdn.net/weixin_42012759/article/details/109590423