Write My Paper Button

WhatsApp Widget

Unit 2 Self-Check Assignment Visualize Data Generated in Python (Google Colab) This assignment introduces you to Google Colab, a fully online software environment where you can upload Python code and data, run analytics

Unit 2 Self-Check Assignment Visualize Data Generated in Python (Google Colab)

This assignment introduces you to Google Colab, a fully online software environment where you can upload Python code and data, run analytics, and download the results. It runs through a browser, not on your computer.

In this assignment, you will learn basic Colab functionality, including file upload, code execution, and file download. We assume no prior programming experience.

You will submit the answers you find in this assignment using the self-check quiz.

There are the following steps in this assignment:
1. Log into Google Colab
2. Upload an .ipynb file (a Jupyter notebook which contains Python code) to Google Colab
3. Run the .ipynb file in Colab
4. Upload a data file and run the Jupyter notebook on the input file to produce an output file
5. Download the results and visualize in Tableau

Attachments:
• Unit_2_Google_Colab_Import.ipynb
• Birds and Mottos INPUT.csv file

Log into Google Colab

1. Open a new browser window on your computer.
2. Go to colab.research.google.com. It should bring up something a little like this. You may need to create a Google account (an existing @gmail.com email will work just fine here).

Alt Text: Colab welcome page

Upload a Jupyter Notebook (.ipynb) file to Google Colab

Python is a programming language, and lots of code-centric files associated with Python try to use the letters “py” wherever they can. You’ll see “numpy” and “scipy” libraries. You’ll see people talking about “pythonic” thinking. And you will see something called a Jupyter Notebook, which is a file which can contain Python code and comments and notes.

1. Download the “Unit_2_Google_Colab_Import.ipynb” file to your computer. Save it in a folder and pay attention to where you saved it.
2. Go back to your Google Colab Browser. Choose File -> Upload notebook.

Alt text: Colab page and directions

3. Browse and tell it to upload the “Unit_2_Google_Colab_Import.ipynb” file. It should look a little like this after you have uploaded the file:

Alt text: Upload in Google Colab
Run the .ipynb notebook in Google Colab

1. Let’s just jump right in. If you are more curious about this code and want to learn beyond what is explained here, you can certainly copy and paste the code into ChatGPT with the prompt, “I am going to give you some Python code. Explain to me what it does.”
2. Jupyter notebooks split the code into cells. Each cell contains one or more lines of code, and all the lines in a given cell will run when you execute the cell.
3. Some lines are comments and don’t contain code executed by the computer. A comment has a
# in front of it.
4. If you hover over the [ ] characters, you will see a little triangle icon, which is what we use to
run cells.
5. Let’s run the first cell. Hover over the [ ] and press the triangle when it shows up. You will get something like this:

Alt text: Running first cell
Enter your own name (the computer has forgotten all about whoever Mary was from earlier) and press the Enter key. It will execute the welcome message and print one to you.

6. You can change Python code on the screen and it will run according to your changes. Let’s demonstrate this as we run the next cell. The demo below uses a random seed of 42 and makes a magic number for Mary.
a. You can change this code – click on the 42 and change it to a different number.
b. Then click to run this cell, and you will see slightly different results from Mary’s magic number of 755.

Alt text: next cell
Question 1 – Verifying Your Magic Number

Change the random seed in the Python code book from 42 to 99. What is the magic number that you receive in return?
A. 42
B. 101 C. 382 D. 514
E. 755

Upload a data file and run the Jupyter notebook on the input file to produce an output file

1. We have seen how to upload Python code and run it, including typing in data such as your name when it asks, and changing the code itself (like when you updated the random seed to 99.)
2. One of its best features is you can upload a data file which is in one format, run some code to transform the data file, and then download the data file. This can be especially helpful if the data you want to visualize is not quite in the right format for Tableau, but if Python can pretty straightforwardly arrange it as such.
3. Now, we are going to upload the ‘Birds and Mottos INPUT.csv’ file to Google Colab. Click on the little folder icon on the left.

Alt text: Colab import
Then click on the “Upload to session storage” icon. This will let Colab know you want to upload a data file.

Alt text: Colab session storage in colab
a. Browse and locate the ‘Birds and Mottos INPUT.csv’ data file and upload it.
b. You may get a warning to ensure that your files are saved elsewhere, and that the runtime files will be deleted when this runtime is terminated. That’s OK – you have a copy on your own computer, and it’s just telling you that when you log out of Colab it won’t keep these files in memory for you.
4. You should now see the ‘Birds and Mottos INPUT.csv’ file on the left menu:

Alt text: Colab birds and mottos input in colab

5. Go ahead and run the next cell, in which it reads in the uploaded file:

Alt text: uploaded file in colab
6. Our next cell is going to ask you to enter a random seed from the keyboard (here, we suggest you type in 1), and then based on the seed number that you enter, it will append a sequence of numbers to our data file, and write the whole thing to an output .csv data file.

7. Here’s what it looks like before you enter the random seed (note there’s no output file on the
left.)

Alt text: Random Seed in colab

8. Here’s what it looks like after you enter the random seed and press enter. Note there’s now an output file listed on the left menu.

Alt text: Output file in colab
9. We can double-click on the output file name and it will show us what’s in the file:

Alt text: Output file name colab
10. If you want to go back to the Notebook code, you can click on the word ‘Notebook’

Alt text: Notebook in colab
Download the Results and Visualize in Tableau

1. Once you are satisfied with the results of your file, you can download to your computer and then from there, import them to Tableau like you usually would.
2. Right-click on the “Birds and Mottos OUTPUT.csv” file and it will let you download to your computer:

Alt text: Output
3. From here, you can fire up Tableau and connect to this .csv file just like you usually would.
You will need to do “Connect -> To a File -> More” and then choose your .csv file.
4. Go to a new worksheet. Let’s visualize our state-and-lucky-number-data.
5. First, drag the State geospatial variable onto the canvas:

Alt text: Variable in canvas
6. Change the Marks type to Map and the Color to Year of Admission (there is just one row per state, so it’s OK if it wants the color to be SUM(Year of Admission.)
7. Convert the Lucky Number to a continuous field (right-click on it and choose “Convert to Continuous”)
8. Now Set up a filter so the Lucky Number is between 0 and 499 (including 499).

Alt text: Lucky number
9. Which state has the southernmost point of land? In this case, it would be Texas.
10. Go back to the Notebook and rerun the code to append lucky numbers. Previously, you entered a random seed of 1.
11. This time, enter a random seed of 2.

Alt text: Random seed colab
12. Make sure it generates a new “Birds and Mottos OUTPUT.csv” file. Download that file, and visualize it similarly in Tableau. You will have a geospatial map with filters showing Lucky Numbers between 0 and 499 (include 499).

Question 2 – Run the Code with Your Own Random Seed; Import to Tableau

Which state has the easternmost point of land? Be sure your filters for the Lucky Number are on.
(Note your quiz may offer these in a slightly different order.)

A. Colorado B.
Montana
C. New Mexico
D. Texas
E. Wyoming