پایتون؛ کتابخانه numpy

import numpy as np #import library to current file

vector = [0, 1, 3] #define  normal vector

nvector = numpy.array(vector) #cast normal vector into numpy vector

matrix = [[0,1,2,3], [4,5,6,7], [8,9,10,11], [12,13,14,15]]

nmatrix = np.array(matrix)

#################################################################

# create zero or one matrix

np.ones((2,3), float)

np.ones((2,3), int)

np.zeros((2,3), float)

np.zeros((2,3), int)


marr = narr.copy()  #==> create new copy of array

################################################################

nmatrix.astype("f") #change elements type to float

np.diag(nmatrix) #Matrix diameter

vector1.T #Transpose vector

np.repeat(vector,2) #Repeat  vector element 2 times  ==> (1,2)  ==> (1, 1 , 2, 2)

pos = np.arange(0,15,1) **2 #generate matrix elements from value 0 to 15  with 1 as step finally power on 2

################################################################

# Vertical & Horizental append
vector1 = np.array([1,2])
vector2 = np.array([3,4])
np.vstack([vector1, vector2])   #==>  array([[1, 2],  [3, 4]])
np.hstack([vector1, vector2]) #==> array([1, 2, 3, 4])

################################################################

vector3 = vector1 * vector2 #multiply vectors  ==>  [3 8]
vector1.dot(vector2)   #multiply vectors and finally add elements
##################################################################
nmatrix.mean()   #==> average
nmatrix.std() #==> standard deviation
nmatrix.argmax() #==> element index with max value
###################################################################
pos = array([  0,   1,   4,   9,  16,  25,  36,  49,  64,  81, 100, 121, 144,169, 196])
pos[2:3:1]  #==> [4]
pos[2]   #==> 4
pos[1:5:1]   #==> [1, 4, 9, 16]
pos[:5]   #==> [0, 1, 4, 9, 16]
pos[-2]   #==>  169
pos[-2::1]   #==> [169, 196]
pos[5:12:3]   #==>  [25,  64, 121]
pos[10::-2]   #==> [0, 4, 16, 36, 64,  100]   Wrong,   [100, 64, 36, 16, 4, 0] Correct
pos[1:6:3]   #==> [1, 16]
pos[-1:-6:-4]   #==>   [100,  196]  Wrong,  [196, 100] Correct
pos[3::5]   #==> [9,  64, 169]
pos[:-1] #==>از اول تا یکی مونده به آخر


narr = numpy.array([ [1,2,3], [0,0,0], [0,0,0] ])
narr[0:2, 1:3]   #==> [[2,3], [0,0]]
narr[::2, -1]   #==>  [3, 0] 
narr[::2, -1:]   #==> [ [3],  [0] ]
narr[2, -1]   #==>  0
narr[2:, -1:]   #==> [[0]]
narr[1::2, -2]   #==> [ 0 ]
narr[-1::-1, -2:]   #==> [[0,0], [0,0], [2,3] ]
narr[:] = narr[:] ** 2   #==>     [ [1,4,9], [0,0,0], [0,0,0] ]
############################################################################
import numpy as np
narr = np.array([[1, 2, 3],
       [0, 0, 0],
       [0, 0, 0]])

narr[(narr > 0) & (narr < 3)]  #==>    [1, 2]
narr > 0   
#==> array([[ True, True, True],
       [False, False, False],
       [False, False, False]])

narr[(narr > 0) & (narr < 3)] = 10 			#==>  it is call by reference, now narr  is:
array([[10, 10,  3],
       [ 0,  0,  0],
       [ 0,  0,  0]])
#############################################################################
import numpy
tarr = numpy.random.randint(0, 10, (4, 3) )
tarr     #==> 
array([[0, 9, 6],
       [6, 5, 6],
       [8, 1, 3],
       [0, 8, 3]])

tarr[1::1, 1:2] #==> [[5],
       [1],
       [8]]

####################################
def quicksort (arr, i = 0):
    if i == 0:                           #cast array to numpy array in first function call
        arr = numpy.array(arr)
    if len(arr) == i:                    #continue recursive call till last element
        return arr
        
    m = min( arr[i:] )                   #find min from floating incremental position
    mi = numpy.argmin( arr[i:] ) + i     #find min position & add i to achive real position in original array
    arr[mi:mi+1] = arr[i:i+1]            
    arr[i:i+1] = m               

    return quicksort (arr, i+1)
#----------------------
#Sample Call:

import numpy
arr = [6,5,4,8,6,5,4,7,9,9,-1,100]

quicksort(arr)
#----------------------
#Result:


array([ -1,   4,   4,   5,   5,   6,   6,   7,   8,   9,   9, 100])
###########################################


نظرات 0 + ارسال نظر
امکان ثبت نظر جدید برای این مطلب وجود ندارد.