|
| 1 | +--- |
| 2 | +title : Insert the chapter title here |
| 3 | +description : Insert the chapter description here |
| 4 | +attachments : |
| 5 | + slides_link : https://s3.amazonaws.com/assets.datacamp.com/course/teach/slides_example.pdf |
| 6 | + |
| 7 | +--- type:VideoExercise lang:python xp:50 skills:1 |
| 8 | +## Analyze movie ratings |
| 9 | + |
| 10 | +*** =video_link |
| 11 | +//player.vimeo.com/video/154783078 |
| 12 | + |
| 13 | +--- type:MultipleChoiceExercise lang:python xp:50 skills:1 |
| 14 | +## A really bad movie |
| 15 | + |
| 16 | +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? |
| 17 | + |
| 18 | +*** =instructions |
| 19 | +- Long movies, clearly |
| 20 | +- Short movies, clearly |
| 21 | +- Long movies, but the correlation seems weak |
| 22 | +- Short movies, but the correlation seems weak |
| 23 | + |
| 24 | +*** =hint |
| 25 | +Have a look at the plot. Do you see a trend in the dots? |
| 26 | + |
| 27 | +*** =pre_exercise_code |
| 28 | +```{r} |
| 29 | +# The pre exercise code runs code to initialize the user's workspace. You can use it for several things: |
| 30 | +
|
| 31 | +# 1. Pre-load packages, so that users don't have to do this manually. |
| 32 | +import pandas as pd |
| 33 | +import matplotlib.pyplot as plt |
| 34 | +
|
| 35 | +# 2. Preload a dataset. The code below will read the csv that is stored at the URL's location. |
| 36 | +# The movies variable will be available in the user's console. |
| 37 | +movies = pd.read_csv("http://s3.amazonaws.com/assets.datacamp.com/course/introduction_to_r/movies.csv") |
| 38 | +
|
| 39 | +# 3. Create a plot in the viewer, that students can check out while reading the exercise |
| 40 | +plt.scatter(movies.runtime, movies.rating) |
| 41 | +plt.show() |
| 42 | +``` |
| 43 | + |
| 44 | +*** =sct |
| 45 | +```{r} |
| 46 | +# The sct section defines the Submission Correctness Tests (SCTs) used to |
| 47 | +# evaluate the student's response. All functions used here are defined in the |
| 48 | +# pythonwhat Python package |
| 49 | +
|
| 50 | +msg_bad = "That is not correct!" |
| 51 | +msg_success = "Exactly! The correlation is very weak though." |
| 52 | +
|
| 53 | +# Use test_mc() to grade multiple choice exercises. |
| 54 | +# Pass the correct option (Action, option 2 in the instructions) to correct. |
| 55 | +# Pass the feedback messages, both positive and negative, to feedback_msgs in the appropriate order. |
| 56 | +test_mc(4, [msg_bad, msg_bad, msg_bad, msg_success]) |
| 57 | +``` |
| 58 | + |
| 59 | +--- type:MultipleChoiceExercise lang:python xp:50 skills:1 |
| 60 | +## A really bad movie |
| 61 | + |
| 62 | +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? |
| 63 | + |
| 64 | +*** =instructions |
| 65 | +- Long movies, clearly |
| 66 | +- Short movies, clearly |
| 67 | +- Long movies, but the correlation seems weak |
| 68 | +- Short movies, but the correlation seems weak |
| 69 | + |
| 70 | +*** =hint |
| 71 | +Have a look at the plot. Do you see a trend in the dots? |
| 72 | + |
| 73 | +*** =pre_exercise_code |
| 74 | +```{python} |
| 75 | +# The pre exercise code runs code to initialize the user's workspace. You can use it for several things: |
| 76 | +
|
| 77 | +# 1. Pre-load packages, so that users don't have to do this manually. |
| 78 | +import pandas as pd |
| 79 | +import matplotlib.pyplot as plt |
| 80 | +
|
| 81 | +# 2. Preload a dataset. The code below will read the csv that is stored at the URL's location. |
| 82 | +# The movies variable will be available in the user's console. |
| 83 | +movies = pd.read_csv("http://s3.amazonaws.com/assets.datacamp.com/course/introduction_to_r/movies.csv") |
| 84 | +
|
| 85 | +# 3. Create a plot in the viewer, that students can check out while reading the exercise |
| 86 | +plt.scatter(movies.runtime, movies.rating) |
| 87 | +plt.show() |
| 88 | +``` |
| 89 | + |
| 90 | +*** =sct |
| 91 | +```{python} |
| 92 | +# The sct section defines the Submission Correctness Tests (SCTs) used to |
| 93 | +# evaluate the student's response. All functions used here are defined in the |
| 94 | +# pythonwhat Python package |
| 95 | +
|
| 96 | +msg_bad = "That is not correct!" |
| 97 | +msg_success = "Exactly! The correlation is very weak though." |
| 98 | +
|
| 99 | +# Use test_mc() to grade multiple choice exercises. |
| 100 | +# Pass the correct option (option 4 in the instructions) to correct. |
| 101 | +# Pass the feedback messages, both positive and negative, to feedback_msgs in the appropriate order. |
| 102 | +test_mc(4, [msg_bad, msg_bad, msg_bad, msg_success]) |
| 103 | +``` |
| 104 | + |
| 105 | +--- type:NormalExercise lang:python xp:100 skills:1 |
| 106 | +## Plot the movies yourself |
| 107 | + |
| 108 | +Do you remember the plot of the last exercise? Let's make an even cooler plot! |
| 109 | + |
| 110 | +A dataset of movies, `movies`, is available in the workspace. |
| 111 | + |
| 112 | +*** =instructions |
| 113 | +- The first function, `np.unique()`, uses the `unique()` function of the `numpy` package to get integer values for the movie genres. You don't have to change this code, just have a look! |
| 114 | +- Import `pyplot` in the `matplotlib` package. Set an alias for this import: `plt`. |
| 115 | +- Use `plt.scatter()` to plot `movies.runtime` onto the x-axis, `movies.rating` onto the y-axis and use `ints` for the color of the dots. You should use the first and second positional argument, and the `c` keyword. |
| 116 | +- Show the plot using `plt.show()`. |
| 117 | + |
| 118 | +*** =hint |
| 119 | +- You don't have to program anything for the first instruction, just take a look at the first line of code. |
| 120 | +- Use `import ___ as ___` to import `matplotlib.pyplot` as `plt`. |
| 121 | +- Use `plt.scatter(___, ___, c = ___)` for the third instruction. |
| 122 | +- You'll always have to type in `plt.show()` to show the plot you created. |
| 123 | + |
| 124 | +*** =pre_exercise_code |
| 125 | +```{python} |
| 126 | +# The pre exercise code runs code to initialize the user's workspace. You can use it for several things: |
| 127 | +
|
| 128 | +# 1. Preload a dataset. The code below will read the csv that is stored at the URL's location. |
| 129 | +# The movies variable will be available in the user's console. |
| 130 | +import pandas as pd |
| 131 | +movies = pd.read_csv("http://s3.amazonaws.com/assets.datacamp.com/course/introduction_to_r/movies.csv") |
| 132 | +
|
| 133 | +# 2. Preload a package |
| 134 | +import numpy as np |
| 135 | +``` |
| 136 | + |
| 137 | +*** =sample_code |
| 138 | +```{python} |
| 139 | +# Get integer values for genres |
| 140 | +_, ints = np.unique(movies.genre, return_inverse = True) |
| 141 | +
|
| 142 | +# Import matplotlib.pyplot |
| 143 | +
|
| 144 | +
|
| 145 | +# Make a scatter plot: runtime on x-axis, rating on y-axis and set c to ints |
| 146 | +
|
| 147 | +
|
| 148 | +# Show the plot |
| 149 | +
|
| 150 | +``` |
| 151 | + |
| 152 | +*** =solution |
| 153 | +```{python} |
| 154 | +# Get integer values for genres |
| 155 | +_, ints = np.unique(movies.genre, return_inverse = True) |
| 156 | +
|
| 157 | +# Import matplotlib.pyplot |
| 158 | +import matplotlib.pyplot as plt |
| 159 | +
|
| 160 | +# Make a scatter plot: runtime on x-axis, rating on y-axis and set c to ints |
| 161 | +plt.scatter(movies.runtime, movies.rating, c=ints) |
| 162 | +
|
| 163 | +# Show the plot |
| 164 | +plt.show() |
| 165 | +``` |
| 166 | + |
| 167 | +*** =sct |
| 168 | +```{python} |
| 169 | +# The sct section defines the Submission Correctness Tests (SCTs) used to |
| 170 | +# evaluate the student's response. All functions used here are defined in the |
| 171 | +# pythonwhat Python package. Documentation can also be found at github.com/datacamp/pythonwhat/wiki |
| 172 | +
|
| 173 | +# Check if the student changed the np.unique() call |
| 174 | +# If it's not called, we know the student removed the call. |
| 175 | +# If it's called incorrectly, we know the student changed the call. |
| 176 | +test_function("numpy.unique", |
| 177 | + not_called_msg = "Don't remove the call of `np.unique` to define `ints`.", |
| 178 | + incorrect_msg = "Don't change the call of `np.unique` to define `ints`.") |
| 179 | +# Check if the student removed the ints object |
| 180 | +test_object("ints", |
| 181 | + undefined_msg = "Don't remove the definition of the predefined `ints` object.", |
| 182 | + incorrect_msg = "Don't change the definition of the predefined `ints` object.") |
| 183 | + |
| 184 | +# Check if the student imported matplotlib.pyplot like the solution |
| 185 | +# Let automatic feedback message generation handle the feedback messages |
| 186 | +test_import("matplotlib.pyplot", same_as = True) |
| 187 | + |
| 188 | +# Check whether the student used the scatter() function correctly |
| 189 | +# If it's used, but incorrectly, tell them to check the instructions again |
| 190 | +test_function("matplotlib.pyplot.scatter", |
| 191 | + incorrect_msg = "You didn't use `plt.scatter()` correctly, have another look at the instructions.") |
| 192 | + |
| 193 | +# Check if the student called the show() function |
| 194 | +# Let automatic feedback message generation handle all feedback messages |
| 195 | +test_function("matplotlib.pyplot.show") |
| 196 | + |
| 197 | +success_msg("Great work!") |
| 198 | +``` |
0 commit comments