NumPy > Create arrays

Name
Code
Output
array = numpy.array([1,2,3,4,5])
print (array)
print ("shape",array.shape)
[1 2 3 4 5]
shape (5,)
array = numpy.array([(1,2,3), (4,5,6)])
print (array)
print ("shape",array.shape)
[[1 2 3]
 [4 5 6]]
shape (2, 3)
array = numpy.array([[(1,1,1), (2,2,2)],\
                     [(3,3,3), (4,4,4)]])
print (array)
print ("shape",array.shape)
[[[1 1 1]
  [2 2 2]]

 [[3 3 3]
  [4 4 4]]]
shape (2, 2, 3)
array = numpy.arange(10,15)
print (array)
print ("shape",array.shape)
[10 11 12 13 14]
shape (5,)
array = numpy.arange(10,25,2)
print (array)
print ("shape",array.shape)
[10 12 14 16 18 20 22 24]
shape (8,)
array = numpy.full((3,5),4)
print (array)
print ("shape",array.shape)
[[4 4 4 4 4]
 [4 4 4 4 4]
 [4 4 4 4 4]]
shape (3, 5)
array = numpy.ones((3,5))
print (array)
print ("shape",array.shape)
[[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]
shape (3, 5)
array = numpy.zeros((3,5))
print (array)
print ("shape",array.shape)
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]
shape (3, 5)
array = numpy.random.randint(10, size=(2, 3))
print ("original array \n",array)
view = array[1,]
print("array view \n",view)
array[:,:] = array + 1
print("view after array update \n",view)
original array 
 [[7 0 0]
 [6 2 3]]
array view 
 [6 2 3]
view after array update 
 [7 3 4]
array = numpy.random.random((2,3))
print (array)
print ("shape",array.shape)
[[ 0.64677843  0.64422554  0.37583197]
 [ 0.6459913   0.86611144  0.30007033]]
shape (2, 3)
array = numpy.random.randint(10, size=(2, 3))
print ("original array \n",array)
copy = numpy.copy(array)
# alternatively you can use also array.copy()
print("array copy \n",copy)
array[0,0] = 100
print ("modified array \n",array)
print("unmodified copy \n",copy)
original array 
 [[5 9 3]
 [9 6 0]]
array copy 
 [[5 9 3]
 [9 6 0]]
modified array 
 [[100   9   3]
 [  9   6   0]]
unmodified copy 
 [[5 9 3]
 [9 6 0]]
array = numpy.empty((2,3))
print (array)
print ("shape",array.shape)
[[ 0.64677843  0.64422554  0.37583197]
 [ 0.6459913   0.86611144  0.30007033]]
shape (2, 3)
array = numpy.linspace(0,2,6)
print (array)
print ("shape",array.shape)
[ 0.   0.4  0.8  1.2  1.6  2. ]
shape (6,)
array = numpy.eye(3)
print (array)
print ("shape",array.shape)
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
shape (3, 3)