Tutorial 3 - support

ECE Generic - University of Houston


Han Q Le (c) - copyrighted

This shows only the structure of a problem matters, not the content.

Portfolio examples

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Below, we will do "translation" of Mathemetica code into python

In [6]:
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]
In [7]:
filepath='http://courses.egr.uh.edu/ECE/ECE3340/Homework/portfolio_data.csv';
[dateList, stocksymb, shares, pricedata]=datacsvimp(filepath)
In [8]:
print(dateList)
print(stocksymb)
print(shares)
['12/31/2014' '3/31/2015' '6/30/2015' '9/30/2015' '12/31/2015' '3/31/2016'
 '6/30/2016' '9/30/2016' '12/31/2016' '3/31/2017' '6/30/2017' '9/30/2017'
 '12/31/2017' '3/31/2018' '6/30/2018' '9/30/2018' '12/31/2018' '3/31/2019'
 '6/30/2019' '9/30/2019' '12/31/2019']
['FB' 'AAPL' 'AMZN' 'NFLX' 'GOOG' 'MMM' 'JNJ' 'XOM' 'V' 'JPM' 'HD' 'CVS'
 'INTC' 'QQQ' 'SPY' 'IBB' 'FNCMX' 'VFINX' 'PRULX' 'PRTNX']
[ 75. 100.  30. 150.  30.  75. 100.  75. 200. 100. 100.  80. 225. 200.
 100.  80. 120.  50. 600. 800.]

pricedata matrix can be big. Don't print if not want

In [9]:
value=np.dot(shares,pricedata)
In [11]:
plt.figure(figsize=(10,6.7))
plt.plot(dateList,value)
Out[11]:
[<matplotlib.lines.Line2D at 0x1c997267fd0>]

Grocery shopping

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).

In [12]:
filepath='http://courses.egr.uh.edu/ECE/ECE3340/Homework/grocery_price_data.csv';
[storename, item, itemquantity, priceGrocdata]=datacsvimp(filepath)
In [13]:
print(storename)
print(item)
print(itemquantity)
['store A' 'store B' 'store C' 'store D']
['juice' 'eggs' 'fruits' 'vegetables' 'milk' 'cereals' 'coffee' 'tea'
 'ice cream' 'napkins' 'foils' 'storage bags' 'toothpaste' 'shampoo'
 'detergent']
[ 3.  4. 12.  8.  2.  6.  1.  2.  3.  5.  2. 10.  4.  3.  2.]

The price matrix is not too big, we can print out to show:

In [14]:
print(priceGrocdata)
[[ 1.55  1.45  1.65  1.4 ]
 [ 1.95  2.4   2.    2.2 ]
 [ 0.85  0.8   0.7   0.8 ]
 [ 1.35  1.    1.    1.1 ]
 [ 2.55  2.25  2.55  3.1 ]
 [ 2.7   3.35  3.05  3.45]
 [10.85  7.5   8.45  8.5 ]
 [ 4.2   4.15  3.75  3.95]
 [ 7.35  6.75  6.75  4.95]
 [ 1.1   1.2   1.2   1.25]
 [ 3.5   3.75  3.6   3.75]
 [ 0.8   0.7   0.85  0.7 ]
 [ 1.6   1.6   1.55  1.6 ]
 [ 3.55  2.6   2.9   2.7 ]
 [ 9.55  9.05  8.65  7.1 ]]
In [15]:
cost=np.dot(itemquantity,priceGrocdata)
In [23]:
plt.bar(storename,cost,color='g')
Out[23]:
<BarContainer object of 4 artists>
In [ ]: