from datascience import *
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plots
plots.style.use('fivethirtyeight')
Functions (also refered to as call expressions
) take in input values (called arguments
) and return an output value.
A few example functions are:
abs()
: Takes the absolute value of a numbermin(x, y, ..., z)
: Takes the minimum value of x, y, ..., zround()
: round a number, potentially to a given number of decimal places# take the absolute value
# take the absolute value of a difference of numbers
# get the minimum value
# round a number
# round a number to 2 decimal places
Python (and most programming languages) have different ways to represent numbers. Two import numerical representation of numbers are:
ints
: These represent integers (whole numbers)floats
: These represent (approximate) real numbers (although still with finite precision)
A string
is the term used to describe data that is text. There are a number of operations (functions) that can be applied to strings as shown below. We can also convert numbers (ints and floats) into string representations, and strings back to number representations.
As we have seen there are different types
of data; e.g., ints, floats, strings, etc. Different functions operate on particular types of data. For example, one can't take the absolute value of a string (try it and you will see you will get a TypeError
).
We can use the type()
function to take what type of value a given name is holding.
Arrays are a data structure that holds a sequence of values of the same type. For example, a squence of all numbers, or a squence of all strings, etc.
We can use use the make_array
function from the datascience
package to create what are called ndarray
that are array implemented by the NumPy
package. One can perform a range of operations on these arrays in a very efficient manner.
We can use use the numpy
np.arange()
function to create ndarrays
that contain useful sequences of numbers.
Tables stored structured data. We can use the datascience
package to create Table
objects that we can perform data manipulation operations on (the Table
object is a simplified version of a Pandas DataFrame).
Some methods we can perform on Table
objects are:
tb.show(k)
: show the first k rows of the tabletb.select('col1', 'col2')
: select col1
and col2
from the tabletb.drop('col')
: remove col
from the tabletb.sort('col')
: sort the rows in the table based on the values in col
tb.where('col', value)
: reduce the table to rows where col
is equal to value
These methods all return Table objects that have been modified based on the methods that have been called.
Let's look at data on ice cream cones that is described in the class textbook.
# Load the ice cream data. Each row represents one ice cream cone.
cones = Table.read_table('cones.csv')
cones
# Show the first 2 rows of the data
# select only the Flavor column
# the original cones Table is not modified
# select the Flavor and Price columns
# remove the Color column
# sort by price
# sort by price highest to loweset
# select only the chocolate cones
# We can combine mulitple method called. Let's drop the color and then sort by price
We can extract columns from a Table
as either:
Table
with fewer columns using tb.select()
ndarray
using tb.column()