Data Management and Visualization: Week 4 Assignment
Creating your own graphs :SAS and Python
SAS: Code
Output:
Python: Code
#importing Pandas and NumPy libraries import pandas import numpy import seaborn import matplotlib.pyplot as plt #Loading the Mars Crater dataset DATA = pandas.read_csv('marscrater_pds.csv',low_memory=False) pandas.set_option('display.max_columns',None) pandas.set_option('display.max_rows',None) #Printing the number of rows and coloums in the dataset print(len(DATA)) print(len(DATA.columns)) #Converting the value of the object into numeric DATA['LATITUDE_CIRCLE_IMAGE']= pandas.to_numeric(DATA['LATITUDE_CIRCLE_IMAGE']) #defining a new variable in order to divide the latitude into three groups, viz >25, <-25, and -25<0<25 def LAT (row): if row['LATITUDE_CIRCLE_IMAGE'] >25: return 1 if row['LATITUDE_CIRCLE_IMAGE'] < -25: return 2 else: return 0 DATA['LAT']= DATA.apply(lambda row:LAT(row),axis=1) #Printing the number of rows and coloums in the dataset print(len(DATA)) print(len(DATA.columns)) #subsetting to obtain the specific data required ie the data coming in the range of latitude -25 to +25 EQTR=DATA[(DATA['LAT']==0)] EQTR2=EQTR.copy() #Printing the number of rows and coloums in the dataset print(len(EQTR2)) print(len(EQTR2.columns)) #Converting the value of the object into numeric EQTR2['DIAM_CIRCLE_IMAGE']= pandas.to_numeric(EQTR2['DIAM_CIRCLE_IMAGE']) EQTR2['DEPTH_RIMFLOOR_TOPOG']= pandas.to_numeric(EQTR2['DEPTH_RIMFLOOR_TOPOG']) EQTR2['NUMBER_LAYERS']= pandas.to_numeric(EQTR2['NUMBER_LAYERS']) #Omitting missing data EQTR2['DEPTH_RIMFLOOR_TOPOG']=EQTR['DEPTH_RIMFLOOR_TOPOG'].replace(0, numpy.nan) EQTR2['NUMBER_LAYERS']=EQTR['NUMBER_LAYERS'].replace(0, numpy.nan) #Data management of latitude variable along with frequency and precentages EQTR2['EQR']= pandas.cut(EQTR2.LATITUDE_CIRCLE_IMAGE,[-25,-15,-5,5,15,25]) EQTR2['EQR']=EQTR2['EQR'].astype('category') print("Latitude grouped") FREQ1=EQTR2['EQR'].value_counts(sort=True) print(FREQ1) PER1=EQTR2['EQR'].value_counts(sort=True, normalize=True)*100 print(PER1) #Data management of diameter variable along with frequency and precentages EQTR2['DIA']= pandas.cut(EQTR2.DIAM_CIRCLE_IMAGE,[1,1.5,2.5,3.5,4.5,1096.65]) EQTR2['DIA']=EQTR2['DIA'].astype('category') print("Diameter of craters in equatorial region grouped") FREQ2=EQTR2['DIA'].value_counts(sort=True) print(FREQ2) PER2=EQTR2['DIA'].value_counts(sort=True, normalize=True)*100 print(PER2) #Data management of depth variable along with frequency and precentages EQTR2['DPTH']= pandas.cut(EQTR2.DEPTH_RIMFLOOR_TOPOG,[0.1,0.2,0.4,0.6,0.8,4.95]) EQTR2['DPTH']=EQTR2['DPTH'].astype('category') print("Depth of craters in equatorial region grouped") FREQ3=EQTR2['DPTH'].value_counts(sort=True) print(FREQ3) PER3=EQTR2['DPTH'].value_counts(sort=True, normalize=True)*100 print(PER3) #Summary statisstics of variables print("SUMMARY STATISTICS OF VARIABLES") print("Diameter of craters at equatorial region") stat1=EQTR2['DIAM_CIRCLE_IMAGE'].describe() print(stat1) print("median") mdn1=EQTR2['DIAM_CIRCLE_IMAGE'].median() print(mdn1) print("mode") md1=EQTR2['DIAM_CIRCLE_IMAGE'].mode() print(md1) print("Depths of craters in the equatorial region") stat2=EQTR2['DEPTH_RIMFLOOR_TOPOG'].describe() print(stat2) print("median") mdn2=EQTR2['DEPTH_RIMFLOOR_TOPOG'].median() print(mdn2) print("mode") md2=EQTR2['DEPTH_RIMFLOOR_TOPOG'].mode() print(md2) print("Number of ejecta layers on craters in the eqiatorial region") stat3=EQTR2['NUMBER_LAYERS'].describe() print(stat3) print("median") mdn3=EQTR2['NUMBER_LAYERS'].median() print(mdn3) print("mode") md3=EQTR2['NUMBER_LAYERS'].mode() print(md3) #Univariate graphs EQTR2['NUMBER_LAYERS']=EQTR2['NUMBER_LAYERS'].astype('category') seaborn.countplot(x="NUMBER_LAYERS", data=EQTR2) plt.xlabel('Number of ejecta layers') plt.title('Number of ejecta layers on craters in the Equatorial region of Mars surface') plt.show() seaborn.distplot(EQTR2['DIAM_CIRCLE_IMAGE'].dropna(), kde=False) plt.xlabel('Diameter of the craters in kms') plt.title('Distribution plot of the diamaters of the craters in the equtorial region') plt.show() seaborn.countplot(x="DIA", data=EQTR2) plt.xlabel('Diameter of the craters in kms') plt.title('Histogram of the diamaters of the craters grouped in the equtorial region') plt.show() seaborn.distplot(EQTR2['DEPTH_RIMFLOOR_TOPOG'].dropna(), kde=False) plt.xlabel('Depths of the craters in kms') plt.title('Distribution plot of the depths of the craters in the equtorial region') plt.show() seaborn.countplot(x="DPTH", data=EQTR2) plt.xlabel('Depths of the craters in kms') plt.title('Histogram of the depths of the craters grouped in the equtorial region') plt.show() #Bivariate graphs scat1=seaborn.regplot(x="LATITUDE_CIRCLE_IMAGE", y="DIAM_CIRCLE_IMAGE", fit_reg=False, data=EQTR2) plt.xlabel('Latitude') plt.ylabel('Diameter of craters in equatorial region') plt.title('scatter Plot: Latitude vs Diameters of craters') plt.show() scat2=seaborn.regplot(x="LATITUDE_CIRCLE_IMAGE", y="DEPTH_RIMFLOOR_TOPOG", fit_reg=False, data=EQTR2) plt.xlabel('Latitude') plt.ylabel('Depth of craters in equatorial region') plt.title('scatter Plot: Latitude vs Depth of craters') plt.show() seaborn.factorplot(x="EQR", y="DIAM_CIRCLE_IMAGE", data=EQTR2, kind="bar",ci=None) plt.xlabel('Latitude') plt.ylabel('Diameter of craters in equatorial region') plt.title('Bar Plot: Latitude vs Diameters of craters') plt.show() seaborn.factorplot(x="EQR", y="DEPTH_RIMFLOOR_TOPOG", data=EQTR2, kind="bar",ci=None) plt.xlabel('Latitude') plt.ylabel('Depth of craters in equatorial region') plt.title('Bar Plot: Latitude vs Depth of craters') plt.show()
Output:
The variables are grouped for better data management and plotting purposes. The summary statistics of the three variables of interest are found. It can be seen that the mean diameter of the craters at the equatorial region is 3.35 kms. This is in agreement with the hypothesis proposed at the beginning of the course that is the diameter of craters at the equatorial region are all small and is less than 8 kms. Similarly the depth (elevation) of the craters are also a low value that is 0.45 km. This can be connected to fact that most of the craters are having only 1 layer of ejecta.
It can be clearly seen from the histograms that all variables have right skewed distribution with the depth variable having a bi-modal right skewed nature. The variables are grouped into categories to find this.
Scatter plots are created with diameter and depth of craters as the response variables(y) and latitude as explanatory variables(x). While examining the scatter plot a relationship cannot be determined between the variable. So the variables are grouped and it can be concluded that the Diameter and latitude have a zigzag relationship. At the equatorial region its showing a bi-modal trend. The depth and latitude have a negative linear relationship in this region of mars.







