制作移动网站公司,2019年建设什么网站好,福田网站建设信科网络,网站做成app【实验名称】
实验一#xff1a;绘制板块层级图
【实验目的】 1. 掌握数据文件读取 2. 掌握数据处理的方法 3. 实现板块层级图的绘制 【数据介绍】Instacart Market Basket Analysis 1. 数据说明 数据共有300 0000orders#xff0c; 20 0000users#xff0c; …【实验名称】
实验一绘制板块层级图
【实验目的】 1. 掌握数据文件读取 2. 掌握数据处理的方法 3. 实现板块层级图的绘制 【数据介绍】Instacart Market Basket Analysis 1. 数据说明 数据共有300 0000orders 20 0000users 5000products 每个user提供有4-100个orders 2. 各数据内容了解 aisles产品摆放位置说明 order_products__prior订单产品关联表 orders.csv: 用户下单记录表。 products.csv: 产品ID分类及其摆放位置的关系表 departments.csv 产品分类表 3. 目标分析 目标是预测用户下次购买时可能再次购买的产品。 即用户历史购买的产品那些是用户下次购买还会购买的。 4. 训练数据构建 order_id, product_id(订单中的一个产品) lable是否下次购买。 1产品特征 1产品被购买次数。 2产品被重复购买次数 3产品被重复购买次数/总的购买次数。 4产品在不同week被购买次数 5产品在不同hour被购买次数。 2用户特征 1用户总下单次数。 2用户总购买量。 3用户每单平均购买量。 4用户距离上一次购物时间。 5用户频繁购买是周几。 6用户购买当天小时。 7用户购买商品数去重 8用户购买最多的商品 7用户购买最少的商品。 9用户在不同week购买最多以及最少的商品。 10用户在不同hour购买最多以及最少的商品。 3user_products特征 1该用户购买该商品次数/该用户下单次数。 2该用户上一次购买该商品距离现在天数。 3该用户上一次购买该商品平均week日期。 4该用户上一次购买该商品平均时间。 5该用户购买该商品的频率 Instacart 的数据科学团队在提供这种令人愉悦的购物体验方面发挥着重要作用。目前他们使用交易数据来开发模型以预测用户在会话期间会再次购买、首次尝试或下次添加到购物车的产品。
无论您是从精心策划的购物清单中购物还是让奇思妙想引导您放牧我们独特的美食仪式都定义了我们是谁。Instacart 是一款杂货订购和送货应用程序旨在让您在需要时轻松地将您个人最喜欢的和主食装满您的冰箱和食品储藏室。通过 Instacart 应用程序选择产品后个人购物者会查看您的订单并为您进行店内购物和送货。
Instacart 的数据科学团队在提供这种令人愉悦的购物体验方面发挥着重要作用。目前他们使用交易数据来开发模型以预测用户在会话期间会再次购买、首次尝试或下次添加到购物车的产品。最近Instacart 开源了这些数据 - 请参阅他们的博客文章 300 万个 Instacart 订单。 【实验原理】
板块层级图treemap是一种基于面积的可视化方式通过每一个板块通常为矩形的尺寸大小进行度量。外部矩形代表父类别而内部矩形代表子类别。我们也可以通过板块层级图简单的呈现比例关系不过它更擅于呈现树状结构的数据。
读取绘图所用的数据并对数据进行处理将数据处理成我们可以使用的形式绘制板块层级图设置标签和标题。
【实验环境】
Windows 11python3.11.1pycharm professional 2024.2.1jupyter notebook
【实验步骤】
题目一安装pandas、matplotlib、seaborn、squarify
1、输入命令pip install pandas 2、输入命令pip install matplotlib 3、输入命令pip install seaborn 输入命令pip install squarify 题目二读取数据 在这里我们使用pandas库中的read_csv函数来读取这3个数据文件。
import pandas as pdproducts_df pd.read_csv(products.csv)
aisles_df pd.read_csv(aisles.csv)
departments_df pd.read_csv(departments.csv)
aisles_df.head(10) 数据读取的结果aisles_df部分数据读取结果 题目三数据预处理
我们需要根据源表对目标表进行匹配查询使用merge函数进行操作。
order_products_prior_df pd.merge(products_df, aisles_df, onaisle_id, howleft)
order_products_prior_df pd.merge(order_products_prior_df, departments_df, ondepartment_id, howleft)
order_products_prior_df.head()
temp order_products_prior_df[[product_name, aisle, department]]
temp pd.concat([order_products_prior_df.groupby(department)[product_name].nunique().rename(products_department),order_products_prior_df.groupby(department)[aisle].nunique().rename(aisle_department)
], axis1).reset_index()
temp temp.set_index(department)
temp2 temp.sort_values(byaisle_department, ascendingFalse)
进行匹配操作后的数据。
print(temp) print(temp2) 题目四绘制板块层级图
1.绘制初始的板块层级图
cmap matplotlib.cm.viridis
mini, maxi temp2.products_department.min(), temp2.products_department.max()
norm matplotlib.colors.Normalize(vminmini, vmaxmaxi)
colors [cmap(norm(value)) for value in temp2.products_department]
colors[1] #FBFCFE
labels [%s\n%d aisle num\n%d products num % (label) for label inzip(temp2.index, temp2.aisle_department, temp2.products_department)]
fig plt.figure(figsize(12, 10))
ax fig.add_subplot(111, aspectequal)
ax squarify.plot(temp2.aisle_department, colorcolors, labellabels, axax, alpha.7)绘制结果 2.设置x、y轴的属性
ax.set_xticks([])
ax.set_yticks([])3.添加图表标题
fig.suptitle(How are aisles organized within departments, fontsize20 )
4.添加数据标签
img plt.imshow([temp2.products_department], cmapcmap)
img.set_visible(False)
fig.colorbar(img, orientationvertical, shrink.96)
fig.text(.76, .9, numbers of products, fontsize14)这样我们的板块层级图就绘制完毕了 附录总代码
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib
import squarify
color sns.color_palette()
pd.options.mode.chained_assignment None # defaultwarn
products_df pd.read_csv(products.csv)
aisles_df pd.read_csv(aisles.csv)
departments_df pd.read_csv(departments.csv)
order_products_prior_df pd.merge(products_df, aisles_df, onaisle_id, howleft)
order_products_prior_df pd.merge(order_products_prior_df, departments_df, ondepartment_id, howleft)
order_products_prior_df.head()
temp order_products_prior_df[[product_name, aisle, department]]
temp pd.concat([order_products_prior_df.groupby(department)[product_name].nunique().rename(products_department),order_products_prior_df.groupby(department)[aisle].nunique().rename(aisle_department)
], axis1).reset_index()
temp temp.set_index(department)
temp2 temp.sort_values(byaisle_department, ascendingFalse)
print(temp)
print(temp2)
x 0
y 0
width 100
height 100
cmap matplotlib.cm.viridis
mini, maxi temp2.products_department.min(), temp2.products_department.max()
norm matplotlib.colors.Normalize(vminmini, vmaxmaxi)
colors [cmap(norm(value)) for value in temp2.products_department]
colors[1] #FBFCFE
labels [%s\n%d aisle num\n%d products num % (label) for label inzip(temp2.index, temp2.aisle_department, temp2.products_department)]
fig plt.figure(figsize(12, 10))
ax fig.add_subplot(111, aspectequal)
ax squarify.plot(temp2.aisle_department, colorcolors, labellabels, axax, alpha.7)
fig.suptitle(How are aisles organized within departments, fontsize20 )
ax.set_xticks([])
ax.set_yticks([])
img plt.imshow([temp2.products_department], cmapcmap)
img.set_visible(False)
fig.colorbar(img, orientationvertical, shrink.96)
fig.text(.76, .9, numbers of products, fontsize14)
plt.show()