wget https://raw.githubusercontent.com/aidenhuynh/CS_Swag/master/_notebooks/2022-11-30-randomvalues.ipynb

Libraries

  • A library is a collection of precompiled codes that can be used later on in a program for some specific well-defined operations.
  • These precompiled codes can be referred to as modules. Each module contains bundles of code that can be used repeatedly in different programs.
  • A library may also contain documentation, configuration data, message templates, classes, and values, etc.

Why are libraries important?

  • Using Libraries makes Python Programming simpler and convenient for the programmer.
  • One example would be through looping and iteration, as we don’t need to write the same code again and again for different programs.
  • Python libraries play a very vital role in fields of Machine Learning, Data Science, Data Visualization, etc.

A few libraries that simplify coding processes:

  • Pillow allows you to work with images.
  • Tensor Flow helps with data automation and monitors performance.
  • Matplotlib allows you to make 2D graphs and plots.

The AP Exam Refrence Sheet itself is a library! Screenshot 2022-12-11 221853

Hacks:

Research two other Python Libraries NOT DISCUSSED DURING LESSON and make a markdown post, explaining their function and how it helps programmers code.

API’s

  • An Application Program Interface, or API, contains specific direction for how the procedures in a library behave and can be used.
  • An API acts as a gateway for the imported procedures from a library to interact with the rest of your code.

Activity: Walkthrough with NumPy

  • Install NumPy on VSCode:
    1. Open New Terminal In VSCode:
    2. pip3 install --upgrade pip
    3. pip install numpy

REMEMBER: When running library code cells use Python Interpreter Conda (Version 3.9.12)

Example of using NumPy for arrays:

import numpy as np
new_matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
 
print (new_matrix)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Example of using NumPy for derivatives:

import numpy as np
 
# defining polynomial function
var = np.poly1d([2, 0, 1])
print("Polynomial function, f(x):\n", var)
 
# calculating the derivative
derivative = var.deriv()
print("Derivative, f(x)'=", derivative)
 
# calculates the derivative of after
# given value of x
print("When x=5  f(x)'=", derivative(5))
Polynomial function, f(x):
    2
2 x + 1
Derivative, f(x)'=  
4 x
When x=5  f(x)'= 20

Random Values

  • Random number generation (RNG) produces a random number (crazy right?)
    • This means that a procedure with RNG can return different values even if the parameters (inputs) do not change
  • CollegeBoard uses RANDOM(A, B), to return an integer between integers A and B.
    • RANDOM(1, 10) can output 1, 2, 3, 4, 5, 6, 7, 8, 9, or 10
    • In Python, this would be random.randint(A, B), after importing Python's "random" library (import random)
    • JavaScript's works a little differently, with Math.random() returning a value between 0 and 1.
      • To match Python and CollegeBoard, you could make a procedure like this

CollegeBoard Example: What is the possible range of values for answ3

CollegeBoard

Convert the following procedure to Python, then determine the range of outputs if n = 5.


PROCEDURE Dice(n)
    sum ← 0
    REPEAT UNTIL n = 0
        sum ← sum + RANDOM(1, 6)
        n ← n - 1
    RETURN sum

import random # Fill in the blank


def Dice(n):
    sum = 0
    while n>= 0:
        sum += random.randint(1, 6)
        n -= 1

    return sum
Dice(5) 
19

Homework

  1. Write a procedure that generates n random numbers, then sorts those numbers into lists of even and odd numbers (JS or Python, Python will be easier).

  2. Using NumPy and only coding in python cell, find the answer to the following questions: a. What is the derivative of 2x^5 - 6x^2 + 24x? b. What is the derivative of (13x^4 + 4x^2) / 2 when x = 9?

  3. Suppose you have a group of 10 dogs and 10 cats, and you want to create a random order for them. Show how random number generation could be used to create this random order.

import random

n = int(input("How many numbers would you like to generate?"))
a = int(input("Give the lowest number you want all numbers to be picked from?"))
b = int(input("Give the highest number you want all numbers to be picked from?"))
numbers = [random.randint(a, b) for _ in range(n)]
# I input the range from numbers 1-100

evens = []
odds = []
for number in numbers:
    if number % 2 == 0:
        evens.append(number)
    else:
        odds.append(number)

print(evens)
print(odds)
[82, 90, 36, 50, 74, 62, 42, 66, 74, 8]
[39, 25, 43, 35, 15, 49, 85, 83, 27, 59]
import numpy as np

poly = np.poly1d([2, 0, 0, -6, 24, 0])

derivative = poly.deriv()

print("The derivative of \n" + str(poly) + "\n" + "is" + "\n" + str(derivative))
The derivative of 
   5     2
2 x - 6 x + 24 x
is
    4
10 x - 12 x + 24
import numpy as np

n = np.poly1d([13, 0, 4, 0, 0])

d = np.poly1d([2])

derivative = ((d * n.deriv()) - (d.deriv() * n)) / 4

result = derivative(9)


print("The derivative of \n" + str(n) + "\n divided by" + str(d) + "\nis: \n" + str(derivative))
print("\nWhen x = 9,  f'(x) = \n" + str(round(result)))
The derivative of 
    4     2
13 x + 4 x
 divided by 
2
is: 
    3
26 x + 4 x

When x = 9,  f'(x) = 
18990
import random

animals = ['dog1', 'dog2', 'dog3', 'dog4', 'dog5', 'dog6', 'dog7', 'dog8', 'dog9', 'dog10', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7', 'cat8', 'cat9', 'cat10']

random.shuffle(animals)

print(animals)
['dog3', 'dog9', 'cat2', 'dog4', 'dog5', 'cat4', 'cat8', 'dog8', 'cat10', 'dog6', 'cat1', 'cat5', 'cat7', 'dog7', 'cat9', 'cat6', 'cat3', 'dog10', 'dog2', 'dog1']

Hacks

Two Other Python Libraries

Keras

  • Keras is a deep learning API written in Python, running on top of the machine learning platform TensorFlow. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result as fast as possible is key to doing good research.
  • It is simple, flexible, and powerful.
  • Keras reduces developer cognitive load to free you to focus on the parts of the problem that really matter.
  • Keras adopts the principle of progressive disclosure of complexity: simple workflows should be quick and easy, while arbitrarily advanced workflows should be possible via a clear path that builds upon what you've already learned.
  • Keras provides industry-strength performance and scalability: it is used by organizations and companies including NASA, YouTube, or Waymo.

Pandas

  • Pandas is an open source Python package that is most widely used for data science/data analysis and machine learning tasks. It is built on top of another package named Numpy, which provides support for multi-dimensional arrays. -Pandas makes it simple to do many of the time consuming, repetitive tasks associated with working with data such as; data cleansing, data fill, data normalization, merges and joins, data visualization, statistical analysis, data inspection, loading and saving data, etc.
  • Pretty much used to analyze data fast, powerfully, and flexibly