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 placesabs(-5) # take the absolute value
5
# take the absolute value of a difference of numbers
a = 5
b = 7
abs(a - b)
2
# get the minimum value
min(8, 3)
3
# round a number
round(123.56789)
124
# round a number to 2 decimal places
round(123.56789, 2)
123.57
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)2 # int
30
10 + 3 # int
1.7 + 4 # float
5.7
2. + 3 # float
5.0
10 / 3 # float
3.3333333333333335
10 / 2 # still a float
5.0
123 ** 4
228886641
30 / 400
0.075
30 / 4000000000 # output in scientific notation
7.5e-09
9 ** 0.5
3.0
.12345678901234567890123456789 # limited precision
13 ** 0.5
3.605551275463989
(13 ** 0.5) ** 2 # After arithmetic, the final few decimal places can be wrong
12.999999999999998
float(3)
3.0
int(6.75)
6
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.
'Flavor'
'Flavor'
'any snippet of text'
'any snippet of text'
'2' + 'x' # concatenation
'2x'
'straw' + 'berry'
'strawberry'
'two ' + 'words' # notice the space after the 'two '
'two words'
'ha' * 5
'hahahahaha'
str(2)
'2'
int('2')
2
int('2.3')
float('2.3')
2.3
int(float('2.3'))
2
str('3', '2') # To concatenate strings, use +
'3'+'2'
'32'
2 + 'x'
"I'm a data scientist!"
'I'm a data scientist!'
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.
type(2.3)
type(100)
type('abcd')
a = 5.7
type(a)
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.
make_array(1, 2, 3, 4)
my_array = make_array(5, 6, 7, 8)
my_array
len(my_array)
sum(my_array)
sum(my_array) / len(my_array)
my_array
my_array * 2
another_one = make_array(20, 30, 40, 50)
my_array + another_one
yet_another = make_array(1, 2, 3, 4, 5, 6)
my_array + yet_another
my_array
my_array.item(0)
We can use use the numpy
np.arange()
function to create ndarrays
that contain useful sequences of numbers.
np.arange(4)
array([0, 1, 2, 3])
np.arange(5, 25)
np.arange(5, 25, 10)
np.arange(5, 26, 10)
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
type(cones)
# Show the first 2 rows of the data
cones.show(2)
# select only the Flavor column
only_flavor = cones.select('Flavor')
only_flavor
# the original cones Table is not modified
cones
# select the Flavor and Price columns
cones.select('Flavor', 'Price')
# remove the Color column
no_color = cones.drop('Color')
no_color
Flavor | Price |
---|---|
strawberry | 3.55 |
chocolate | 4.75 |
chocolate | 5.25 |
strawberry | 5.25 |
chocolate | 5.25 |
bubblegum | 4.75 |
# sort by price
cones.sort('Price')
Flavor | Color | Price |
---|---|---|
strawberry | pink | 3.55 |
chocolate | light brown | 4.75 |
bubblegum | pink | 4.75 |
chocolate | dark brown | 5.25 |
strawberry | pink | 5.25 |
chocolate | dark brown | 5.25 |
# sort by price highest to loweset
cones.sort('Price', descending=True)
Flavor | Color | Price |
---|---|---|
chocolate | dark brown | 5.25 |
strawberry | pink | 5.25 |
chocolate | dark brown | 5.25 |
bubblegum | pink | 4.75 |
chocolate | light brown | 4.75 |
strawberry | pink | 3.55 |
# select only the chocolate cones
cones.where('Flavor', 'chocolate')
Flavor | Color | Price |
---|---|---|
chocolate | light brown | 4.75 |
chocolate | dark brown | 5.25 |
chocolate | dark brown | 5.25 |
# We can combine mulitple method called. Let's drop the color and then sort by price
cones.drop('Color').sort('Price', descending=True)
Flavor | Price |
---|---|
chocolate | 5.25 |
strawberry | 5.25 |
chocolate | 5.25 |
bubblegum | 4.75 |
chocolate | 4.75 |
strawberry | 3.55 |
We can extract columns from a Table
as either:
Table
with fewer columns using tb.select()
ndarray
using tb.column()
cones.select('Price') # still a table
Price |
---|
3.55 |
4.75 |
5.25 |
5.25 |
5.25 |
4.75 |
type(cones.select('Price'))
datascience.tables.Table
cones.column('Price') # an array
array([3.55, 4.75, 5.25, 5.25, 5.25, 4.75])
type(cones.column('Price'))
numpy.ndarray