import numpy as np
import matplotlib.pyplot as plt
(fruitSales,
makeupSales)=np.loadtxt(r'C:\Users\XXGC\Desktop\mall_sales.csv',\
delimiter=',',\
skiprows=1,\
usecols=(1,4),\
unpack=True)
covmDiag=covm.diagonal()
print('\n协方差矩阵的对角线元素是:\n',covmDiag)
convTrc=covm.trace()
print('\n协方差矩阵的迹是:\n',convTrc)
r=covm/(fruit_sales_r.std()*makeup_sales_r.std())
print('\n相关系数矩阵是:\n',r)
t=np.arange(len(fruit_sales_r))
plt.plot(t,fruit_sales_r,lw=1)
plt.plot(t,makeup_sales_r,lw=1)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
vegetable_sales = np.loadtxt(r'C:\Users\XXGC\Desktop\mall_sales.csv',
delimiter=',',
skiprows=1,
usecols=(2),
unpack=True)
vegetable_sales_r = np.diff(vegetable_sales)/ vegetable_sales[:-1]
covm = np.cov(vegetable_sales_r, makeup_sales_r)
print('协方差矩阵是:\n', covm)
covmDiag = covm.diagonal()
print('\n协方差矩阵的对角线元素是:\n', covmDiag)
convTrc = covm.trace()
print('\n协方差矩阵的迹是:\n', convTrc)
r = np.corrcoef(vegetable_sales_r, makeup_sales_r)
print('\n相关系数矩阵是:\n', r)
t = np.arange(len(vegetable_sales_r))
plt.plot(t, vegetable_sales_r, lw=1)
plt.plot(t, makeup_sales_r, lw=1)
plt.show()