利用pandas进行数据分析

人工智能34

1.系统环境:
Windows10: 20H2
MySQL:8.0.23
3.项目背景
(1)
如今的处在一个数据爆炸性增长的大数据时代,自阿里提出"数据赋能"以来,越来越多的电商企业和运营开始关注数据的应用。日常业务产生的海量数据蕴含着巨大的价值,如何在现在竞争激烈的环境中生存下来,其中最重要的一个因素就是数据化运营。
在这样的背景下,我将基于pandas进行数据分析,探索用户行为规律,从而提供有价值的信息。
(2)
皮尔逊乘积矩相关系数,又称皮尔逊乘积矩相关系数,一般用来分析两个连续变量之间的关系,是一种线性相关系数。

[En]

Pearson product moment correlation coefficient, also known as Pearson product moment correlation coefficient, is generally used to analyze the relationship between two continuous variables, which is a linear correlation coefficient.

补充:
|r|

3.用户行为日志avocado.csv,日志中的字段定义如下:
Date | 日期
AveragePrice | 平均价格
Total Volume | 总交易量
4046 | 售出 PLU 4046 的鳄梨总数
4225 | 售出 PLU 4225 的鳄梨总数
4770 | 售出 PLU 4770 的鳄梨总数
Total Bags |总袋数
Small Bags | 小包
Large Bags | 大包
XLarge Bags | 特大包
Type | 是否有机
Year | 哪一年
Region | 地区

4.明确问题
本文将对鳄梨的销售数据进行分析,试图分析其中有价值的信息。

[En]

This paper will analyze the avocado sales data and try to analyze the valuable information.

5.数据来源
数据来自Kaggle社区
网址:https://www.kaggle.com/neuromusic/avocado-prices

6.数据分析
首先导入数据,并查看数据的统计信息:

import pandas as pd
df = pd.read_csv("D:/迅雷下载/archive/avocado.csv")
pd.set_option('display.max_columns', None)
df['AveragePrice'].mean()
df['AveragePrice'].median()
df['AveragePrice'].mode()
df.describe().round(2).T
 然后探究一下各产地总和:
df["region"].value_counts()
 发现数据很均匀,然后探究一下各地的平均价格:
df[['AveragePrice','region']].groupby(by='region').mean().sort_values('AveragePrice',ascending=False)

调查发现,最贵的哈特福德和最便宜的休斯顿之间存在很大的价差,这里我们想知道,除了地理上的原因,是不是因为有机价格更贵,导致了两地的价格差距很大?

[En]

It is found that there is a big price gap between the most expensive Hartford and the cheapest Houston, and here we wonder, apart from the geographical reasons, is it because the organic price is more expensive that leads to a large price gap between the two places?

df[['AveragePrice','type']].groupby(by='type').mean().sort_values('AveragePrice',ascending=False)

看看有机和普通的平均价格,发现确实有一定的差距,再看看两地的有机和普通的平均价格:

[En]

Look at the organic and ordinary average prices, and find that there is indeed a certain gap, and then look at the organic and ordinary average prices between the two places:

df.loc[df['region'] == 'HartfordSpringfield'].groupby(by="type").mean()
df.loc[df['region'] == 'Houston'].groupby(by="type").mean()
发现两地有机和普通价格差距都很大,再查看有机和普通的数量:
df.loc[df['region'] == 'HartfordSpringfield']['type'].value_counts()
df.loc[df['region'] == 'Houston']['type'].value_counts()

发现数量相同,应对现有数据进行处理。结合前两地的平均价格发现,哈特福德牛油果并不贵,因为它们消费的有机牛油果更贵。休斯顿牛油果更便宜,因为他们吃得更多,看看这两个地方消费的牛油果数量:

[En]

It is found that the quantity is the same, and the available data should be processed. Combined with the average prices of the previous two places, it is found that Hartford avocados are not expensive because they consume more expensive organic avocados. Houston avocados are cheaper because they eat more, check the number of avocados consumed in the two places:

sum(df.loc[df['region'] == 'HartfordSpringfield']['Total Bags'])
sum(df.loc[df['region'] == 'Houston']['Total Bags'])

发现休斯顿消耗的牛油果数量是哈特福德的五倍左右,猜想初步成立,根据字段Small Bags、Large Bags的提示再次猜想,休斯顿牛油果便宜是因为他们一次性买更多牛油果,查看数据:

sum(df.loc[df['region'] == 'HartfordSpringfield']['Small Bags'])
sum(df.loc[df['region'] == 'Houston']['Small Bags'])

发现哈特福德牛油果小包购买率达95.11%,而休斯顿的牛油果小包购买率64.77%,好像相关,计算皮尔逊相关系数:

import numpy as np
u1,u2 = df['AveragePrice'].mean(),(df['Small Bags']/df['Total Bags']).mean()
df['(x-u1)*(y-u2)'] = (df['AveragePrice'] - u1) * ((df['Small Bags']/df['Total Bags']) - u2)
df['(x-u1)**2'] = (df['AveragePrice'] - u1)**2
df['(y-u2)**2'] = ((df['Small Bags']/df['Total Bags']) - u2)**2
r = df['(x-u1)*(y-u2)'].sum() / (np.sqrt(df['(x-u1)**2'].sum() * df['(y-u2)**2'].sum()))
print('Pearson相关系数为:%.4f' % r)

发现两者相关系数为0.1168,虽然是正相关,但是因为相关系数小于0.3所以不是线性相关,猜想错误。
再次猜想,平均价格和年份有关系,查看数据:

df.loc[df['year'] == 2015]['AveragePrice'].mean()
df.loc[df['year'] == 2016]['AveragePrice'].mean()
df.loc[df['year'] == 2017]['AveragePrice'].mean()
df.loc[df['year'] == 2018]['AveragePrice'].mean()

发现牛油果的平均价格是先在2016年小幅降低,再在2017年大幅升高,最后回归到正常价格。
7.总结
在这次数据分析中,我用了pandas进行数据的筛选、数据的描述和数据的分组和聚合,利用了皮尔逊计算相关系数,简单地分析了数据,合理提出猜想,并验证猜想,得出结论,但缺点是没有将时间和变化联系起来,不能很好的动态反应价格随时间的变化。
在本次实训中学到的关于前端、web和日志的相关技术也没有很好的利用上,内容比较单薄,不够详实充分。

Original: https://blog.csdn.net/heH/article/details/122478443
Author: he
H
Title: 利用pandas进行数据分析

相关文章
YAML块样式 人工智能

YAML块样式

YAML包括两种块标量样式:字面和折叠。块标量由少量指标控制,标题在内容本身之前。 块标量头的示例如下 - %YAML 1.2 !!map { ? !!str "strip" : ...
keras安装教程 人工智能

keras安装教程

keras安装教程 本文主要介绍了win10安装Anaconda+tensorflow2.0-CPU+keras教程,主要针对本人在安装keras时h5py会报错的情况的安装方式,如果有相同问题可参考...
深度学习进行人体的姿态估计 人工智能

深度学习进行人体的姿态估计

深度学习进行人体姿态估计 简介 内容 * 什么是姿态估计? - 自下而上与自上而下的方法 姿态估计的重要性 什么是人体姿态估计? - 什么是2D人体姿态估计? 什么是3D人体姿态估计? 三维人体建模 ...
语音压缩编解码器:lyra 人工智能

语音压缩编解码器:lyra

语音压缩编解码器:lyra New Lyra 是 Google 开源的超低比特率,却拥有超高语音质量的编解码器,即便在很糟糕的网络情况下,lyra 也能让你有流畅的语音体验。Lyra 编解码器的基本架...
【课程笔记】中科大信息论(四) 人工智能

【课程笔记】中科大信息论(四)

马尔可夫过程的熵率 熵率 为什么要研究熵率 除了研究i.i.d.随机变量的熵,之前已经从条件熵的角度研究了两个关联的随机变量之间的关系,现在想进一步研究 当这些随机变量来自于某个随机过程时的情况。我们...