Vortex.ihrc.fiu.edu



Mid-term review#Numpy array>>> import numpy as np>>> myArray=np.array(mylist)>>> myArray=np.ndarray((3,3)) #The array is created in memory but uninitialized. >>> myArray=np.zeros((3,3))>>> myArray=np.ones((3,3))>>> myArray= np.arange(0,10,0.2)>>> myArray =np.linspace(2.0, 3.0, num=5)>>> myArray = np.random.random((3,5))>>> myArray = np.random.randn(10000) # normally distributed data# NumPy Array Slicing>>> myArray[0:2,0:2]#Transposing an array,>>> np.transpose(myArray)>>> myarray.T#Finding dimensions of an array>>> myArray.shape# Reshaping an array,>>> myArray_1= myArray.reshape(2,6)#Find elements in an array satisfying a condition>>> myArray[myArray>5]# Combining arrays >>> a = np.array([[1, 2], [3, 4]])>>> b = np.array([[5, 6]])>>> np.concatenate((a, b), axis=0)array([[1, 2], [3, 4], [5, 6]])>>> np.concatenate((a, b.T), axis=1)array([[1, 2, 5], [3, 4, 6]])#Array math>>> Z=X+Y>>> Z=X-Y>>> Z=X*Y>>> Z=X/Y#mean>>> np.mean(x)>>> np.mean(x,axis=0)>>> np.mean(x,axis=1)>>> np.nanmean(x)>>> np.nanmean(x,axis=0)>>> np.nanmean(x,axis=1)#minimum, maximum>>> np.amin(x)>>> np.amin(x,axis=0)>>> np.amin(x,axis=1)>>> np.nanmin(x)>>> np.nanmin(x,axis=0)>>> np.nanmin(x,axis=1)>>> np.amax(x)>>> np.amax(x,axis=0)>>> np.amax(x,axis=1)>>> np.nanmax(x)>>> np.nanmax(x,axis=0)>>> np.nanmax(x,axis=1)# Finding array index of minimum and maximum>>>i_min=np.argmin(a)>>>i_min=np.argmin(a,axis=0)>>>i_min=np.argmin(a,axis=1)>>>i_max=np.argmax(a)>>>i_max=np.argmax(a,axis=0)>>>i_max=np.argmax(a,axis=1)#standard deviation >>> np.std(x)>>> np.std(x,axis=0)>>> np.std(x,axis=1)>>> np.nanstd(x)>>> np.nanstd(x,axis=0)>>> np.nanstd(x,axis=1)#numpy math>>> np.exp(10)>>> np.log(10)>>> np.sin(10)……#graphics >>>import matplotlib.pyplot as plt>>> plt.plot(x,y)>>>plt.show()#curve properties>>> plt.plot(x,y,'--') #line style>>> plt.plot(x,y,'r') #line color>>> plt.plot(x,y,'.') #marker>>> plt.plot(x,y, linewidth=5.0)>>> plt.plot(x,y, color=[0.5,0.5,0.5])>>> plt.plot(x,y, 'o', markersize=20.0)# labels and title>>> plt.xlabel('some numbers')>>> plt.ylabel('some numbers')>>> plt.title('Illustration plot')>>> plt.xlabel('some numbers',fontsize=28)>>> plt.ylabel('some numbers',fontsize=28)>>> plt.title('Illustration plot',fontsize=28)#label ticks>>> xticks=np.arange(0,7.5,0.5)>>> plt.xticks(xticks)>>> yticks=np.arange(-1,1.25,0.25)>>> plt.yticks(yticks)>>> plt.xticks(xticks,['a','b','c','d','e','f','g','h'])>>> plt.xticks(xticks,[])>>> labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs','Frogs', 'Hogs', 'Bogs', 'Slogs']>>> plt.xticks(xticks,labels, rotation='vertical')#define axis range>>> plt.axis([0, 6, 0, 20])#background grids>>> plt.grid(True)#legend>>> plt.legend(['sin','cos'],loc=1,fontsize=20) # change location and font size of the legend.#text>>> plt.text(5,0.5,'text here') >>> plt.text(3,0.5,'text here',fontsize=20)>>> plt.text(3,0.5,'text here',fontsize=20,color='r')>>> plt.text(3,0.5,'text here',style='italic')>>> plt.text(3,0.5,'text here',fontweight='bold')>>> plt.text(3,0.5,'text here',bbox={'facecolor':'red', 'alpha':0.1, 'pad':10}) #alpha and pad control depth of shapes and pad size.>>> plt.text(5,0.5,'$E=cm^2$') >>> plt.text(5,0.5,r'$\alpha +\beta$') #using latex#arrow>>> plt.arrow(0, -0.5, 1.2, 0.3, head_width=0.05, head_length=0.2, fc='k', ec='k')#define figures>>> plt.figure(1)#subplot>>> plt.subplot(2,2,1)#figure layout>>> plt.tight_layout()>>> plt.tight_layout(pad=3)>>> plt.tight_layout(h_pad=2.5, w_pad=0.5)>>> plt.tight_layout(rect=[0.5, 0, 1, 1])#save figures>>> plt.savefig('plt.plot_example1.pdf')>>> plt.savefig('plt.plot_example1.png')#errorbar>>> yerr = np.std(y)>>> plt.errorbar(x, y, yerr)>>> plt.errorbar(x, y, xerr, yerr)#Asymmetric errorbars>>> plt.errorbar(x, y, yerr=[yerr, 2*yerr], fmt='ro',ecolor='g',elinewidth=3, capthick=3, capsize=5)# Histogram>>> plt.hist(x, num_bins, facecolor='green')>>> plt.hist(x, num_bins, facecolor='green', normed=1)>>> plt.hist(x, num_bins, facecolor='green', cumulative=1)>>> x = mu+sigma*np.random.randn(1000, 3)>>> n, bins, patches = plt.hist(x, num_bins, normed=1, histtype='bar', color=['crimson', 'burlywood', 'chartreuse'], label=['Group A', 'Group B', 'Group C'])#pie chart>>> plt.pie(x, explode=explode, labels=labels, colors=colors, autopct='%2.1f%%', shadow=True, startangle=90)#I/O>>> np.save(r"D:\Users\zhup\wrk\course\course10\2017\Lect06\maximums.npy", CXY)>>> CXY=np.load(r"D:\Users\zhup\wrk\course\course10\2017\Lect06\maximums.npy")>>> np.savez(r'D:\Users\zhup\wrk\course\course10\python\output\test1.npy', x,y)>>> npzfile=np.load(r"D:\Users\zhup\wrk\course\course10\2017\Lect06\test1.npy.npz")>>> npzfile.files>>>np.savetxt(r'D:\Users\zhup\wrk\course\course10\2017\Lect06\test2.txt',CXY)>>> npzfile=np.loadtxt(r'D:\Users\zhup\wrk\course\course10\2017\Lect06\test2.txt')>>> f=open(r'D:\Users\zhup\wrk\course\course10\2017\Lect06\test3.txt','wb')>>> np.savetxt(f,x)>>> np.savetxt(f,CXY.reshape(100,1))>>> np.savetxt(f,y)>>> f.close()>>> comb_array=np.loadtxt(r'D:\Users\zhup\wrk\course\course10\2017\Lect06\test2.txt')>>> f=open(r'D:\Users\zhup\wrk\course\course10\2017\Lect06\test3.txt','w')>>> f.write( "Python is a great language.\nYeah its great!!\n")>>> f.write("Art: %5d, price per Unit: %8.2f" % (453, 59.058))>>> f.close()>>> f=open(r'D:\Users\zhup\wrk\course\course10\2017\Lect06\test3.txt','r+')>>> f.read()'Python is a great language.\nYeah its great!!\nArt: 453, price per Unit: 59.06'>>> f.close()#Load Matlab data>>>import scipy.io>>>matfile=scipy.io.loadmat(r'D:\Users\zhup\wrk\analysis\ab_model\data\nond_2d_MK97.mat')>>> matfile{'t': array([[ ….'e': array([[ ….. 'r': array([[…>>> t=matfile['t']>>> e=matfile['e']>>> r=matfile['r']# Load Netcdf data>>> from scipy.io import netcdf>>>f=cdf_file(r'D:\Users\zhup\wrk\analysis\Edouard\data\radar_edouard_N43_1720.nc', 'r')>>> f.variables{'v': <scipy.cdf_variable object at 0x00000000055CA400>, 'lonc': <scipy.cdf_variable object at 0x00000000058D1FD0>, 'ws': <scipy.cdf_variable object at 0x00000000055CA550>, 'w': <scipy.cdf_variable object at 0x00000000055CA470>, 'lon': <scipy.cdf_variable object at 0x00000000058D4FD0>, 'lat': <scipy.cdf_variable object at 0x00000000058D4DA0>, 'latc': <scipy.cdf_variable object at 0x00000000058D4BE0>, 'dbz': <scipy.cdf_variable object at 0x00000000055CA4E0>, 'z': <scipy.cdf_variable object at 0x00000000055CA048>, 'u': <scipy.cdf_variable object at 0x00000000055CA4A8>}>>> dbz=f.variables['dbz']>>> dbz=np.array(list(dbz))>>> dbz.shape(99, 101, 37) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download