title : Insert the chapter title here description : Insert the chapter description here attachments : slides_link : https://s3.amazonaws.com/assets.datacamp.com/course/teach/slides_example.pdf
--- type:MultipleChoiceExercise lang:python xp:50 skills:1 key:4800e25739
Have a look at the plot that showed up in the viewer to the right. Which type of movies have the worst rating assigned to them?
*** =instructions
- Long movies, clearly
- Short movies, clearly
- Long movies, but the correlation seems weak
- Short movies, but the correlation seems weak
*** =hint Have a look at the plot. Do you see a trend in the dots?
*** =pre_exercise_code
# The pre exercise code runs code to initialize the user's workspace.
# You can use it to load packages, initialize datasets and draw a plot in the viewer
import pandas as pd
import matplotlib.pyplot as plt
movies = pd.read_csv("http://s3.amazonaws.com/assets.datacamp.com/course/introduction_to_r/movies.csv")
plt.scatter(movies.runtime, movies.rating)
plt.show()
*** =sct
# SCT written with pythonwhat: https://github.com/datacamp/pythonwhat/wiki
msg_bad = "That is not correct!"
msg_success = "Exactly! The correlation is very weak though."
test_mc(4, [msg_bad, msg_bad, msg_bad, msg_success])
--- type:NormalExercise lang:python xp:100 skills:1 key:cf255d39f0
Do you remember the plot of the last exercise? Let's make an even cooler plot!
A dataset of movies, movies
, is available in the workspace.
*** =instructions
- The first function,
np.unique()
, uses theunique()
function of thenumpy
package to get integer values for the movie genres. You don't have to change this code, just have a look! - Import
pyplot
in thematplotlib
package. Set an alias for this import:plt
. - Use
plt.scatter()
to plotmovies.runtime
onto the x-axis,movies.rating
onto the y-axis and useints
for the color of the dots. You should use the first and second positional argument, and thec
keyword. - Show the plot using
plt.show()
.
*** =hint
- You don't have to program anything for the first instruction, just take a look at the first line of code.
- Use
import ___ as ___
to importmatplotlib.pyplot
asplt
. - Use
plt.scatter(___, ___, c = ___)
for the third instruction. - You'll always have to type in
plt.show()
to show the plot you created.
*** =pre_exercise_code
import pandas as pd
movies = pd.read_csv("http://s3.amazonaws.com/assets.datacamp.com/course/introduction_to_r/movies.csv")
import numpy as np
*** =sample_code
# Get integer values for genres
_, ints = np.unique(movies.genre, return_inverse = True)
# Import matplotlib.pyplot
# Make a scatter plot: runtime on x-axis, rating on y-axis and set c to ints
# Show the plot
*** =solution
# Get integer values for genres
_, ints = np.unique(movies.genre, return_inverse = True)
# Import matplotlib.pyplot
import matplotlib.pyplot as plt
# Make a scatter plot: runtime on x-axis, rating on y-axis and set c to ints
plt.scatter(movies.runtime, movies.rating, c=ints)
# Show the plot
plt.show()
*** =sct
# SCT written with pythonwhat: https://github.com/datacamp/pythonwhat/wiki
test_function("numpy.unique",
not_called_msg = "Don't remove the call of `np.unique` to define `ints`.",
incorrect_msg = "Don't change the call of `np.unique` to define `ints`.")
test_object("ints",
undefined_msg = "Don't remove the definition of the predefined `ints` object.",
incorrect_msg = "Don't change the definition of the predefined `ints` object.")
test_import("matplotlib.pyplot", same_as = True)
test_function("matplotlib.pyplot.scatter",
incorrect_msg = "You didn't use `plt.scatter()` correctly, have another look at the instructions.")
test_function("matplotlib.pyplot.show")
success_msg("Great work!")
--- type:BokehServerExercise lang:python xp:100 skills:1 key:a9e9f99d6a
Blabla bokeh Server
*** =instructions Plot the plot with bokeh server
*** =hint
- plot
- plot again
*** =pre_exercise_code
#
*** =sample_code
import numpy as np
from numpy import pi
from bokeh.driving import cosine
from bokeh.plotting import figure, curdoc
x = np.linspace(0, 4*pi, 80)
y = np.sin(x)
p = figure()
r1 = p.line([0, 4*pi], [-1, 1], color="firebrick")
r2 = p.line(x, y, color="navy", line_width=4)
curdoc().add_root(p)
@cosine(w=0.03)
def update(step):
# updating a single column of the the *same length* is OK
r2.data_source.data["y"] = y * step
r2.glyph.line_alpha = 1 - 0.8 * abs(step)
curdoc().add_periodic_callback(update, 50)
*** =solution
#
*** =sct
#