# Numpy #

# #

# Before we start with Numpy...
#

# # Because Numpy is based on Python, we want to make sure you know the basics of Python3 in order to understand the materials we will dicuss throughout the course without any significant difficulties. Let's begin our learning with basic Python3 syntax before we dig into the actual data science. # # *If you are an exprienced Python programmer, you can skip this section and start from the **What is Numpy?** section.* # # ### Python3 # # #### Variables and Assignments # # When we do a programming, we need to know what variables are, when we create them, and how we use them. A **variable** is a memory location that stores certain value, object, or data type. Let's see an example: # In[ ]: x = 3 y = 2 # What did we just do above? We just created two variables, one called **x** which stores a numerical value of 3, and the other one called **y** which stores a numerical value of 2. We call it **assignment** because we **assigned** a value of 3 to x and a value of 2 to y. We can also do **multiple assignments** like: # In[ ]: x, y, z = 1, 2, 3 # This will assign x as 1, y as 2, and z as 3. Really intuitive isn't it? :) # # Numbers are not the only type we can assign. See different types of variables we can create: # In[1]: # We can write any statement as "comment" if we put a hashtag in front of a line. # These comments will be ignored by computer when it tries to read the code, so you can write anything here @#!#$ name = "Jared Lim" # any combination of characters called String graduation_year = 2020 # Integer CDS_Member = True # is it true or false? called Boolean list_of_qualities = ["Fun", "Cool"] # a list of things # The most important data type we will discuss is **list**, because it can be interpreted as a one-dimensional vector, which can be seen as a matrix with only one row (Numpy is a package that deals with matrix and matrix-related operations). # # #### List Operations # As shown above, list is a data type that stores a sequence of items separated by commas and encolsed by two square brackets []. One big advantage if list is that the items of a list can be of different types: for example, a list can contain number, string, and boolean all at the same time. # In[5]: cds_list = [1998, "cds", True] #notice in the list there is an Int, String, and Boolean bigger_list = ["abcd", 1234, cds_list] # list within a list # So far, we have only discussed about initializing a list. Now, let's learn how to deal with items inside of a list. Python is a **0-indexed language**, which means we start counting from 0. # We can access the values in a list by using these indexes: # In[6]: bigger_list[0] #returns 'abcd', which is the 0th element in the list # In[7]: bigger_list[1] # returns 1234, which is our "first" element # In[9]: bigger_list[2] #returns cds_list which is the 2nd element within the bigger_list # You can also set the values to a specific index: # In[7]: my_list = ["A", "B", "C"] my_list[0] = "D" my_list # returns ['D', 'B', 'C'] # A more advanced technique to access the elements in the list involve **slice operator** ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. # In[26]: a_list = ["Abby", "Ann", "Cameron", "Shubhom", "Ethan"] a_list[0:2] # the first index is inclusive, the second index is exclusive # returns ["Abby", "Ann"] # You can use the plus (+) sign to concatenate lists.

# You can use the asterisk (\*) to repeat the list multiple times.

# For more details, check the Python list operations: [click me](https://www.tutorialspoint.com/python/python_lists.htm) # In[27]: b_list=["Dae Won", "Jared"] ab_list=a_list + b_list # Using the plus (+) sign I can put 2 lists together ab_list # In[28]: b_list=b_list*3 # Using the asterisk (*) I can repeat the list. b_list #

# What is Numpy?
#

# # Numerical Python, or "Numpy" for short, is a foundational package on which many of the most common data science packages are built. Numpy provides us with multi-dimensional arrays, called ndarrays, which can be created as vectors or matrices. We can use numpy to manipulate datasets to make them easier to work with. Numpy also comes with a number of helpful statistical methods. # # The key features of numpy are: # # - **ndarrays** are n-dimensional arrays of the same data type which are fast and space-efficient. There are many built-in methods for ndarrays which allow for rapid processing of data without using loops (e.g., compute the mean). # - **Broadcasting** is a tool which dictates how operations between multi-dimensional arrays of different sizes will be carried out. # - **Vectorization** allows for numeric operations on ndarrays. # - **Input/Output** simplifies reading and writing of data from/to file. # #

# Numpy Documentation

#

#

#
# Intro to ndarrays

#
# Creating and modifying Rank 1 ndarrays:
#

#
# Creating a Rank 2 ndarray:

#
# Different ways to create ndarrays:
#

#
# Using Array Indexing
#

# Slice indexing (slicing):
#

#
# Using integer indexing & slice indexing
#

#
# Using Array Indexing to change elements
#

# Using Boolean Indexing
#
#

#
# Using Array Indexing to change elements
#

#
# Array Operations
#

#
# Arithmetic Array Operations:
#
#

#
# Intro to Statistical Methods, Sorting, and Set Operations
#

#

#
# Getting Started with Statistical Operations
#

# In[25]: # setup a random 3x3 matrix arr = 10 * np.random.randn(3,3) print(arr) # compute the mean for all elements in the array print('\n',arr.mean()) # set the axis value to 1 compute the means for each row print('\n',arr.mean(axis = 1)) # set the axis value to 0 compute the means for each column print('\n',arr.mean(axis = 0)) # sum all the elements in the array print('\n',arr.sum()) # compute the median for all elements in the array print('\n',np.median(arr)) # compute the medians for each row print('\n',np.median(arr, axis = 1)) #

#
# Using the Unique method
#

#
# Set Operations on ndarrays
#

#
# Intro to Broadcasting:
#

#

#
# Other Common ndarray Operations
#

#

#
# Dot Product and Inner Product
#
#

#
# Using sum():
#

#
# Element-wise Functions:

#
# Reshaping arrays:
#

#
# Using transpose():
#
#

#
# Indexing using where():

#
# Using any() and all()

#
# Random Number Generation:
#

#
# Merging two data sets:
#