{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Read in the data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy\n", "import re\n", "\n", "data_files = [\n", " \"ap_2010.csv\",\n", " \"class_size.csv\",\n", " \"demographics.csv\",\n", " \"graduation.csv\",\n", " \"hs_directory.csv\",\n", " \"sat_results.csv\"\n", "]\n", "\n", "data = {}\n", "\n", "for f in data_files:\n", " d = pd.read_csv(\"schools/{0}\".format(f))\n", " data[f.replace(\".csv\", \"\")] = d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Read in the surveys" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "all_survey = pd.read_csv(\"schools/survey_all.txt\", delimiter=\"\\t\", encoding='windows-1252')\n", "d75_survey = pd.read_csv(\"schools/survey_d75.txt\", delimiter=\"\\t\", encoding='windows-1252')\n", "survey = pd.concat([all_survey, d75_survey], axis=0)\n", "\n", "survey[\"DBN\"] = survey[\"dbn\"]\n", "\n", "survey_fields = [\n", " \"DBN\", \n", " \"rr_s\", \n", " \"rr_t\", \n", " \"rr_p\", \n", " \"N_s\", \n", " \"N_t\", \n", " \"N_p\", \n", " \"saf_p_11\", \n", " \"com_p_11\", \n", " \"eng_p_11\", \n", " \"aca_p_11\", \n", " \"saf_t_11\", \n", " \"com_t_11\", \n", " \"eng_t_10\", \n", " \"aca_t_11\", \n", " \"saf_s_11\", \n", " \"com_s_11\", \n", " \"eng_s_11\", \n", " \"aca_s_11\", \n", " \"saf_tot_11\", \n", " \"com_tot_11\", \n", " \"eng_tot_11\", \n", " \"aca_tot_11\",\n", "]\n", "survey = survey.loc[:,survey_fields]\n", "data[\"survey\"] = survey" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Add DBN columns" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data[\"hs_directory\"][\"DBN\"] = data[\"hs_directory\"][\"dbn\"]\n", "\n", "def pad_csd(num):\n", " string_representation = str(num)\n", " if len(string_representation) > 1:\n", " return string_representation\n", " else:\n", " return \"0\" + string_representation\n", " \n", "data[\"class_size\"][\"padded_csd\"] = data[\"class_size\"][\"CSD\"].apply(pad_csd)\n", "data[\"class_size\"][\"DBN\"] = data[\"class_size\"][\"padded_csd\"] + data[\"class_size\"][\"SCHOOL CODE\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Convert columns to numeric" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cols = ['SAT Math Avg. Score', 'SAT Critical Reading Avg. Score', 'SAT Writing Avg. Score']\n", "for c in cols:\n", " data[\"sat_results\"][c] = pd.to_numeric(data[\"sat_results\"][c], errors=\"coerce\")\n", "\n", "data['sat_results']['sat_score'] = data['sat_results'][cols[0]] + data['sat_results'][cols[1]] + data['sat_results'][cols[2]]\n", "\n", "def find_lat(loc):\n", " coords = re.findall(\"\\(.+, .+\\)\", loc)\n", " lat = coords[0].split(\",\")[0].replace(\"(\", \"\")\n", " return lat\n", "\n", "def find_lon(loc):\n", " coords = re.findall(\"\\(.+, .+\\)\", loc)\n", " lon = coords[0].split(\",\")[1].replace(\")\", \"\").strip()\n", " return lon\n", "\n", "data[\"hs_directory\"][\"lat\"] = data[\"hs_directory\"][\"Location 1\"].apply(find_lat)\n", "data[\"hs_directory\"][\"lon\"] = data[\"hs_directory\"][\"Location 1\"].apply(find_lon)\n", "\n", "data[\"hs_directory\"][\"lat\"] = pd.to_numeric(data[\"hs_directory\"][\"lat\"], errors=\"coerce\")\n", "data[\"hs_directory\"][\"lon\"] = pd.to_numeric(data[\"hs_directory\"][\"lon\"], errors=\"coerce\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Condense datasets" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class_size = data[\"class_size\"]\n", "class_size = class_size[class_size[\"GRADE \"] == \"09-12\"]\n", "class_size = class_size[class_size[\"PROGRAM TYPE\"] == \"GEN ED\"]\n", "\n", "class_size = class_size.groupby(\"DBN\").agg(numpy.mean)\n", "class_size.reset_index(inplace=True)\n", "data[\"class_size\"] = class_size\n", "\n", "data[\"demographics\"] = data[\"demographics\"][data[\"demographics\"][\"schoolyear\"] == 20112012]\n", "\n", "data[\"graduation\"] = data[\"graduation\"][data[\"graduation\"][\"Cohort\"] == \"2006\"]\n", "data[\"graduation\"] = data[\"graduation\"][data[\"graduation\"][\"Demographic\"] == \"Total Cohort\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Convert AP scores to numeric" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cols = ['AP Test Takers ', 'Total Exams Taken', 'Number of Exams with scores 3 4 or 5']\n", "\n", "for col in cols:\n", " data[\"ap_2010\"][col] = pd.to_numeric(data[\"ap_2010\"][col], errors=\"coerce\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Combine the datasets" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "combined = data[\"sat_results\"]\n", "\n", "combined = combined.merge(data[\"ap_2010\"], on=\"DBN\", how=\"left\")\n", "combined = combined.merge(data[\"graduation\"], on=\"DBN\", how=\"left\")\n", "\n", "to_merge = [\"class_size\", \"demographics\", \"survey\", \"hs_directory\"]\n", "\n", "for m in to_merge:\n", " combined = combined.merge(data[m], on=\"DBN\", how=\"inner\")\n", "\n", "combined = combined.fillna(combined.mean())\n", "combined = combined.fillna(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Add a school district column for mapping" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_first_two_chars(dbn):\n", " return dbn[0:2]\n", "\n", "combined[\"school_dist\"] = combined[\"DBN\"].apply(get_first_two_chars)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Find correlations" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "correlations = combined.corr()\n", "correlations = correlations[\"sat_score\"]\n", "print(correlations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting survey correlations" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "combined.corr()[\"sat_score\"][survey_fields].plot.bar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are high correlations between `N_s`, `N_t`, `N_p` and `sat_score`. Since these columns are correlated with `total_enrollment`, it makes sense that they would be high. \n", "\n", "It is more interesting that `rr_s`, the student response rate, or the percentage of students that completed the survey, correlates with `sat_score`. This might make sense because students who are more likely to fill out surveys may be more likely to also be doing well academically.\n", "\n", "How students and teachers percieved safety (`saf_t_11` and `saf_s_11`) correlate with `sat_score`. This make sense, as it's hard to teach or learn in an unsafe environment.\n", "\n", "The last interesting correlation is the `aca_s_11`, which indicates how the student perceives academic standards, correlates with `sat_score`, but this is not true for `aca_t_11`, how teachers perceive academic standards, or `aca_p_11`, how parents perceive academic standards." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exploring safety" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "combined.plot.scatter(\"saf_s_11\", \"sat_score\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There appears to be a correlation between SAT scores and safety, although it isn't thatstrong. It looks like there are a few schools with extremely high SAT scores and high safety scores. There are a few schools with low safety scores and low SAT scores. No school with a safety score lower than `6.5` has an average SAT score higher than 1500 or so." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting safety" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from mpl_toolkits.basemap import Basemap\n", "\n", "districts = combined.groupby(\"school_dist\").agg(numpy.mean)\n", "districts.reset_index(inplace=True)\n", "\n", "m = Basemap(\n", " projection='merc', \n", " llcrnrlat=40.496044, \n", " urcrnrlat=40.915256, \n", " llcrnrlon=-74.255735, \n", " urcrnrlon=-73.700272,\n", " resolution='i'\n", ")\n", "\n", "m.drawmapboundary(fill_color='#85A6D9')\n", "m.drawcoastlines(color='#6D5F47', linewidth=.4)\n", "m.drawrivers(color='#6D5F47', linewidth=.4)\n", "m.fillcontinents(color='white',lake_color='#85A6D9')\n", "\n", "longitudes = districts[\"lon\"].tolist()\n", "latitudes = districts[\"lat\"].tolist()\n", "m.scatter(longitudes, latitudes, s=50, zorder=2, latlon=True, c=districts[\"saf_s_11\"], cmap=\"summer\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looks like Upper Manhattan and parts of Queens and the Bronx tend to have lower safety scores, whereas Brooklyn has high safety scores." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Racial differences in SAT scores" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [], "source": [ "race_fields = [\"white_per\", \"asian_per\", \"black_per\", \"hispanic_per\"]\n", "combined.corr()[\"sat_score\"][race_fields].plot.bar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looks like a higher percentage of white or asian students at a school correlates positively with sat score, whereas a higher percentage of black or hispanic students correlates negatively with sat score. This may be due to a lack of funding for schools in certain areas, which are more likely to have a higher percentage of black or hispanic students." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [], "source": [ "combined.plot.scatter(\"hispanic_per\", \"sat_score\")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(combined[combined[\"hispanic_per\"] > 95][\"SCHOOL NAME\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The schools listed above appear to primarily be geared towards recent immigrants to the US. These schools have a lot of students who are learning English, which would explain the lower SAT scores." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(combined[(combined[\"hispanic_per\"] < 10) & (combined[\"sat_score\"] > 1800)][\"SCHOOL NAME\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many of the schools above appear to be specialized science and technology schools that receive extra funding, and only admit students who pass an entrance exam. This doesn't explain the low `hispanic_per`, but it does explain why their students tend to do better on the SAT -- they are students from all over New York City who did well on a standardized test." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Gender differences in SAT scores" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "gender_fields = [\"male_per\", \"female_per\"]\n", "combined.corr()[\"sat_score\"][gender_fields].plot.bar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the plot above, we can see that a high percentage of females at a school positively correlates with SAT score, whereas a high percentage of males at a school negatively correlates with SAT score. Neither correlation is extremely strong." ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [], "source": [ "combined.plot.scatter(\"female_per\", \"sat_score\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Based on the scatterplot, there doesn't seem to be any real correlation between `sat_score` and `female_per`. However, there is a cluster of schools with a high percentage of females (`60` to `80`), and high SAT scores." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(combined[(combined[\"female_per\"] > 60) & (combined[\"sat_score\"] > 1700)][\"SCHOOL NAME\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These schools appears to be very selective liberal arts schools that have high academic standards." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# AP Exam Scores vs SAT Scores" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [], "source": [ "combined[\"ap_per\"] = combined[\"AP Test Takers \"] / combined[\"total_enrollment\"]\n", "\n", "combined.plot.scatter(x='ap_per', y='sat_score')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looks like there is a relationship between the percentage of students in a school who take the AP exam, and their average SAT scores. It's not an extremely strong correlation, though." ] } ], "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.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }