From f0418c91a03d4d9ead531668c79f74dd410c98af Mon Sep 17 00:00:00 2001 From: Mayara Souza Date: Mon, 7 Nov 2022 21:47:55 +0100 Subject: [PATCH] incomplete --- your-code/main.ipynb | 603 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 569 insertions(+), 34 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 4128c1e..4b1dd12 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,7 +16,8 @@ "metadata": {}, "outputs": [], "source": [ - "# import numpy and pandas\n" + "import numpy as np\n", + "import pandas as pd" ] }, { @@ -30,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -48,11 +49,130 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
NameJob TitlesDepartmentFull or Part-TimeSalary or HourlyTypical HoursAnnual SalaryHourly Rate
0AARON, JEFFERY MSERGEANTPOLICEFSalaryNaN101442.0NaN
1AARON, KARINAPOLICE OFFICER (ASSIGNED AS DETECTIVE)POLICEFSalaryNaN94122.0NaN
2AARON, KIMBERLEI RCHIEF CONTRACT EXPEDITERGENERAL SERVICESFSalaryNaN101592.0NaN
3ABAD JR, VICENTE MCIVIL ENGINEER IVWATER MGMNTFSalaryNaN110064.0NaN
4ABASCAL, REECE ETRAFFIC CONTROL AIDE-HOURLYOEMCPHourly20.0NaN19.86
\n", + "
" + ], + "text/plain": [ + " Name Job Titles \\\n", + "0 AARON, JEFFERY M SERGEANT \n", + "1 AARON, KARINA POLICE OFFICER (ASSIGNED AS DETECTIVE) \n", + "2 AARON, KIMBERLEI R CHIEF CONTRACT EXPEDITER \n", + "3 ABAD JR, VICENTE M CIVIL ENGINEER IV \n", + "4 ABASCAL, REECE E TRAFFIC CONTROL AIDE-HOURLY \n", + "\n", + " Department Full or Part-Time Salary or Hourly Typical Hours \\\n", + "0 POLICE F Salary NaN \n", + "1 POLICE F Salary NaN \n", + "2 GENERAL SERVICES F Salary NaN \n", + "3 WATER MGMNT F Salary NaN \n", + "4 OEMC P Hourly 20.0 \n", + "\n", + " Annual Salary Hourly Rate \n", + "0 101442.0 NaN \n", + "1 94122.0 NaN \n", + "2 101592.0 NaN \n", + "3 110064.0 NaN \n", + "4 NaN 19.86 " + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "salaries.head()" ] }, { @@ -64,11 +184,61 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Name 0\n", + "Job Titles 0\n", + "Department 0\n", + "Full or Part-Time 0\n", + "Salary or Hourly 0\n", + "Typical Hours 25161\n", + "Annual Salary 8022\n", + "Hourly Rate 25161\n", + "dtype: int64" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "salaries.isna().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 33183 entries, 0 to 33182\n", + "Data columns (total 8 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Name 33183 non-null object \n", + " 1 Job Titles 33183 non-null object \n", + " 2 Department 33183 non-null object \n", + " 3 Full or Part-Time 33183 non-null object \n", + " 4 Salary or Hourly 33183 non-null object \n", + " 5 Typical Hours 8022 non-null float64\n", + " 6 Annual Salary 25161 non-null float64\n", + " 7 Hourly Rate 8022 non-null float64\n", + "dtypes: float64(3), object(5)\n", + "memory usage: 2.0+ MB\n" + ] + } + ], + "source": [ + "salaries.info()" ] }, { @@ -80,12 +250,42 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "8022" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(salaries[salaries[\"Salary or Hourly\"]==\"Hourly\"])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "25161" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "len(salaries[salaries[\"Salary or Hourly\"]==\"Salary\"])\n" ] }, { @@ -104,11 +304,58 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Department\n", + "ADMIN HEARNG 39\n", + "ANIMAL CONTRL 81\n", + "AVIATION 1629\n", + "BOARD OF ELECTION 107\n", + "BOARD OF ETHICS 8\n", + "BUDGET & MGMT 46\n", + "BUILDINGS 269\n", + "BUSINESS AFFAIRS 171\n", + "CITY CLERK 84\n", + "CITY COUNCIL 411\n", + "COMMUNITY DEVELOPMENT 207\n", + "COPA 116\n", + "CULTURAL AFFAIRS 65\n", + "DISABILITIES 28\n", + "DoIT 99\n", + "FAMILY & SUPPORT 615\n", + "FINANCE 560\n", + "FIRE 4641\n", + "GENERAL SERVICES 980\n", + "HEALTH 488\n", + "HUMAN RELATIONS 16\n", + "HUMAN RESOURCES 79\n", + "INSPECTOR GEN 87\n", + "LAW 407\n", + "LICENSE APPL COMM 1\n", + "MAYOR'S OFFICE 85\n", + "OEMC 2102\n", + "POLICE 13414\n", + "POLICE BOARD 2\n", + "PROCUREMENT 92\n", + "PUBLIC LIBRARY 1015\n", + "STREETS & SAN 2198\n", + "TRANSPORTN 1140\n", + "TREASURER 22\n", + "WATER MGMNT 1879\n", + "Name: Name, dtype: int64" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "salaries.groupby(\"Department\")[\"Name\"].count()" ] }, { @@ -122,11 +369,85 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "from scipy.stats import ttest_ind, norm, t, ttest_1samp\n", + "from statsmodels.stats.weightstats import ztest as ztest\n", + "\n", + "from scipy.stats import f_oneway" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4 19.86\n", + "6 46.10\n", + "7 35.60\n", + "10 2.65\n", + "18 17.68\n", + " ... \n", + "33164 46.10\n", + "33168 17.68\n", + "33169 35.60\n", + "33174 46.35\n", + "33175 48.85\n", + "Name: Hourly Rate, Length: 8022, dtype: float64" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mean_hourly = salaries[\"Hourly Rate\"][salaries[\"Salary or Hourly\"]==\"Hourly\"]\n", + "mean_hourly" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.3230240486229894e-92" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stat_t, p_values = ttest_1samp(mean_hourly, 30, alternative='two-sided')\n", + "p_values" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There's a difference\n" + ] + } + ], + "source": [ + "if p_values < 0.05:\n", + " print(\"There's a difference\")" ] }, { @@ -140,11 +461,73 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0 101442.0\n", + "1 94122.0\n", + "2 101592.0\n", + "3 110064.0\n", + "5 50436.0\n", + " ... \n", + "33178 72510.0\n", + "33179 48078.0\n", + "33180 90024.0\n", + "33181 93354.0\n", + "33182 115932.0\n", + "Name: Annual Salary, Length: 25161, dtype: float64" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "mean_salary = salaries[\"Annual Salary\"][salaries[\"Salary or Hourly\"]==\"Salary\"]\n", + "mean_salary" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.507879259958193e-09" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stat_t, p_value = ttest_1samp(mean_salary, 86000, alternative=\"greater\")\n", + "p_value" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There's a difference\n" + ] + } + ], + "source": [ + "if p_values < 0.025: #one sided, alpha/2\n", + " print(\"There's a difference\")" ] }, { @@ -156,11 +539,22 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'STREETS & SAN'" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "pd.crosstab(salaries[\"Department\"], salaries[\"Salary or Hourly\"])[\"Hourly\"].idxmax()" ] }, { @@ -172,11 +566,73 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7 35.60\n", + "21 21.43\n", + "24 35.60\n", + "36 36.21\n", + "39 35.60\n", + " ... \n", + "33106 36.13\n", + "33107 35.60\n", + "33147 35.60\n", + "33149 36.21\n", + "33156 22.12\n", + "Name: Hourly Rate, Length: 1862, dtype: float64" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "dept_hourly = salaries[\"Hourly Rate\"].loc[(salaries[\"Department\"]== 'STREETS & SAN') & (salaries[\"Salary or Hourly\"] == \"Hourly\")]\n", + "dept_hourly" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.6689265282353859e-21" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stat_t, p_value = ttest_1samp(dept_hourly, 35, alternative=\"less\")\n", + "p_value" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The salary is less than 35/h\n" + ] + } + ], + "source": [ + "if p_value < 0.025: #one sided, alpha/2\n", + " print(\"The salary is less than 35/h\")" ] }, { @@ -200,11 +656,44 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.1352284271240131" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "empirical_standard_error = np.std(mean_hourly) / np.sqrt(len(mean_hourly))\n", + "empirical_standard_error" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(30.82829793032407, 34.748817502236406)" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ci = t.interval(confidence = 0.95, df = len(mean_hourly)-1, loc = np.mean(mean_hourly))\n", + "ci" ] }, { @@ -216,11 +705,44 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "132.64812515894783" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "empirical_standard_error2 = np.std(mean_salary) / np.sqrt(len(mean_salary))\n", + "empirical_standard_error2" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(86785.03973187502, 86788.95984842784)" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ci_salary = t.interval(confidence = 0.95, df = len(mean_salary)-1, loc = np.mean(mean_salary))\n", + "ci_salary" ] }, { @@ -248,7 +770,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -262,7 +784,20 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.4" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false } }, "nbformat": 4,