#Python is an open source programming language. An easy way to get started #with Python is to install a prebuilt distribution of Python, such as Anaconda. # #http://continuum.io/downloads#py34 # #Note that we are using Python 3 rather than Python 2. Also, make sure to get #the version that is appropriate for your system (32 bit or 64 bit). # #Anaconda includes an IDE (integrated development environment/front end) called #Spyder. Spyder is a front end for Python in the same way Rstudio is a front #end for R. # #Like Rstudio, Spyder has a script window and a console window, and we can #execute commands from the script window using F9 (and a variety of other ways). ################################################### #SUMMARY OF MAJOR DIFFERENCES BETWEEN PYTHON AND R# ################################################### #In R, ^ is the exponentiation operation. #In Python, ** is exponentiation, and ^ is the bitwise XOR operation. #In R, the first component of a vector x is x[1] #In Python, the zeroth component of a vector x is x[0] #In Python, indentation matters in if then statements. ################## #BASIC ARITHMETIC# ################## x=5 y=2 x+y x-y x*y x/y x**y #Exponentiation, as in SAS help("**") #Getting information with the help command #WARNING: In Python, ^ is actually the bitwise XOR command, not exponentiation. #Example: x=12 #which is 1100 in binary y=10 #which is 1010 in binary x^y #is 6 0110 in binary #To see a list of variables you have defined, use the variable explorer #window in the top right corner. #To delete a variable, use the del command. a=1 a del a a ############################ #MODULES, NUMPY, and ARRAYS# ############################ #Packages in Python are called modules. The module NumPy will allow us to #work with arrays efficiently. #NumPy tutorial: http://wiki.scipy.org/Tentative_NumPy_Tutorial from numpy import * #Imports all functions from numpy module. A=array([[1,2],[3,4]]) A A[0,0] A[1,0] A[0,1] A[1,1] B=array([[4,0],[-2,5]]) B #Elementwise operations. A+B A-B A*B A/B A+2 A**2 #Matrix operations dot(A,B) #Matrix multiplication linalg.inv(A) #Inverse dot(A,linalg.inv(A)) #Verifying that this is an inverse transpose(A) #Transpose C=linalg.solve(A,B) #inv(A) times B dot(A,C) A.diagonal() #Diagonal of A #Stacking Matrices hstack((A,B)) #Note the double parentheses here. vstack((A,B)) #Creating arrays ones([3,5]) zeros([3,5]) identity(3,float) #Here we are specifying data type = float diag([9,10]) #Diagonal matrix arange(20,40,5) #40 not included arange(20,41,5) #40 is included A=arange(12) A A=arange(12).reshape(3,4) A A.shape #Returns number of rows and columns. A.size #Total number of entries in A. A.dtype #Data type (in this case, 32-bit integer) A.diagonal() A.diagonal(offset=1) A.diagonal(offset=-1) #Rows of A A[0,:] A[1,:] A[2,:] A[[0,2],:] #Columns of A A[:,0] A[:,1] A[:,2] A[:,3] A[:,[0,2,3]] #Indexing x=arange(10)**2 x x[0] #0th component x[2] #2nd component x[2:5] #2nd through 4th components x[-1] #Last component x[-2] #Second to last component x[:5] #0th through 4th components x[5:] #5th through 9th components index=(x>=40) index x[index] #################### #LOGICAL OPERATIONS# #################### 3==3 3==5 3!=5 3<5 3<=5 3>5 3>=3 (3<5) and (4>7) (3<5) or (4>7) not(3<5) ########################## #SIMPLE PROGRAMMING TOOLS# ########################## #Example 1 if (3<5): print("Hooray!") #Example 2 if (3>5): print("Hooray!") #Example 3 if (3<5): print("Three is less than five") print("It really is") print("Here's a statement that isn't indented") print("Indentation matters in Python") #Example 4 if (3>5): print("Three is less than five") print("It really is") print("Here's a statement that isn't indented") print("Indentation matters in Python") #Example 5 if(3<5): print("I like dogs") else: print("I like cats") print("This line isn't indented") #Example 6 if(3>5): print("I like dogs") else: print("I like cats") print("This line isn't indented") #For Loops x=arange(10) for i in x: print(x[i]) #Functions def mysum(x,y): return x+y mysum(5,9) ########## #PLOTTING# ########## from matplotlib import * x=arange(10) y=x**2 pyplot.plot(x,y) #PyPlot documentation: http://matplotlib.org/api/pyplot_api.html ###################### #STATISTICAL COMMANDS# ###################### x=numpy.random.normal(500,100,1000) mean(x) std(x) from numpy import * x=random.normal(500,100,1000) mean(x) std(x) min(x) max(x) sort(x) from pylab import * pylab.hist(x) hist(x) x=numpy.random.uniform(low=0,high=1,size=1000) mean(x) std(x) 1/sqrt(12) hist(x) #Plotting a scatterplot with a regression curve. x=random.normal(100,15,1000) epsilon=random.normal(0,5,1000) y=x+epsilon plot(x,y) #Execute this by itself plot(x,y,'.',color="black") #Execute this by itself plot(sort(x),sort(x),'-',color="red") #Execute these at the same time plot(x,y,'.',color="black") plot(sort(x),sort(x),'-',color="red") ################ #IMPORTING DATA# ################ from numpy import * mydata=genfromtxt( "T:\Fall 2014\Math 5364\Python Experimentation\math5305Lab2Data.txt", dtype=float, delimiter=",") mydata