import numpy as np
list=[(1,2,3),(4,5,6)]
tuple=(1,2,3,4)
def np_2d_array(list):
np_array=np.array(list)
print np_array
def the_2ed_argument_of_array(list):
np_array=np.array(list,dtype=complex)
print np_array
#ones() is same as this
def the_use_of_zeros():
print np.zeros((3,4))
#the initial elements depends on the state of memory
def the_use_of_empty():
print np.empty((3,4))
#arange reterns arrays but range returns lists
def the_difference_between_arange_and_range():
print np.arange(10,20,5)
print range(10,20,5)
print np.arange(1,3,0.2) # arange() accepts float arguments but not range()
def the_use_of_reshape():
print np.arange(6) #1 dimensional array
print np.arange(12).reshape(2,6) #2 dimensional array
print np.arange(24).reshape(2,3,4) #3 dimensional array
def the_use_of_asterisk_among_arrays():
print [[1, 2][3, 4]] * [[5, 6][7, 8]]
def the_use_of_dot(nparray1, nparray2):
print np.dot(nparray1, nparray2)
print nparray.dot(nparray2)
# some operations such as *=, += will modify the initial array, others will create a new one
# in numpy, there are some diferences with python27, in former, int + float is illegal, the latter will create a new object
def operations_modify_arrays():
try:
a = np.ones((2, 3), dtype=int)
b = np.random.random((2, 3))
a *= 3
b += a
a += b
except TypeError as e:
print e
try:
c=int(1)
d=float(0.1)
c+=d
print c
except TypeError as e:
print e
# in python, if a list is too large, the system will print the corner of this list
# in numpy, there is a function which can print totally this large array
# more details can read numpy
def axis_parament():
a=np.random.random((2,3))
print a.sum()
print a.min()
print a.max()
b=np.arange(12).reshape(3,4)
print b.sum(axis=0) # sum of each column
print b.min(axis=1) # the min of each row
def universal_functions():
a=np.array([0,1,2])
print np.exp(a) # the n-th of e
print np.sqrt(a)
b=np.array([3,4,5])
print np.add(a,b)
# in numpy, the ndarray is same as lists in python, which can be indexed, sliced and iterated over
# there are codes I'm not familiar to
def slice_ndarray():
a=np.array([0,1,2,3,4,5,6])
a[:6:2]=-1000 # from start to position 6, the first element of each two elements is assigned -1000
print a
print a[::-1] # the step is '-'
def the_function_of_fromfunction():
def f(x,y):
return 5*x+y
b=np.fromfunction(f,(5,4),dtype=int) # due to the result, the function of this function is to use abscissa axis to be x, the other is y.
print b
print b[2,3] # b[2,3] is same as b[2][3]
print b[2][3]