import numpy as npa = np.array([1, 2, 3, 4])b = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]])print (a.shape)print (‘---‘)print (b.shape)# 输出 (4,)---(3, 4)
(4,) shape has an element that is a one-dimensional array with 4 elements in the array
(3, 4) shape has two elements that are two-dimensional arrays, with arrays of 3 rows and 4 columns
1.1 Change the length of each axis of the array by modifying the Shape property of the array to keep the number of array elements constant. The following example changes the shape of array b to (4, 3), from (3, 4) to (4, 3) not to transpose the arrays, but only changes the size of each axis, and the position of the array elements in memory does not change:
4, 3print (b)# 输出 [[ 1 2 3] [ 4 4 5] [ 6 7 7] [ 8 9 10]]
1.2. When the element of an axis is-1, the length of the axis is automatically calculated based on the number of elements in the array, and the following program changes the shape of array B to (2, 6):
2, -1print (b)# 输出 [[ 1 2 3 4 4 5] [ 6 7 7 8 9 10]]
2. Using the reshape method of the array, you can create a new array that changes the dimensions, and the shape of the original array remains the same :
a = np.array((1, 2, 3, 4))b = a.reshape((2, 2))b# 输出 array([[1, 2], [3, 4]])
3. Create an array:. Array
You first need to create an array to perform other operations on it by creating an array of sequence objects that pass Python to the array function, and if you are passing multiple nested sequences, you will create a multidimensional array (such as C):
Import NumPy as NPA = Np.array ([1,2,3,4]) B = Np.array ((5, 6, 7, 8) c = Np.array ([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, ten]]) print (A ) print ( '---') print (b) print ( '---') print (c) # output [1 2 3 4]---[5 6 7 8]---[[1 2 3 4] [4 5 6 7] [7 8 9]] /span>
- If you import numpy with a
import numpy
command, the form used when creating the array a = numpy.array([1, 2, 3, 4])
- If you import numpy with a
import numpy as np
command, use thea = np.array([1, 2, 3, 4])
http://blog.csdn.net/lwplwf/article/details/55506896
http://blog.csdn.net/sinat_34474705/article/details/74458605