Skip to content

Commit

Permalink
adding labs
Browse files Browse the repository at this point in the history
  • Loading branch information
briandalessandro committed Sep 19, 2017
1 parent 6b08302 commit beac72f
Showing 1 changed file with 334 additions and 0 deletions.
334 changes: 334 additions & 0 deletions ipython/Labs_Student/Lab3_Pandas_Exploration_Student.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this lab we're going to do simple data exploration using Pandas. Our objective is to learn basic operations that aid in visual data exploration.\n",
"\n",
"<br>\n",
"\n",
"First, let's import our required libraries and read in the data."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import os\n",
"\n",
"\n",
"cwd = os.getcwd()\n",
"\n",
"#If on MAC, this will likely work\n",
"datadir = '/'.join(cwd.split('/')[0:-1]) + '/data/'\n",
"#If on window's machine, explicitly put in data dir\n",
"#datadir = \n",
"\n",
"%matplotlib inline\n",
"\n",
"\n",
"#Now read in the dataset loansdata.csv\n",
"loansData = "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The full schema can be found <a href=\"https://github.com/herrfz/dataanalysis/blob/master/assignment1/Assignment1.pdf\">here</a>, but let's use native Pandas methods to also explore the data. Although not specified above, this csv has row headers and the read_csv function implicitly knows this. <br>\n",
"\n",
"Let's take a look at the column names, in a nicely readable way:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"Column Names Are:\")\n",
"print(\"\")\n",
"\n",
"for column_name in loansData.columns.values:\n",
" print(column_name)\n",
"\n",
"print(\"\")\n",
"print('Total # of Columns = {}'.format(len(loansData.columns)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is oftentimes helpful to take a quick glance at the first few records (when possible). This is an easy way to discover basic data flaws (i.e., all nulls, misaligned fields, etc.). We can do this in Pandas very easily."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"loansData.head().transpose() #We transpose it so it will fit in the display window"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, let's run a quick line to get summary statistics of the numeric fields."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"loansData.describe().transpose()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's now focus on a single column, 'Monthly.Income.' First things first, let's display the first five records of just this field."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Student input line here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try and understand the distribution of this field. We can do this using the hist() method and matplotlib."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#plt.figure()\n",
"#hist_inc = loansData['Monthly.Income'].hist()\n",
"#plt.title('Histogram of Monthly Income')\n",
"#plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How would you characterize the shape of this distribution? Is there anything we can do to the income variable to make the distribution more bell curved? Let's create a new column in the dataframe called 'Monthly.LogIncome' and print a histogram of it. What might be some advantages of making such a transformation?\n",
"<br>\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Student: Add a new column Monthly.LogIncome to the dataset that is the log of the Monthly.Income columne"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#Now look at the distribution\n",
"plt.figure()\n",
"h = loansData['Monthly.LogIncome'].hist()\n",
"plt.title('Histogram of Log(Monthly Income)')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Now let's answer some questions about the data.\n",
"\n",
"<b>Q1: What is the cardinality (i.e., # of distinct values) for 'Interest.Rate' and 'FICO.Range'</b>"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Student input code here - hint, the 'describe()' method returns a useful dataframe\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Q2: What data type did Pandas set fo Interest.Rate? Can we create a new field which is stored as a float?</b>"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Student input code here - show the field type\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the cell below, create a new field 'Interest.Rate.Num' where 'Interest.Rate' is converted to a float. Hint: this can be done in one line using the .str assessor, strip and astype methods."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Student input code here - convert field to a float using the '.str' assessor\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Q3: Can we get a sense of the relationship between monthly income and interest rate? Use the scatter() function from Matplotlib.</b>"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Student input line here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given the skewed distribution of Income, is this chart visually helpful? Let's try the Log of income instead."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Student input code here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Q4: What is the average interest rate for each FICO range?</b> <br>\n",
"Hint: use the groupby() method in Pandas."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Student input code here\n",
"fico_grp = "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#fico_grp"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [py35]",
"language": "python",
"name": "Python [py35]"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit beac72f

Please sign in to comment.