numpy is used to realize the simple code example of the concatenation of one and two dimensional arrays

  • 2020-06-15 09:46:59
  • OfStack

1 d array

1.numpy initializes a 1-dimensional array


a = np.array([1,2,3]);
print a.shape

The output value should be (3,).

2 d array

numpy initializes a 2-dimensional array


a = np.array([[1,2,3]]);
b = np.array([[1],[2],[3]]);

print a.shape//(1 . 3 ) 
print b.shape// ( 3,1 ) 

Note that the arrays of (3,) and (3,1) are different. The former is a 1-dimensional array and the latter is a 2-dimensional array.

Joining together

3.numpy has many splicing functions. hstack and vstack, for example. Online and a lot of such summary posts. But the only way two arrays can be concatenated is if they have the same dimensions. So newaxis is used to convert a 1-dimensional array to a 2-dimensional array when concatenating 2-dimensional arrays and 1-dimensional arrays, that is, shape is converted from (3,) to (3,1).


a = np.array([1,2,3]);
b = np.array([[1],[2],[3]]);
# will 1 Dimensional array a into 2 Dimensional array 
a = a[:,np.newaxis];
c = np.concatenate((b,a),axis=1)
print c.shape// Output for ( 3,2 ) 

conclusion

That's the end of this article's simple code example of using numpy to splicing 1. 2 dimensional arrays. Those who are interested can continue to see other related topics on this site. If there is any deficiency, please let me know. Thank you for your support!


Related articles: