Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mooc-rr
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
6c4b0fdbf8f14f2bf5b4f27e84287421
mooc-rr
Commits
2aa0ba45
Commit
2aa0ba45
authored
Mar 12, 2023
by
6c4b0fdbf8f14f2bf5b4f27e84287421
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
no commit message
parent
55a7d0ea
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
41 deletions
+28
-41
influenza-like-illness-analysis.ipynb
module3/exo1/influenza-like-illness-analysis.ipynb
+28
-41
No files found.
module3/exo1/influenza-like-illness-analysis.ipynb
View file @
2aa0ba45
...
@@ -2517,59 +2517,46 @@
...
@@ -2517,59 +2517,46 @@
}
}
],
],
"source": [
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import isoweek\n",
"import os\n",
"import os\n",
"import urllib\n",
"import urllib.request\n",
"import pandas as pd\n",
"\n",
"\n",
"data_file = \"incidence-PAY-3.csv\"\n",
"# Define the URL for the data\n",
"if not os.path.isfile(data_file):\n",
"url = 'https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/Data/ml-basics/crime.csv'\n",
" url = \"http://www.sentiweb.fr/datasets/incidence-PAY-3.csv\"\n",
" urllib.request.urlretrieve(url, data_file)\n",
"\n",
"\n",
"
raw_data = pd.read_csv(data_file, skiprows=1)
\n",
"
# Define the local filename
\n",
"
raw_data[raw_data.isnull().any(axis=1)]
\n",
"
local_data_file = 'crime.csv'
\n",
"\n",
"\n",
"data = raw_data.dropna().copy()\n",
"# If the local file doesn't exist, download the data and save to the local file\n",
"if not os.path.exists(local_data_file):\n",
" urllib.request.urlretrieve(url, local_data_file)\n",
"\n",
"\n",
"def convert_week(year_and_week_int):\n",
"# Read the data from the local file into a Pandas DataFrame\n",
" year_and_week_str = str(year_and_week_int)\n",
"raw_data = pd.read_csv(local_data_file)\n",
" year = int(year_and_week_str[:4])\n",
" week = int(year_and_week_str[4:])\n",
" w = isoweek.Week(year, week)\n",
" return pd.Period(w.day(0), 'W')\n",
"\n",
"\n",
"data['period'] = [convert_week(yw) for yw in data['week']]\n",
"# Drop the first column, which contains the crime codes\n",
"sorted_data = data.set_index('period').sort_index()\n",
"data = raw_data.drop(columns=['Offence Code'])\n",
"periods = sorted_data.index\n",
"\n",
"\n",
"for p1, p2 in zip(periods[:-1], periods[1:]):\n",
"# Convert the 'Reported Date' column to a Pandas datetime type\n",
" delta = p2.to_timestamp() - p1.end_time\n",
"data['Reported Date'] = pd.to_datetime(data['Reported Date'], format='%d/%m/%Y')\n",
" if delta > pd.Timedelta('1s'):\n",
" print(p1, p2)\n",
"\n",
"\n",
"sorted_data['inc'].plot()\n",
"# Filter the data to include only the weeks of interest\n",
"sorted_data['inc'][-200:].plot()\n",
"data = data[(data['Reported Date'] >= '1989-05-01') & (data['Reported Date'] <= '1989-05-07') | \n",
"first_august_week = [pd.Period(pd.Timestamp(y, 8, 1), 'W')\n",
" (data['Reported Date'] >= '1989-05-15') & (data['Reported Date'] <= '1989-05-21')]\n",
" for y in range(1985,\n",
" sorted_data.index[-1].year)]\n",
"year = []\n",
"yearly_incidence = []\n",
"\n",
"\n",
"for week1, week2 in zip(first_august_week[:-1],\n",
"# Compute the weekly crime incidence\n",
" first_august_week[1:]):\n",
"weekly_incidence = data.groupby(pd.Grouper(key='Reported Date', freq='W')).size()\n",
" one_year = sorted_data['inc'][week1:week2-1]\n",
"\n",
" assert abs(len(one_year)-52) < 2\n",
"# Compute the yearly crime incidence\n",
" yearly_incidence.append(one_year.sum())\n",
"yearly_incidence = weekly_incidence.groupby(weekly_incidence.index.year).sum()\n",
" year.append(week2.year)\n",
"year = yearly_incidence.index.astype(str)\n",
" \n",
"yearly_incidence.index = year\n",
"yearly_incidence = pd.Series(data=yearly_incidence, index=year)\n",
"\n",
"\n",
"# Plot the yearly crime incidence\n",
"yearly_incidence.plot(style='*')\n",
"yearly_incidence.plot(style='*')\n",
"yearly_incidence.sort_values()\n",
"yearly_incidence.sort_values()\n",
"yearly_incidence.hist(xrot=20)\n"
"yearly_incidence.hist(xrot=20)\n",
"\n"
]
]
},
},
{
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment