Here we present two methods for stitching arrays:
(): stacked in the vertical direction
(flat
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]]
One more thing that needs to be emphasized here is that when I was doing assignment1 on cs231n when it comes to hstack applications, I always get errors here on hstack! I just realized that what I learned before was very superficial!
(1)()
Function prototype: (tup)
where tup is a sequence of arrays.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 one:
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]))
The last line will give an error if not commented out; the
[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 was thinking that the dimensions of my arrays were inconsistent. Just change it to +, the plus sign is a list splice!
Example two:
print (([1,2,3,3,4],[3,4,5,8,6,6,7]))
The result: shows that the one-dimensional array hstack is arbitrary.
[1 2 3 3 4 3 4 5 8 6 6 7]
Example three:
Show that our hstack must be the same in the second dimension:
print (([1,2,3,3,4],[3,4,5,8,6,6,7])) print (([[1,2,3],[2,3,4]],[[1,2],[2,3]]))
Results:
[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 above to the below it 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.
Indicates that we must have the same shape in all dimensions except the first dimension, which can be different. one-dimensional arrays must be the same size.
Example one:
print (([1,2,3],[3,4,3])) print (([1,2,3],[2,3]))
But you should note that the second line is out of order!
Example two:
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 if the second dimension of our array is not the same so there is an error.
print (([[1,2,3],[3,4,3]],[[2,4,5]])) print (([[1,2,3],[3,4,3]],[[4,5]]))
Example three:
We passed in 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)
Results:
[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 article about () and () is introduced to this, more related () and () content please search my previous articles or continue to browse the following related articles I hope you will support me more in the future!