Unit 3 Sections 14 and 15
Libraries and Random Values
- Libraries
- collection of precompiled codes that can be used later on in a program for some specific well-defined operations.
- precompiled codes can be referred to as modules. Each module contains bundles of code that can be used repeatedly in different programs.
- may also contain documentation, configuration data, message templates, classes, and values, etc.
- makes Python Programming simpler and convenient for the programmer
- ex: Pillow, Tensor Flow, Matplotlib
- APIs
- application program interface, contains specific direction for how the procedures in a library can behave or be used
- acts as a gateway for the imported procedures from a library to interact with the rest of your code
import numpy as np
new_matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
print (new_matrix)
Numpy to find 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))
- Random Values
- Random number generation (RNG) produces a random number
- procedure with RNG can return different values even if the parameters do not change
- Random number generation (RNG) produces a random number
import random
def dice(n):
sum = 0
while n > 0:
sum = sum + random.randint(1,6)
n = n -1
return sum
dice(5)
-
Requests
- allows you to send HTTP/1.1 requests extremely easily
- coders don't have to manually add query strings to URLs, form encode PUT and POST data
- creates more efficient ways to:
- Make requests using the most common HTTP methods
- Customize your requests’ headers and data, using the query string and message body
- Inspect data from your requests and responses
- Make authenticated requests
- Configure your requests to help prevent your application from backing up or slowing down
-
Theano
- allows tou to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays
- helps you perform data intensive computations up to 140x faster
- can compute derivatives for functions of one or many inputs
- evaluates expressions faster
- 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).
import random
list = []
n = int(input("How many numbers would you like to generate?"))
min = int(input("What is the smallest number you would like to generate?"))
max = int(input("What is the biggest number you would like to generate?"))
num = [random.randint(min, max) for v in range(n)] #generating 100 random numbers in the range of 0-200 (in this case my n random numbers is 100. Im setting a range to prevent ridiculous numbers)
list.extend(num)
def splitevenodd(list):
evenlist = []
oddlist = []
for i in list:
if (i % 2 == 0):
evenlist.append(i)
else:
oddlist.append(i)
print("Even list:", evenlist)
print("Odd list:", oddlist)
splitevenodd(list)
- 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?
import numpy as np
# defining polynomial function
eq1 = np.poly1d([2, 0, 0, 6, 24, 0])
print("f(x)=\n", eq1)
# calculating the derivative
derivative = eq1.deriv()
print("The derivative of f(x)=\n", derivative)
#-----------------------------------
eq2 = np.poly1d([6.5, 0, 2, 0, 0]) #i simplified your derivative which shud be fine right?
print("g(x)=\n", eq2)
derivative2 = eq2.deriv()
print("The derivative of 13x^4 + 4x^2 / 2 (g(x)) when x = 9 is:\n", derivative2(9))
- 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
dogs = ["bob", "mark", "lucy", "tod", "frank", "lily", "cookie", "oreo", "ollie", "charlie"]
cats = ["luna", "ginger", "mochi", "pluto", "jeff", "skippy", "lemon", "boba", "chase", "louis"] #list of 10 dog and cat names
def randomOrder(animals):
list = []
random.shuffle(animals)
result = f'{animals}'
print("New Random Order: " + result)
randomOrder(dogs)
randomOrder(cats)