{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Practice Problems\n", "### Lecture 16\n", "Answer each number in a separate cell\n", "\n", "Rename the notebook with your lastName first initial and the lecture number\n", " \n", " ex. Cych_B_16\n", " \n", "Turn this notebook into Canvas" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# 1. Linear regression\n", "- Read in the dataset **Datasets/SpreadingRates/km_age.txt** as a Pandas DataFrame \n", " - Each data point is the age and distance from the South Atlantic ridge crests to a particular magnetic anomaly. The ages (Ma) were published by Gradstein et al. (2004) \n", "- Plot the age against distance; use age as the x-axis. Label your figure\n", "\n", "- Calculate the spreading rate by using the function **np.polyfit( )** and a linear fit\n", "- Evaluate the linear fit using **np.polyval( )**\n", "- Plot the best fit line as a red dashed line along with the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Lambda functions\n", "- Write a lambda function that converts z to velocity\n", "- Write a second lambda function that converts mu to distance\n", "- Rewrite the code cell that follows to call your new functions" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16.865755533316488\n" ] } ], "source": [ "new_data=pd.read_csv('../../Datasets/redShiftDistance/mu_z.csv',header=1)\n", "\n", "# function returns age in Ga for Ho\n", "age_from_Ho= lambda Ho : 1e-9*3.09e19/(Ho*np.pi*1e7)\n", "\n", "# convert z to velocity in 10^3 km/s (or 10^6 m/s)\n", "c=3e8 # speed of light in m/s\n", "new_data['velocity']=1e-6*c* \\\n", " (((new_data.z+1.)**2-1.)/((new_data.z+1.)**2+1.)) # the formula for v from z (and c)\n", "# convert mu to distance in 10^3 Mpc (a Gpc):\n", "new_data['distance']=10.*(10.**((new_data['mu'])/5.))*1e-9 # convert mu to Gpc\n", "# and filter for the closest objects\n", "close_data=new_data[new_data.distance<0.7]\n", "\n", "close_fit= np.polyfit(close_data['distance'],close_data['velocity'],1) # calculate the coefficients\n", "close_modelYs=np.polyval(close_fit,close_data['distance']) # get the model values\n", "\n", "age=age_from_Ho(close_fit[0]) # get a new age \n", "print (age)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Curve Fitting\n", "\n", "- generate a set of data from 0 to 720 degrees at a spacing of 10 degree intervals.\n", "- convert it to radians\n", "- make a cosine wave from your radians array. \n", "- create a normal distribution generator with **stats.norm( )** that has a mean of 0 and a standard deviation of .2.\n", "- generate a set of simulated normally distributed \"noise\" that has the same number of data points as your cosine curve. \n", "- add your \"noise\" to your cosine curve. \n", "- use **np.polyfit** and **np.polyval** to fit a polynomial curve to your noisy cosine data (use degrees 5 to 8). \n", "- Make a plot of the data with the opacity set to .5.\n", "- Plot each fit (using a different colored line) and add a legend to the right of the plot using with the degree as the label. Hint: use the **bbox\\_to\\_anchor( )** method. \n", "- Add the cosine curve to your plot. \n", "- Which degree fits the best without over fitting? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }