import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Below, we will do "translation" of Mathemetica code into python
def datacsvimp(filepath) :
ximpdat=np.array(pd.read_csv(filepath,header=None));
pricestr=ximpdat[2:, 2: ]; # the price data are strings with $ sign
# we have to remove '$' and float the numbers
pricenum=np.array([[float(str.replace(str.replace(a,'$',''),',',''))
for a in arow] for arow in pricestr])
return [ximpdat[1, 2: ] # Mathematica: ximpdat[[2, 3 ;; ]]
,ximpdat[2:, 0 ] # ximpdat[[ 3 ;; , 1 ]]
,np.asfarray(ximpdat[2:, 1 ]) # ximpdat[[ 3 ;; , 2 ]]
,pricenum]
filepath='http://courses.egr.uh.edu/ECE/ECE3340/Homework/portfolio_data.csv';
[dateList, stocksymb, shares, pricedata]=datacsvimp(filepath)
print(dateList)
print(stocksymb)
print(shares)
pricedata matrix can be big. Don't print if not want
value=np.dot(shares,pricedata)
plt.figure(figsize=(10,6.7))
plt.plot(dateList,value)
The idea is that there is no need to create any new code. The problem is structurally the same as the Portfolio, hence use the same code, with exception being variable names for our benefits (not computer).
filepath='http://courses.egr.uh.edu/ECE/ECE3340/Homework/grocery_price_data.csv';
[storename, item, itemquantity, priceGrocdata]=datacsvimp(filepath)
print(storename)
print(item)
print(itemquantity)
The price matrix is not too big, we can print out to show:
print(priceGrocdata)
cost=np.dot(itemquantity,priceGrocdata)
plt.bar(storename,cost,color='g')