NumPy > Transform array

Name
Code
Output
array = numpy.random.random((2,3))
print ("original data type: ",array.dtype.name)
array = array.astype(numpy.int64)
print ("new data type: ",array.dtype.name)

# NumPy Datatypes 
# BOOLEAN TYPES: 
# bool_
# INTEGER TYPES: 
# int_, intc, intp, int8, int16, int32, int64
# UNSIGNED INTEGER TYPES: 
# uint8, uint16, uint32, uint64
# FLOATING POINTS TYPES: 
# float_, float16, float32, float64
# COMPLEX NUMBER TYPES: 
# complex_, complex64, complex128
original data type:  float64
new data type:  int64
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = array.ravel()
print ("flatened array \n",result)
array 
 [[4 4 1]
 [0 0 3]]
flatened array 
 [4 4 1 0 0 3]
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result = array.reshape(3,2)
print ("reshaped array \n",result)
array 
 [[3 4 3]
 [3 3 2]]
reshaped array 
 [[3 4]
 [3 3]
 [3 2]]
array = numpy.random.randint(5, size=(3, 4))
print ("array \n",array)
array.resize((2,3))
print ("resized array \n",array)
array 
 [[1 1 4 0]
 [1 4 1 3]
 [3 4 3 2]]
resized array 
 [[1 1 4]
 [0 1 4]]
array = numpy.array([1,2,3])
print ("array \n",array)
print ("reversed array \n", array[: : -1])
array 
 [1 2 3]
reversed array 
 [3 2 1]
array = numpy.random.randint(10, size=(2, 3))
print ("original array \n",array)
array.sort()
print("sorted array \n",array)
original array 
 [[3 9 1]
 [5 3 8]]
sorted array 
 [[1 3 9]
 [3 5 8]]
array = numpy.random.randint(20, size=(2, 3))
print ("original array \n",array)
array.sort(axis=0)
print("sorted array axis 0 \n",array)
array.sort(axis=1)
print("sorted array axis 1 \n",array)
original array 
 [[16 11  7]
 [ 1 12 16]]
sorted array axis 0 
 [[ 1 11  7]
 [16 12 16]]
sorted array axis 1 
 [[ 1  7 11]
 [12 16 16]]
array = numpy.random.randint(5, size=(2, 3))
print ("array \n",array)
result1 = numpy.transpose(array)
print ("transpose method 1 \n",result1)
result2 = array.T
print ("transpose method 2 \n",result2)
array 
 [[1 4 1]
 [4 1 3]]
transpose method 1 
 [[1 4]
 [4 1]
 [1 3]]
transpose method 2 
 [[1 4]
 [4 1]
 [1 3]]