Here we introduce two methods to splice arrays:
- (): Stack in the vertical direction
- (): Flat laying in the horizontal direction
import numpy as np arr1=([1,2,3]) arr2=([4,5,6]) print ((arr1,arr2)) print ((arr1,arr2)) a1=([[1,2],[3,4],[5,6]]) a2=([[7,8],[9,10],[11,12]]) print a1 print a2 print ((a1,a2))
The results are as follows:
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
[[1 2]
[3 4]
[5 6]]
[[ 7 8]
[ 9 10]
[11 12]]
[[ 1 2 7 8]
[ 3 4 9 10]
[ 5 6 11 12]]
It should be emphasized here that when I am using hstack, when I am doing assignment1 on cs231n, I always make errors in hstack! I realized that I had learned very superficially before!
(1)()
Function prototype: (tup)
where tup is an arrays sequence,tup : sequence of ndarrays
The arrays must have the same shape along all but the second axis,except 1-D arrays which can be any length.
Equivalent to: (tup, axis=1)
Example 1:
import numpy as np brr1=([1,2,3,4,55,6,7,77,8,9,99]) brr1_folds=np.array_split(brr1,3) print brr1_folds print brr1_folds[0:2]+brr1_folds[1:3] print ((brr1_folds[:2]+brr1_folds[1:3])) print brr1_folds[0:2] print brr1_folds[1:3] #print ((brr1_folds[0:2],brr1_folds[1:3]))
If the last line is not commented out, an error will occur;
[array([1, 2, 3, 4]), array([55, 6, 7, 77]), array([ 8, 9, 99])] [array([1, 2, 3, 4]), array([55, 6, 7, 77]), array([55, 6, 7, 77]), array([ 8, 9, 99])] [ 1 2 3 4 55 6 7 77 55 6 7 77 8 9 99] [array([1, 2, 3, 4]), array([55, 6, 7, 77])] [array([55, 6, 7, 77]), array([ 8, 9, 99])]
The reason for the error is that I think the dimensions of my array are inconsistent. Just change it to +, the plus sign is a splicing of list!
Example 2:
print (([1,2,3,3,4],[3,4,5,8,6,6,7]))
The result is: It shows that the one-dimensional array hstack is arbitrary.
[1 2 3 3 4 3 4 5 8 6 6 7]
Example 3:
It shows that our hstack must have the second dimension of the same:
print (([1,2,3,3,4],[3,4,5,8,6,6,7])) print (([[1,2,3],[2,3,4]],[[1,2],[2,3]]))
result:
[1 2 3 3 4 3 4 5 8 6 6 7]
[[1 2 3 1 2][2 3 4 2 3]]
If you change the top to the bottom, you will report an error! ! !
print (([1,2,3,3,4],[3,4,5,8,6,6,7])) print (([[1,2,3],[2,3,4]],[[1,2]]))
(2)()
Function prototype: (tup)
tup : sequence of ndarrays
The arrays must have the same shape along all but the first axis.1-D arrays must have the same length.
It means that except for the first dimension, we must have the same shape in other dimensions. One-dimensional arrays must be the same size.
Example 1:
print (([1,2,3],[3,4,3])) print (([1,2,3],[2,3]))
But you need to note that the second line is an error!
Example 2:
print (([[1,2,3],[3,4,3]],[[1,3,4],[2,4,5]])) print (([[1,2,3],[3,4,3]],[[3,4],[4,5]]))
The same shows that if the second dimension of our array is different, it makes an error.
print (([[1,2,3],[3,4,3]],[[2,4,5]])) print (([[1,2,3],[3,4,3]],[[4,5]]))
Example 3:
What we pass in is the list:
import numpy as np arr1=([[1,2],[2,4],[11,33],[2,44],[55,77],[11,22],[55,67],[67,89]]) arr11=([[11,2,3],[22,3,4],[4,5,6]]) arr1_folds=np.array_split(arr1,3) print arr1_folds print (arr1_folds)
result:
[array([[ 1, 2],
[ 2, 4],
[11, 33]]), array([[ 2, 44],
[55, 77],
[11, 22]]), array([[55, 67],
[67, 89]])]
[[ 1 2]
[ 2 4]
[11 33]
[ 2 44]
[55 77]
[11 22]
[55 67]
[67 89]]
This is the end of this article about the implementation of () and () in Numpy. For more related Numpy () content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!