站酷设计官方网站,做网站源代码,安徽建设工程信息网站,最好的网站建设哪家好#x1f935;♂️ 个人主页#xff1a;艾派森的个人主页 ✍#x1f3fb;作者简介#xff1a;Python学习者 #x1f40b; 希望大家多多支持#xff0c;我们一起进步#xff01;#x1f604; 如果文章对你有帮助的话#xff0c; 欢迎评论 #x1f4ac;点赞#x1f4… ♂️ 个人主页艾派森的个人主页 ✍作者简介Python学习者 希望大家多多支持我们一起进步 如果文章对你有帮助的话 欢迎评论 点赞 收藏 加关注 目录
1.项目背景
2.数据集介绍
3.技术工具
4.导入数据
5.数据可视化
源代码 1.项目背景 在电子商务蓬勃发展的今天欺诈性交易已成为一个不容忽视的问题。随着消费者对于在线购物的依赖程度不断加深电子商务市场的规模持续扩大但与此同时欺诈行为也日益猖獗。这些欺诈行为不仅损害了消费者的权益降低了他们对电子商务的信任度还严重干扰了商家的正常经营秩序对整个电子商务生态系统造成了巨大的冲击。 电子商务欺诈行为的多样性和隐蔽性给防范和打击带来了极大的挑战。欺诈者可能采用各种手段如伪造身份、发布虚假商品信息、恶意刷单、虚假评价等以达成非法获利的目的。这些欺诈行为往往隐藏在庞大的交易数据中难以被直接识别和发现因此需要借助先进的数据分析技术来揭示其中的规律和特点。 数据驱动的欺诈防范策略在电子商务领域变得尤为重要。随着大数据和人工智能技术的快速发展对大量的电子商务交易数据进行深入分析和挖掘成为可能。通过构建欺诈性电子商务交易数据集并利用可视化分析技术将数据以直观、易懂的方式呈现出来可以帮助分析人员快速理解数据中的信息发现潜在的欺诈风险并据此制定针对性的防范和应对策略。 可视化分析在欺诈防范中发挥着至关重要的作用。它可以将复杂的数据转化为图表、图像等易于理解的视觉形式帮助分析人员迅速捕捉数据中的关键信息和异常模式。通过对欺诈性电子商务交易数据集的可视化分析我们可以更深入地了解欺诈行为的特点和规律发现欺诈行为的潜在趋势和关联性为制定更加有效的防范措施提供科学依据。 因此研究欺诈性电子商务交易数据集的可视化分析具有重要的理论意义和实践价值。它不仅有助于我们深入理解欺诈行为的本质和规律还有助于提升电子商务市场的安全性和可信度保护消费者和商家的合法权益促进电子商务的健康发展。
2.数据集介绍 本实验数据集来源于Kaggle原始数据集分为训练集和测试集其中训练集共有1472952条数据16个变量。各变量含义解释如下
Transaction ID:每个事务的唯一标识符。
Customer ID:每个客户的唯一标识符。
Transaction Amount:交易中交易的总金额。
Transaction Date:交易发生的日期和时间。
Payment Method:用于完成交易的方式(如信用卡、PayPal等)。
Product Category:交易中涉及的产品类别。
Quantity:交易中涉及的产品数量。
Customer Age:进行交易的客户的年龄。
Transaction Date:客户的地理位置。
Device Used:用于进行交易的设备类型(例如移动设备、桌面设备)。
IP Address:用于交易的设备的IP地址。
Shipping Address:产品发货的地址。
Billing Address :与付款方式相关联的地址。
Is Fraudulent:表示事务是否欺诈性的二进制指示器(1表示欺诈性0表示合法)。
Account Age Days:客户账户在交易时的存续天数。
Transaction Hour:交易发生的当天的时间。
3.技术工具
Python版本:3.9
代码编辑器jupyter notebook
4.导入数据
导入数据分析第三方库 导入数据集 查看数据集大小 查看数据基本信息 查看数值型变量的描述性统计 我们可以看到客户年龄最小值是-16这是不可能的所以我们必须修复客户年龄列。
查看非数值型变量的描述性统计 统计数据缺失值情况 可以发现数据集中并不存在缺失值
统计重复值情况 可以发现数据集中并不存在重复值
使用箱线图查看客户年龄分布 定义一个数据预处理函数并处理我们的数据集 再次查看数据基本信息 数据集已被清理和压缩其大小从180 MB减少到57MB。
5.数据可视化 交易金额普遍分布在0到1000之间且数据是右偏的。 源代码
Transaction ID:每个事务的唯一标识符。
Customer ID:每个客户的唯一标识符。
Transaction Amount:交易中交易的总金额。
Transaction Date:交易发生的日期和时间。
Payment Method:用于完成交易的方式(如信用卡、PayPal等)。
Product Category:交易中涉及的产品类别。
Quantity:交易中涉及的产品数量。
Customer Age:进行交易的客户的年龄。
Transaction Date:客户的地理位置。
Device Used:用于进行交易的设备类型(例如移动设备、桌面设备)。
IP Address:用于交易的设备的IP地址。
Shipping Address:产品发货的地址。
Billing Address :与付款方式相关联的地址。
Is Fraudulent:表示事务是否欺诈性的二进制指示器(1表示欺诈性0表示合法)。
Account Age Days:客户账户在交易时的存续天数。
Transaction Hour:交易发生的当天的时间。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import time
import warnings
warnings.filterwarnings(ignore)train_df pd.read_csv(Fraudulent_E-Commerce_Transaction_Data.csv)
train_df.head()
train_df.shape
train_df.info()
train_df.describe()
我们可以看到客户年龄最小值是-16这是不可能的所以我们必须修复客户年龄列
train_df.describe(includeO)
train_df.isnull().sum()
train_df.duplicated().sum()
px.box(data_frametrain_df,xCustomer Age,width500,height300)
def clean_data(df) - pd.DataFrame: ##我们需要将**Transaction Date**列从对象类型转换为日期时间格式。df[Transaction Date] pd.to_datetime(df[Transaction Date])## 从交易日期提取日、周、月df[Transaction Day] df[Transaction Date].dt.daydf[Transaction DOW] df[Transaction Date].dt.day_of_weekdf[Transaction Month] df[Transaction Date].dt.month## 修复客户年龄mean_value np.round(df[Customer Age].mean(),0) df[Customer Age] np.where(df[Customer Age] -9, np.abs(df[Customer Age]), df[Customer Age])df[Customer Age] np.where(df[Customer Age] 9, mean_value, df[Customer Age])## 如果“Shipping Address”与“Billing Address”相同则取值为1否则取值为0。df[Is Address Match] (df[Shipping Address] df[Billing Address]).astype(int)### 删除不相关的特征并向下转换数据类型以减小数据集大小df.drop(columns[Transaction ID, Customer ID, Customer Location,IP Address, Transaction Date,Shipping Address,Billing Address], inplaceTrue)int_col df.select_dtypes(includeint).columnsfloat_col df.select_dtypes(includefloat).columnsdf[int_col] df[int_col].apply(pd.to_numeric, downcastinteger)df[float_col] df[float_col].apply(pd.to_numeric, downcastfloat)return dftrain_df clean_data(train_df)
train_df.head()
train_df.info()
数据集已被清理和压缩其大小从180 MB减少到57MB。
plt.figure(figsize(10,4))
sns.histplot(train_df[Transaction Amount],bins200)
plt.show()
交易金额普遍分布在0到1000之间
数据是右偏的。
payment_count train_df[Payment Method].value_counts()
plt.figure(figsize(15,4))
plt.subplot(1,2,1)
sns.set_palette(pastel)
colors sns.color_palette()
plt.pie(payment_count,labels payment_count.index,shadowTrue,autopct%1.1f%%,colorscolors,wedgepropsdict(width0.8,edgecolorw))
plt.title(Payment Method)
plt.subplot(1,2,2)
sns.countplot(datatrain_df,xPayment Method,edgecolorblack,linewidth1, paletteSet2)
plt.show()
我们可以看到所有的付款方式都是平均分配的
category_count train_df[Product Category].value_counts()
plt.figure(figsize(15,4))
plt.subplot(1,2,1)
sns.set_palette(pastel)
colors sns.color_palette()
plt.pie(category_count,labels category_count.index,shadowTrue,autopct%1.1f%%,colorscolors,wedgepropsdict(width0.8,edgecolorw))
plt.title(Product Category)plt.subplot(1,2,2)
ax sns.countplot(datatrain_df,xProduct Category,edgecolorblack,linewidth1, paletteSet2)
我们可以看到所有的产品类别也是均匀分布的
quantity_count train_df[Quantity].value_counts()
plt.figure(figsize(15,4))
plt.subplot(1,2,1)
sns.set_palette(pastel)
colors sns.color_palette()
plt.pie(quantity_count,labels quantity_count.index,shadowTrue,autopct%1.1f%%,colorscolors,wedgepropsdict(width0.8,edgecolorw))
plt.title(Quantity)
plt.subplot(1,2,2)
ax sns.countplot(datatrain_df,xQuantity,edgecolorblack,linewidth1, paletteSet2)
plt.figure(figsize(7,4))
sns.histplot(datatrain_df, xCustomer Age,bins150,kdeTrue,colororange)
plt.show()
device_count train_df[Device Used].value_counts()
plt.figure(figsize(15,4))
plt.subplot(1,2,1)
sns.set_palette(pastel)
colors sns.color_palette()
plt.pie(device_count,labels device_count.index,shadowTrue,autopct%1.1f%%,colorscolors,wedgepropsdict(width0.8,edgecolorw))
plt.title(Device Used)
plt.subplot(1,2,2)
ax sns.countplot(datatrain_df,xDevice Used,edgecolorblack,linewidth1, paletteSet2)
hour_count train_df[Transaction Hour].value_counts().head(15)
plt.figure(figsize(10,4))
sns.set_palette(Set2)
colors sns.color_palette()
axsns.barplot(xhour_count.index, yhour_count.values,palettecolors)
plt.xticks(rotation80)
plt.show()
plt.figure(figsize(5,3))
sns.violinplot(datatrain_df, xIs Fraudulent, yTransaction Amount)
plt.show()
column [Payment Method, Product Category, Quantity, Device Used,Transaction DOW, Transaction Month,Is Address Match]
plt.figure(figsize(10,35))
plot_num 1
for col in column:plt.subplot(10,2,plot_num)sns.countplot(datatrain_df, xcol, hueIs Fraudulent)plt.xticks(rotation90)plt.title(col)plt.tight_layout()plot_num 1
plt.figure(figsize(10,6))
plt.subplot(1,2,1)
sns.boxenplot(xIs Fraudulent, yTransaction Amount, datatrain_df)
plt.subplot(1,2,2)
sns.boxenplot(xIs Fraudulent, yTransaction Day, datatrain_df)
plt.yticks(np.arange(0,32))
plt.show()
资料获取更多粉丝福利关注下方公众号获取