Commit b78e17f0 authored by MichaelAng's avatar MichaelAng

[ban265] hw6-hw8

parent 209f688c
......@@ -11,6 +11,6 @@ v_height = [1.67, 1.83, 1.76]
print([round(w / h**2, 2) for w, h in zip(v_weight, v_height)])
# 2. BMI for a person is calculated as weight in kg / (height in meters * height in meters). You need to calculate everyone in the following dictionary variables. (Hint: Alice 23.31, Bob 25.98, Cindy 24.54)
v_weight = {"Bob":87, "Cindy":76, "Alice":65}
v_height = {"Alice":1.67, "Cindy":1.76, "Bob":1.83}
print({k: round(v_weight[k] / v_height[k]**2, 2) for k in v_weight.keys()})
v_weight = {"Bob": 87, "Cindy": 76, "Alice": 65}
v_height = {"Alice": 1.67, "Cindy": 1.76, "Bob": 1.83}
print({k: round(v_weight[k] / v_height[k] ** 2, 2) for k in v_weight.keys()})
# Ang, Michael
# BAN 265 A
# Homework 6
# 2024-04-25
# https://www.dropbox.com/s/kclkrkswvrvk231/PY_hw6.docx?e=1&dl=0
# Question 1
# Please write the python code to (Don’t achieve this task by using the pandas package. It is intended to train your logic thinking of how to use if statement and for statement.)
import csv
transactions = []
with open("supermarket_hw6.csv", "r") as file:
reader = csv.DictReader(file)
transactions = list(reader)
# (1) Show the product name of all the items purchased in the transaction number T005. (Hint: you should have the following 2 items: “EGGPLANT,and MILK”)
for t in transactions:
if t.get("Transaction Number") == "T005":
print("Product Name: ", t.get("Product Name"))
# (2) Maintain a list of all the customer account id. (Hint: you should have [‘C001’, ‘C002’,‘C003’])
customer_accounts = list({t.get("Customer Account") for t in transactions})
customer_accounts = sorted(customer_accounts)
print("Customer Accounts: ", customer_accounts)
# Ang, Michael
# BAN 265 A
# Homework 7
# 2024-04-25
# https://www.dropbox.com/s/ml6w2eu63m64jp0/PY_hw7.docx?e=1&dl=0
# Question 1
# Use numpy to calculate the likelihood ratio for the following data with the formula: f(x) (Use the np.log() function not the math.log() function. Hint: 87.18, 2678.36, 77.20, 405.47)
import numpy as np
values = [(1000, 0.5, 0.3), (8000, 0.2, 0.6), (3000, 0.8, 0.7), (5000, 0.6, 0.4)]
for n, tp, ep in values:
print(
"likelihood_ratio",
round(tp * n * np.log(tp / ep) + (n - tp * n) * np.log((1 - tp) / (1 - ep)), 2),
)
# Question 2
# Use numpy to do the matrix product between...
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.array([[1, 2, 3], [4, 5, 6]])
print(a.dot(b))
# Ang, Michael
# BAN 265 A
# Homework 8
# 2024-04-25
# https://www.dropbox.com/s/nt9npj9jp0gmwe7/PY_hw8.docx?e=1&dl=0
# Question 1
import pandas as pd
df = pd.read_csv('supermarket_hw6.csv')
# (1) Show the product name of all the items purchased in the transaction number T005. (Hint: you should have the following 2 items: “EGGPLANT,and MILK”)
print(df[df['Transaction Number'] == 'T005']['Product Name'].values)
# (2) Maintain a list of all the customer account id. (Hint: you should have [‘C001’, ‘C002’,‘C003’])
customer_accounts = pd.unique(df['Customer Account'])
print("Customer Accounts: ", customer_accounts)
Date,Transaction Number,UPC Number,Product Name,Customer Account,Qty,Regular Price,Sale Price,Discount
2016-04-25 13:34:35.287,T001,P001,EGGPLANT,C001,1.25,1.9900000000000,1.99,0.00
2016-04-25 13:34:35.297,T001,P001,EGGPLANT,C001,1.26,1.9900000000000,1.99,0.00
2016-04-25 13:34:35.307,T001,P002,MILK,C001,1,3.9900000000000,3.99,0.00
2016-04-26 13:34:35.287,T002,P001,EGGPLANT,C002,1.25,1.9900000000000,1.99,0.00
2016-04-26 13:34:35.297,T002,P003,BREAD,C002,2,0.9900000000000,0.99,0.00
2016-04-26 13:34:35.307,T002,P002,MILK,C002,1,3.9900000000000,3.99,0.00
2016-05-01 13:34:35.287,T003,P001,EGGPLANT,C002,1.25,1.9900000000000,1.99,0.00
2016-05-01 13:34:35.297,T003,P003,BREAD,C002,3,0.9900000000000,0.99,0.00
2016-05-09 13:34:35.287,T004,P001,EGGPLANT,C002,1.25,1.9900000000000,1.99,0.00
2016-05-09 13:34:35.297,T004,P003,BREAD,C002,3,0.9900000000000,0.99,0.00
2016-05-10 13:34:35.297,T005,P001,EGGPLANT,C001,1.26,1.9900000000000,1.99,0.00
2016-05-10 13:34:35.307,T005,P002,MILK,C001,1,3.9900000000000,3.99,0.00
2016-05-19 13:34:35.287,T006,P001,EGGPLANT,C002,1.25,1.9900000000000,1.99,0.00
2016-05-19 13:34:35.297,T006,P003,BREAD,C002,3,0.9900000000000,0.99,0.00
2016-05-26 13:34:35.287,T007,P001,EGGPLANT,C001,1.25,1.9900000000000,1.99,0.00
2016-05-26 13:34:35.297,T007,P001,EGGPLANT,C001,1.26,1.9900000000000,1.99,0.00
2016-05-26 13:34:35.307,T007,P002,MILK,C001,1,3.9900000000000,3.99,0.00
2016-05-29 13:34:35.287,T008,P001,EGGPLANT,C002,1.25,1.9900000000000,1.99,0.00
2016-05-29 13:34:35.297,T008,P003,BREAD,C002,3,0.9900000000000,0.99,0.00
2016-06-12 13:34:35.297,T009,P003,BREAD,C003,3,0.9900000000000,0.99,0.00
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment