Originally write NLP Lesson dynamic programming, Japan, and wrote half found himself not a theory very simple, dynamic programming also know Editor's Note, but implemented on a source or a little more difficult, now simply give the title description , junior partner may also be thinking about, a example, we now have one yuan coins, two dollar coins, 5 yuan and 10 yuan coin coin. We want the amount of money for M coin, guarantee a minimum number of coins, what is our change process is, example two, we now have M-meter rope, cut into segments N (N is an integer of certain length), the N segment multiplied by the length of the rope to ensure maximum product results, we need to intercept a few days to head back to write this. Today, we continue to talk about the use numpy, this time we look at the usage numpy through exercises.
Problem: arr replaces all odd -1, without changing arr. (Tip where)
arr = np.arange(10)
out = np.where(arr % 2 == 1, -1, arr)
where syntax, parameters First, conditions, parameters and second, to be replaced to meet the conditions of the why, the third is an input parameter array.
Question: converting a 2-dimensional array of two-dimensional array of rows given: np.arange (10)
arr = np.arange(10)
out = np.reshape(2,-1)
reshape syntax (row, column) as -1 if the two parameters, not to limit the
Problems: a vertically stacked array and array b, given: a = np.arange (10) .reshape (2, -1) b = np.repeat (1, 10) .reshape (2, -1) the desired output :
> array([[0, 1, 2, 3, 4],
> [5, 6, 7, 8, 9],
> [1, 1, 1, 1, 1],
> [1, 1, 1, 1, 1]])
np.arange = A (10) .reshape (2, -1 ) B = np.repeat (. 1, 10) .reshape (2, -1 ) np.concatenate ([A, B], Axis = 0) # Method a np.vstack ([a, B]) # method II np.r_ [a, B] # method three
Method a: concatenate syntax is a list of parameters, axis = 0 is a longitudinal stack, a transverse
Method two: vstack vertically stacked, laterally extended hstack.
Method three: r_ vertically stacked, laterally extended c_.
Problem: Create the following mode without using a hard-coded. Numpy function only following input array and a.
given:
a = np.array ([1,2,3]) `desired output:
> array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
a = np.array([1,2,3])
np.r_[np.repeat(a, 3), np.tile(a, 3)]
Description: np.repeat (a, 3) to obtain the array ([1, 1, 1, 2, 2, 2, 3, 3, 3]), np.tile (a, 3) to obtain the array ([1, 2, 3, 1, 2, 3, 1, 2, 3]), and then be stacked r_
Problem: get a common term between the array and the array b. ab intersection
np.intersect1d(a,b)
Problem: Remove all items from the array array b in a. Given: a = np.array ([1,2,3,4,5]) b = np.array ([5,6,7,8,9]) the desired output:> array ([1,2 3, 4])
a = np.array([1,2,3,4,5]) b = np.array([5,6,7,8,9]) np.setdiff1d(a,b)
Note that when using setdiffld order, in the first parameter data comprises removing the second element, and does not contain the remaining elements of the second parameter. (May be a bit Raozui).
Problems: Get matching elements a and b positions .
a = np.array([1,2,3,2,3,4,3,4,5,6]) b = np.array([7,2,10,2,7,4,9,4,9,8]) np.where(a==b)
Question: Gets all the items between 5-10.
a = np.array([2, 6, 1, 9, 10, 3, 27]) index = np.where((a >= 5) & (a <= 10)) a[index]
Question: arr exchange array rows 1 and 2.
arr = np.arange(9).reshape(3,3)
arr[[1,0,2],:]
Problem: array arr exchange columns 1 and 2.
arr = np.arange(9).reshape(3,3) print(arr) arr[:, [1,0,2]]
Question: line inversion of two-dimensional array arr.
arr = np.arange(9).reshape(3,3)
arr[::-1]
Question: column inversion two-dimensional array of arr.
arr = np.arange(9).reshape(3,3)
arr
arr[:, ::-1]
Problem: Create a 5x3 two-dimensional array shape so as to contain a random decimal number between 5-10.
arr = np.arange(9).reshape(3,3) # Solution Method 1: rand_arr = np.random.randint(low=5, high=10, size=(5,3)) + np.random.random((5,3)) # print(rand_arr) # Solution Method 2: rand_arr = np.random.uniform(5,10, size=(5,3)) print(rand_arr)
It should elaborate, arr [a, b] is a slice of arr example: A [ 2 : 7 : 2 ] # starting at index 2 to index 7 stops, at intervals of 2, and in our two-dimensional array, we can use ''; for example, a [X, M], then we remove, the first element of the M + 1 X + 1 row.
There: and there ... and used. We look at an example: that is all, such as a [:, 2] is what we want to remove all of his pipe in the third column how many rows. Colleagues a [2 ,:], too, all in the second row, though he how many columns it.
We said above, joined by two colons :: N is from: elements, to take first: element, step is N, :: That has made all of it. The step size is negative will reverse. We continue to talk about the next issue of the use of Nump it ~!