In-class Assignment: Installing Python Packages and Plotting with Maps in Python#
Day 4#
CMSE 202#
✅ Put your name here
#✅ Put your group member names here
#What if you wanted to make a map like the one below? How would you go about doing it using Python? Perhaps someone else already created a Python package that you could use!
Agenda for today’s class#
1. Review#
We’ll spend a bit of time going over anything that came up in the pre-class survey data.
2. Getting data from the web#
For this assignment we need to download a couple of data files. Here are direct links to the files:
https://raw.githubusercontent.com/wrobstory/vincent/master/examples/data/US_Unemployment_Oct2012.csv
https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/us-states.json
Rather than navigating to them using your browser, downloading the file and then dragging it into the right location, let’s use your command line skills! Use the curl
terminal command we revisited in the pre-class assignment instead.
Hint: Remember the trick we learned in a previous class period? You can actually run terminal commands from inside python. All you need to do is add the “!” to the beginning of your command.
✅ Do This: As a refresher, try it out – run the ls -la
command by using the following python code:
!ls -la
✅ Do This: Now download the two data files we need for class using the curl
command prefaced by the python terminal command (!). Important: you want to make sure that curl
downloads and saves the file on to your computer, but by default it will just print the contents of the file to the screen. You should have figured out how to save it as a file in previous assignments, but don’t hesitate to ask a group member if it’s slipping your mind.
# Put your code here
✅ Do This: As a group inspect the two new files.
Question 1: What types of files are they? What type of data do they seem to contain? Be prepared to share your answers with your instructors and/or the class.
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Do This: Figure out how to load the US_Unemployment_Oct2012.csv
into python and display some of the data. What libraries did you use?
#Put your code here.
3. Practice with pip
#
Lets switch gears and talk about installing software. Consider the following plotly
package:
✅ Question 2 : What does this package do?
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Do This: Install the plotly
package using pip and the python terminal operator (!) or inside your command line interface. You might run into issues as happens sometimes when install new packages. It might also take a bit of time for the package to install. If you run into issues, get help from your group mates to the instructors.
Important note: Did plot install as you expected? If not, why not? After trying to install plotly, what version is on your computer? Is it the latest version? What command could you use to check this? (hint: this command is included in the PCA) If you don’t have the latest version, how do you update it or install the specific version included on the “Getting Started” page?
#Put your code here
✅ Do This: Read through the documentation for running plotly
and try to get an example to successfully run in your notebook. Make sure to help your group mates to get it working! You might find this page particular useful to take a look at: https://plotly.com/python/getting-started/
#Put your example code here
4. Visualizing data with maps#
Finding good ways to create map visualizations in Python can be tricky and there are quite a number of tools out there that can do it. This part of the assignment will provide you with an opportunity to find new packages and learn how to use them to accomplish this task.
Getting used to learning how to use new packages is an important part of becoming a Python expert!
✅ Do This: Debug the following example code. If you have to use any terminal commands to fix the bug make sure you include those here.
import folium
map_msu = folium.Map(location=[42.7284006,-84.4804778])
folium.Marker([42.7284006,-84.4804778], popup='MSU').add_to(map_msu)
map_msu
✅ Question 3: What does the above example do?
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Question 4: Assuming we did everything up to this point correctly, what does the following code do? Warning: there may be a bug in the code!
import folium
import pandas as pd
state_geo = r'./us-states.json'
state_unemployment = r'./US_Unemployment_Oct2012.csv'
state_data = pd.read_csv(state_unemployment)
#Let Folium determine the scale
employ_map = folium.Map(location=[48, -102], zoom_start=3)
folium.Choropleth(geo_path=state_geo, data=state_data,
columns=['State', 'Unemployment'],
key_on='feature.id',
fill_color='YlOrRd', fill_opacity=0.7, line_opacity=0.2,
legend_name='Unemployment Rate (%)', smooth_factor=0.1,highlight=True).add_to(employ_map)
folium.LayerControl().add_to(employ_map)
employ_map
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Do This: Modify the above code so that the state colors range from green to blue.
✅ Question 5: What other colors are avaliable in the folium library? Is there a finite list of possible options?
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Do This: Explore some of the other function arguments that are available in the choropleth()
and determine how they alter the map visualization. Make note of any argument that you find to be particularly interesting or useful. Or, alternatively, any settings that return a comical or useless version of the map.
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Question 6: What controls in binning of values on the “Unemployment Rate” bar? Can you set the bins manually? How would you do this? You might want to test it out with a bit of code or consult the folium documentation.
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Do This: Think about some other ways that having a package like folium could be useful. Try to find at least three examples on the web of scientific data being represented on a map. Include links to your examples below and explain what the data is trying to represent.
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
More than one way to make a map…#
Folium isn’t the only Python package out there that can plot data on a map. Spend some time finding other Python packages that can allow you to work with maps and data in Python. Some of the solutions you find might use modules you’re already familiar with, some of them might use new modules. Explore the options and choose one that you want to try to get working.
✅ Do This: In the cell below try to get at least one example working in your notebook using a package other than folium for plotting data on a map. Feel free to discuss any issues your run into with the rest of your group!
5. Experimenting with generative AI#
In this part of the assignment we’re going to explore a bit of generative AI. Specifically, we’re going to try experimenting with a large language model (LLM). LLMs are a type of neural network that can be trained to predict the next word in a sequence of words. They are trained on large amounts of text data and can be used to generate new text that is similar to the text that they were trained on.
For this part of the assignment, you are encouraged to try using ChatGPT, which created and maintained by OpenAI. This is the LLM that made big news last year.
Note: You will need to sign up to use ChatGPT. If you do not already have an account, you should create one now. Feel free to use whatever email address you are comfortable using.
Another note: If you’re already played around with ChatGPT and you want to try a different tool, or if ChatGPT is not working, you can also experiment with Claude, which is a similar tool that was created by former OpenAI employees. You can find Claude here: https://claude.ai/
✅ Do This: Now that you’ve logged into either ChatGPT, Claude, or any other similar service that you’re comfortable with, try to get it to generate some text. You can use one of the “prompts” that is provided or you can try to write your own. If you’re having trouble getting it to work, try to find some examples on the web of people using the tool and see if you can figure out what they’re doing differently.
What is your experience with using the tool? Does it produce results you expect? Does it seem to work better with some prompts than others? What are some of the limitations of the tool? What are some of the benefits?
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Do This: Now that you’ve experimented with just generating some text, let’s try using it to assist us with our coding endeavors.
Go back to Part 4 of this assignment, copy some of the provided code or some of the code that you wrote and ask ChatGPT what the code does. How does it do with this task? What are some of the limitations? What are some of the benefits?
✎ Do This - Erase the contents of this cell and replace it with your answer to the above question! (double-click on this text to edit this cell, and hit shift+enter to save the text)
✅ Do This: Now that you’ve experimented with using this tool to assist you with understanding some code, see if you can accomplish the task posted at the end of Part 4 – can you get the tool to help you write functional code that plots data on a map using one of the Python packages you found and explored?
Try using the code provided by the tool as a starting point and see if you can get it to work right here in your notebook (in the cell below). If you run into issues, try to figure out what’s going wrong. You may need to experiment with the prompts you’re using or try to direct the tool further by giving it follow-up prompts. If you’re still stuck, ask your group mates or the instructors for help.
Important note: Don’t forget to include comments in your code or in a markdown cell citing that you used ChatGPT (or another tool) to help you write the code. You should also make sure to include the prompt you used to get to your final code.
# Put your AI-assisted mapping code here
# Make sure to cite the tool you use and the key prompt you used to get to things working
Congratulations, we’re done!#
Now, you just need to submit this assignment by uploading it to the course Desire2Learn web page for today’s dropbox (Don’t forget to add your name in the first cell).
© Copyright 2024, Department of Computational Mathematics, Science and Engineering at Michigan State University