import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
(fruitSales, makeupSales) = np.loadtxt(
"mall_sales.csv",
delimiter=",",
skiprows=1,
usecols=[1,4],
encoding="gbk",
unpack=True
)
print(f"水果销售额为:{fruitSales}")
print(f"化妆品销售额为:{makeupSales}")
fruit_sales_r = np.diff(fruitSales)/fruitSales[:-1]
makeup_sales_r = np.diff(makeupSales)/makeupSales[:-1]
covm = np.cov(fruit_sales_r,makeup_sales_r)
print("水果和化妆品的协方差矩阵为: \n",covm)
covmDiag = covm.diagonal()
print("协方差矩阵的对角线元素为: \n",covmDiag)
convTrc = covm.trace()
print("协方差矩阵的迹为: ",convTrc)
r1 = np.corrcoef(fruit_sales_r,makeup_sales_r)
print("相关系数矩阵为: \n",r1)
t = np.arange(len(fruit_sales_r))
plt.plot(t,fruit_sales_r,"r-",label="fruit")
plt.plot(t,makeup_sales_r,"g--",label="makeup")
plt.legend()
plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["font.family"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
df = pd.read_csv('mall_sales.csv', encoding='gbk')
df['日期'] = pd.to_datetime(df['日期'])
df = df.sort_values('日期').reset_index(drop=True)
fruitSales = df['水果'].values
makeupSales = df['化妆品'].values
vegSales = df['蔬菜'].values
seafoodSales = df['海鲜'].values
fruit_sales_r = np.diff(fruitSales) / fruitSales[:-1]
makeup_sales_r = np.diff(makeupSales) / makeupSales[:-1]
veg_sales_r = np.diff(vegSales) / vegSales[:-1]
seafood_sales_r = np.diff(seafoodSales) / seafoodSales[:-1]
covm_veg_makeup = np.cov(veg_sales_r, makeup_sales_r)
print("蔬菜和化妆品的协方差矩阵为:\n", covm_veg_makeup)
print("协方差矩阵的对角线元素为:\n", covm_veg_makeup.diagonal())
print("协方差矩阵的迹为:", covm_veg_makeup.trace())
r_veg_makeup = np.corrcoef(veg_sales_r, makeup_sales_r)
print("蔬菜和化妆品的相关系数矩阵为:\n", r_veg_makeup)
t_veg_makeup = np.arange(len(veg_sales_r))
plt.plot(t_veg_makeup, veg_sales_r, "b--", label="vegetable")
plt.plot(t_veg_makeup, makeup_sales_r, "g--", label="makeup")
plt.legend()
plt.title("蔬菜和化妆品销售额增长率趋势")
plt.show()
covm_makeup_seafood = np.cov(makeup_sales_r, seafood_sales_r)
print("化妆品和海鲜的协方差矩阵为:\n", covm_makeup_seafood)
print("协方差矩阵的对角线元素为:\n", covm_makeup_seafood.diagonal())
print("协方差矩阵的迹为:", covm_makeup_seafood.trace())
r_makeup_seafood = np.corrcoef(makeup_sales_r, seafood_sales_r)
print("化妆品和海鲜的相关系数矩阵为:\n", r_makeup_seafood)
t_makeup_seafood = np.arange(len(makeup_sales_r))
plt.plot(t_makeup_seafood, makeup_sales_r, "r--", label="makeup")
plt.plot(t_makeup_seafood, seafood_sales_r, "c--", label="seafood")
plt.legend()
plt.title("化妆品和海鲜销售额增长率趋势")
plt.show()
covm_fruit_veg = np.cov(fruit_sales_r, veg_sales_r)
print("水果和蔬菜的协方差矩阵为:\n", covm_fruit_veg)
print("协方差矩阵的对角线元素为:\n", covm_fruit_veg.diagonal())
print("协方差矩阵的迹为:", covm_fruit_veg.trace())
r_fruit_veg = np.corrcoef(fruit_sales_r, veg_sales_r)
print("水果和蔬菜的相关系数矩阵为:\n", r_fruit_veg)
t_fruit_veg = np.arange(len(fruit_sales_r))
plt.plot(t_fruit_veg, fruit_sales_r, "m--", label="fruit")
plt.plot(t_fruit_veg, veg_sales_r, "y--", label="vegetable")
plt.legend()
plt.title("水果和蔬菜销售额增长率趋势")
plt.show()
covm_fruit_seafood = np.cov(fruit_sales_r, seafood_sales_r)
print("水果和海鲜的协方差矩阵为:\n", covm_fruit_seafood)
print("协方差矩阵的对角线元素为:\n", covm_fruit_seafood.diagonal())
print("协方差矩阵的迹为:", covm_fruit_seafood.trace())
r_fruit_seafood = np.corrcoef(fruit_sales_r, seafood_sales_r)
print("水果和海鲜的相关系数矩阵为:\n", r_fruit_seafood)
t_fruit_seafood = np.arange(len(fruit_sales_r))
plt.plot(t_fruit_seafood, fruit_sales_r, color="k", linestyle="--", label="fruit")
plt.plot(t_fruit_seafood, seafood_sales_r, color="orange", marker="o", linestyle="--", label="seafood")
plt.legend()
plt.title("水果和海鲜销售额增长率趋势")
plt.show()
covm_veg_seafood = np.cov(veg_sales_r, seafood_sales_r)
print("蔬菜和海鲜的协方差矩阵为:\n", covm_veg_seafood)
print("协方差矩阵的对角线元素为:\n", covm_veg_seafood.diagonal())
print("协方差矩阵的迹为:", covm_veg_seafood.trace())
r_veg_seafood = np.corrcoef(veg_sales_r, seafood_sales_r)
print("蔬菜和海鲜的相关系数矩阵为:\n", r_veg_seafood)
t_veg_seafood = np.arange(len(veg_sales_r))
plt.plot(t_veg_seafood, veg_sales_r, color="gray", linestyle="--", label="vegetable")
plt.plot(t_veg_seafood, seafood_sales_r, color="purple", marker="s", linestyle="--", label="seafood")
plt.legend()
plt.title("蔬菜和海鲜销售额增长率趋势")
plt.show()