import pandas as pd
import numpy as np
Read in the dataset Datasets/SpreadingRates/km_age.txt as a Pandas DataFrame
Plot the age against distance; use age as the x-axis. Label your figure
Calculate the spreading rate by using the function np.polyfit( ) and a linear fit
Evaluate the linear fit using np.polyval( )
Plot the best fit line as a red dashed line along with the data
new_data=pd.read_csv('../../Datasets/redShiftDistance/mu_z.csv',header=1)
# function returns age in Ga for Ho
age_from_Ho= lambda Ho : 1e-9*3.09e19/(Ho*np.pi*1e7)
# convert z to velocity in 10^3 km/s (or 10^6 m/s)
c=3e8 # speed of light in m/s
new_data['velocity']=1e-6*c* \
(((new_data.z+1.)**2-1.)/((new_data.z+1.)**2+1.)) # the formula for v from z (and c)
# convert mu to distance in 10^3 Mpc (a Gpc):
new_data['distance']=10.*(10.**((new_data['mu'])/5.))*1e-9 # convert mu to Gpc
# and filter for the closest objects
close_data=new_data[new_data.distance<0.7]
close_fit= np.polyfit(close_data['distance'],close_data['velocity'],1) # calculate the coefficients
close_modelYs=np.polyval(close_fit,close_data['distance']) # get the model values
age=age_from_Ho(close_fit[0]) # get a new age
print (age)
16.865755533316488