Git Product home page Git Product logo

dsc-getting-started-with-numpy-lab-onl01-dtsc-ft-030220's Introduction

Getting Started with NumPy - Lab

Introduction

Now that we have introduced NumPy, let's put it to practice. In this lab, you are going to be creating arrays, performing operations on them, and returning new arrays all using the NumPy library. Let's get started!

Objectives

You will be able to:

  • Instantiate a numpy array with specified values
  • Use broadcasting to perform a math operation on an entire numpy array

Import NumPy under the standard alias

# Import numpy using the standard alias

Generate some mock data

Create a NumPy array for each of the following: 1. Using a range 2. Using a Python list

Below, create a list in Python that has 5 elements (i.e. [0,1,2,3,4]) and assign it to the variable py_list.

Next, do the same, but instead of a list, create a range with 5 elements and assign it to the variable, py_range.

Finally, use the list and range to create NumPy arrays and assign the array from list to the variable array_from_list, and the array from the range to the variable array_from_range.

# Your code here
py_list = None
py_range = None
array_from_list = None
array_from_range = None

Next, we have a list of heights and weights and we'd like to use them to create a collection of BMIs. However, they are both in inches and pounds (imperial system), respectively.

Let's use what we know to create NumPy arrays with the metric equivalent values (height in meters & weight in kg).

Remember: NumPy can make these calculations a lot easier and with less code than a list!

1.0 inch = 0.0254 meters

2.2046 lbs = 1 kilogram

# Use the conversion rate for turning height in inches to meters
list_height_inches = [65, 68, 73, 75, 78]

# Your code here
array_height_inches = None
array_height_meters = None
# Use the conversion rate for turning weight in pounds to kilograms
list_weight_pounds = [150, 140, 220, 205, 265]

# Your code here
array_weight_pounds = None
array_weight_kg = None

The metric formula for calculating BMI is as follows:

BMI = weight (kg) ÷ height^2 (m^2)

So, to get BMI we divide weight by the squared value of height. For example, if I weighed 130kg and was 1.9 meters tall, the calculation would look like:

BMI = 130 / (1.9*1.9)

Use the BMI calculation to create a NumPy array of BMIs

# Your code here
BMI_array = None

Create a vector of ones the same size as your BMI vector using np.ones()

# Your code here
identity = None
identity

Multiply the BMI_array by your vector of ones

The resulting product should have the same values as your original BMI numpy array.

# Your code here

Level Up: Using NumPy to Parse a File

The Pandas library that we've been using is built on top of NumPy; all columns/series in a Pandas DataFrame are built using NumPy arrays. To get a better idea of a how a built-in method like pd.read_csv() works, we'll try and recreate that here!

# Open a text file (csv files are just plaintext separated by commas)
f = open('bp.txt')
n_rows = len(f.readlines())
# Print number of lines in the file
print('The file has {} lines.'.format(n_rows)) 
# After using readlines, we must reopen the file
f = open('bp.txt') 
# The file has values separated by tabs; we read the first line and check it's length 
n_cols = (len(f.readline().split('\t'))) 

f = open('bp.txt')

# Your code here
# Pseudocode outline below
#1) Create a matrix of zeros that is the same size of the file
#2) Iterate through the file: "for line in f:" Hint: using enumerate will also be required
    #3) Update each row of the matrix with the new stream of data
    #Hint: skip the first row (it's just column names, not the data.)
#4) Preview your results; you should now have a NumPy matrix with the data from the file

Summary

In this lab, we practiced creating NumPy arrays from both lists and ranges. We then practiced performing math operations like converting imperial measurements to metric measurements on each element of a NumPy array to create new arrays with new values. Finally, we used both of our new NumPy arrays to operate on each other and create new arrays containing the BMIs from our arrays containing heights and weights.

dsc-getting-started-with-numpy-lab-onl01-dtsc-ft-030220's People

Contributors

alexgriff avatar fpolchow avatar loredirick avatar mas16 avatar mathymitchell avatar mike-kane avatar peterbell avatar sumedh10 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.