hw3
.pdf
keyboard_arrow_up
School
Northeastern University *
*We aren’t endorsed by this school
Course
3000
Subject
Industrial Engineering
Date
Feb 20, 2024
Type
Pages
14
Uploaded by ngocminhphan02
2/8/24, 8
:
33 PM
hw3
Page 1 of 14
about:srcdoc
DS 3000 HW 3
Due: Thursday Feb 8th @ 11
:
59 PM EST
Submission Instructions
Submit this ipynb
file and the a PDF
file included with the coding results to
Gradescope (this can also be done via the assignment on Canvas). To ensure that your
submitted files represent your latest code, make sure to give a fresh Kernel > Restart & Run All
just before uploading the files to gradescope.
Tips for success
Start early (even though you have two weeks on this homework)
Make use of Piazza
Make use of Office hour
Remember to use cells and headings to make the notebook easy to read (if a grader
cannot find the answer to a problem, you will receive no points for it)
Under no circumstances may one student view or share their ungraded homework
or quiz with another student (see also)
, though you are welcome to talk about
(not
show each other) the problems.
Part 1: Plotting Warm Up (18 points)
Plot each of the functions below over 100 evenly spaced points in the domain $
[0, 10]
$
on the same
graph.
Be sure to use the line specifications given below:
Name
Value
Color
Line Width
Style
sinusoid
3 * sin (2/3 x)
Red
4
dotted
polynomial
(x-3) (x - 2) (x-8) / 10
Blue
2
solid
abs value
min(abs(x - 3), abs(x - 8))
Green
3
dashed
add a legend which specifies the name of each function
use seaborn's sns.set()
before plotting to make the graph look nice
2/8/24, 8
:
33 PM
hw3
Page 2 of 14
about:srcdoc
Make sure that the axes are labeled x
and f(x)
You may find the arithmetic functions needed in numpy (sin, abs, minimum)
import
numpy as
np
import
matplotlib.pyplot as
plt
import
seaborn as
sns
sns
.
set
()
x =
np
.
linspace
(
0
, 10
, 100
)
plt
.
figure
(
figsize
=
(
10
, 6
))
plt
.
plot
(
x
, 3 *
np
.
sin
(
2
/
3 *
x
), 'r:'
, label
=
'sinusoid'
, linewidth
=
4
) # Red
plt
.
plot
(
x
, (
x -
3
) *
(
x -
2
) *
(
x -
8
) /
10
, 'b-'
, label
=
'polynomial'
, line
plt
.
plot
(
x
, np
.
minimum
(
np
.
abs
(
x -
3
), np
.
abs
(
x -
8
)), 'g--'
, label
=
'abs valu
plt
.
legend
()
plt
.
xlabel
(
'x'
)
plt
.
ylabel
(
'f(x)'
)
plt
.
show
()
Part 2: FIFA Players (22 points)
In [1]:
2/8/24, 8
:
33 PM
hw3
Page 3 of 14
about:srcdoc
Create a plotly scatter plot which shows the mean Overall
rating for all soccer players
(rows) of a particular Age
. Color your scatter plot per Nationality
of the player,
focusing on three countries (
England
, Germany
, Spain
). Download the players_fifa23.csv
from Canvas and make sure it is in the same directory as this
notebook file.
Export your graph as an html file age_ratings_nationality.html
and submit it
with your completed homework ipynb
to gradescope.
Hints:
There may be multiple ways/approaches to accomplish this task.
One approach: you may use groupby()
and boolean indexing to build these
values in a loop which runs per each Nationality
.
px.scatter()
will only graph data from columns (not the index). Some
approaches may need to graph data from the index. You can use df.reset_index() to
make your index a new column as shown in this example
In some approaches you may need to pass multiple rows to df.append() if need be
as shown in this example
In some approaches you may need to go from "wide" data to "long" data by using
df.melt() as discussed here
The first few code cells below get you started with looking at the data set.
import
warnings
warnings
.
simplefilter
(
action
=
'ignore'
, category
=
FutureWarning
)
# use pandas to read in the data
import
pandas as
pd
df_fifa =
pd
.
read_csv
(
'players_fifa23.csv'
, index_col =
'ID'
)
df_fifa
.
head
()
In [2]:
2/8/24, 8
:
33 PM
hw3
Page 4 of 14
about:srcdoc
import
plotly.express as
px
filtered_df =
df_fifa
[
df_fifa
[
'Nationality'
]
.
isin
([
'England'
, 'Germany'
, 'Sp
grouped_df =
filtered_df
.
groupby
([
'Age'
, 'Nationality'
])[
'Overall'
]
.
mean
()
.
r
fig =
px
.
scatter
(
grouped_df
, x
=
'Age'
, y
=
'Overall'
, color
=
'Nationality'
, labels
=
{
'Overall'
: 'Mean Overall Rating'
}, title
=
'Mean Overall Rating by Age and Nationality'
)
fig
.
write_html
(
'age_ratings_nationality.html'
)
Part 3: Daylight through the year
The remainder of the homework asks you to complete the pipeline which, given the
lattitude / longitude and timezone of some cities:
loc_dict =
{
'Boston'
: (
42.3601
, -
71.0589
, 'US/Eastern'
),
'Lusaka'
: (
-
15.3875
, 28.3228
, 'Africa/Lusaka'
),
'Sydney'
: (
-
33.8688
, 151.2093
, 'Australia/Sydney'
)}
the keys are the name
of the city and the values are tuples of `lat, lon, timezone_name
is able to:
query a sunrise / sunset API
clean and process data (timezone management & building datetime
objects)
Name
FullName
Age
Height
Weight
ID
165153
K. Benzema
Karim
Benzema
34
185
81
https://cdn.sofifa.net/players/16
158023
L. Messi
Lionel Messi
35
169
67
https://cdn.sofifa.net/players/15
231747
K. Mbappé
Kylian
Mbappé
23
182
73
https://cdn.sofifa.net/players/2
192985
K. De Bruyne
Kevin De
Bruyne
31
181
70
https://cdn.sofifa.net/players/19
188545
R.
Lewandowski
Robert
Lewandowski
33
185
81
https://cdn.sofifa.net/players/18
5 rows ×
89 columns
Out[2]:
In [3]:
2/8/24, 8
:
33 PM
hw3
Page 5 of 14
about:srcdoc
For extra credit: produce the following graph of daylight through the year:
Part 3.1: Getting Sunrise Sunset via API (16 points)
Write the get_sunrise_sunset()
function below so that it uses this sunrise sunset
API to produce produce the output shown in the test case below.
It may be helpful to know that this particular API...
requires no api key
returns about 2.5 queries per second
did not block me when I tried to make 100 consecutive calls as quickly as possible
# you will need to run pip install requests in the terminal
# no need to install json, it is built into python
import
requests
import
json
# make sure to write a good docstring! I will do this for you for the other def
get_sunrise_sunset
(
lat
, lng
, date
):
""" fetches the sunrise sunset API information on a particular date for Args:
lat (float): latitude of interest
lng (float): longitude of interest
In [4]:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help