机器学习项目——运用机器学习洞察青年消费趋势

1. 项目背景

        在21世纪的第三个十年,全球经济和技术的飞速发展正深刻影响着各个领域,尤其是青年消费市场。随着数字化进程的加速,尤其是移动互联网的广泛普及,青年的消费行为和生活方式发生了前所未有的转变。

        然而,面对这一迅速变化的消费行为,如何及时、精准地分析并预测趋势,成为了商家制定市场策略的关键挑战。此时,机器学习技术作为强大的工具,能够帮助我们从海量数据中挖掘出有价值的洞见,提供更加科学的市场预测。

        在这一背景下,本研究旨在利用机器学习技术,对青年文化消费数据进行深入分析。通过对消费数据的预处理、清洗、特征工程等步骤,我们不仅能够从数据中揭示青年消费者的偏好和行为模式,还可以通过构建预测模型,准确预估未来的消费趋势。我们相信,这些研究成果将为企业制定更具针对性的营销策略、抢占市场先机提供强有力的支持。

2. 数据集介绍

        研究的数据来源于Kaggle,一个著名的数据科学竞赛平台,提供了丰富的数据集供研究和分析使用。各变量含义如下:

Customer ID:每个数据项都有一个唯一的顾客ID。
Age:显示每位顾客的年龄。
Gender:如Male(男性)或Female(女性),展示了顾客的性别。
Item Purchased:记录了每位顾客购买的商品类别,如Blouse(女衬衫)、Sweater(毛衣)等。
Category:如Clothing(服装),标识了商品所属的大类。
Purchase Amount (USD):显示了每笔交易的购买金额(单位为美元)。
Location:如Kentucky、Maine等,展示了每位顾客的地理位置。
Size:记录了商品的尺码,如S(小号)、L(大号)。
Color:如Gray(灰色)、Maroon(栗色)等,展示了顾客所选商品的颜色偏好。
Season:如Winter(冬季)、Spring(春季)等,表明商品的季节性。
Review Rating:顾客对商品的评分(如3.1)。
Subscription Status:显示顾客是否订阅了我们的服务(如Yes表示已订阅)。
Shipping Type:如Express(快速配送)、Free Shipping(免费配送),展示了顾客选择的配送方式。
Discount Applied:显示顾客是否享受了折扣(如Yes表示有折扣)。
Promo Code Used:记录了顾客是否使用了促销码(如Yes表示使用了促销码),帮助我们评估促销活动的效果。
Previous Purchases:如14、2等,展示了顾客的历史购买次数。
Payment Method:如Venmo、Cash、Credit Card,展示了顾客使用的支付方式。
Frequency of Purchases:如Fortnightly(每两周一次)、Weekly(每周一次),展示了顾客的购物频率,帮助我们预测未来的购买行为。

3. 技术工具

Python版本:3.9

代码编辑器:pycharm

4. 导入数据

        数据准备和预处理是数据分析项目中至关重要的步骤,它直接影响到分析结果的质量和准确性。首先通过Pandas等库对数据进行初步的导入和清洗。以下是一个详细的步骤解析,结合上述代码,解释如何进行数据准备和预处理。

数据筛选

import pandas as pd
import numpy as np
data = pd.read_csv(r"E:shopping_trends.csv")
filtered_data = data[(data['Age'] >= 15) & (data['Age'] <= 35)]

数据清洗

filtered_data['Gender'] = filtered_data['Gender'].map({'Male': 0, 'Female': 1})

filtered_data.drop(['Location','Item Purchased'], axis=1, inplace=True)
for column in filtered_data.columns:
    if pd.api.types.is_numeric_dtype(filtered_data[column]):
        filtered_data[column].fillna(filtered_data[column].mean(), inplace=True)
    else:
        filtered_data[column].fillna(filtered_data[column].mode()[0], inplace=True)
integer_columns = ['Review Rating', 'Previous Purchases']
filtered_data[integer_columns] = filtered_data[integer_columns].astype(int)

数据导出

filtered_data.to_csv(r"E:\\processed_shopping_trends.csv", index=False)

5. 数据可视化

        在对青年消费行为的深入分析中,通过性别分布、支付方式偏好、不同类别的平均购买金额、季节性消费行为,以及购买频率分布五个维度进行了全面的探讨。结果揭示了青年在文化消费上的多样化趋势和细微差异。

        性别分布的可视化分析可以清晰发现青年在不同性别中的文化消费行为差异。这一分析帮助揭示男女在文化消费上的偏好、消费能力以及参与程度的不同。

plt.figure(figsize=(8, 4))
sns.countplot(x='Gender', data=df)
plt.title('Gender Distribution')
plt.xlabel('Gender (0: Male, 1: Female)')
plt.ylabel('Count')
plt.xticks(rotation=45)  
plt.tight_layout()
plt.show()

        季节性消费行为的可视化分析帮助理解青年在不同季节的消费偏好变化。这种变化不仅受天气、假期安排的影响,也与特定季节文化活动的举办紧密相关。

plt.figure(figsize=(8, 5))
sns.countplot(x='Season', data=df)
plt.title('Seasonal Purchase Behavior')
plt.xlabel('Season')
plt.ylabel('Number of Purchases')
plt.show()

        购买频率的分析可以体现出青年的消费欲望,有些青年会根据自己的心情来进行消费,也是从侧面反映出他们的情绪。

plt.figure(figsize=(12, 6))
sns.countplot(x='Frequency of Purchases', data=df)
plt.title('Frequency of Purchases Distribution')
plt.xlabel('Frequency of Purchases')
plt.xticks(rotation=25)
plt.ylabel('Count')
plt.show()

6.机器模型

        在分析和预测青年的文化消费行为时,特征选择和模型训练策略显得尤为重要。首先,在数据预处理阶段,将所有分类变量转换为数值型,以确保数据集的整洁性和可用性。

        在特征选择的过程中,采用随机森林回归模型来评估不同特征对目标变量(例如购买金额)的贡献度。随机森林是一种集成学习技术,通过组合多个决策树的预测结果来增强模型的整体稳定性和准确度。

导入数据与初步处理

data = pd.read_csv("E:\\processed_shopping_trends.csv")
df = pd.DataFrame(data)
df_encoded = pd.get_dummies(df, columns=['Color', 'Frequency of Purchases', 'Payment Method','Shipping Type','Size','Category','Season','Gender'], drop_first=True)

特征选择

features = ["Previous Purchases", "Age",'Review Rating'] + [col for col in df_encoded.columns if col.startswith(('Color_', 'Frequency_', 'Payment_','Shipping Type','Size','Category','Season','Gender'))]
X = df_encoded[features]
y = df_encoded['Purchase Amount (USD)']

划分训练集和测试集

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

随机森林模型训练

rf = RandomForestRegressor(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)

模型预测与评估

y_pred = rf.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
rmse = sqrt(mse)
r2 = r2_score(y_test, y_pred)
print(f"均方误差(MSE): {mse}")
print(f"均方根误差(RMSE): {rmse}")
print(f"R^2分数: {r2}")

预测新客户的购买金额

data = {
    'Previous Purchases': [5, 15, 8],
    'Age': [25, 35, 29],
    'Review Rating': [4.5, 3.5, 4.0],
    'Color_Blue': [1, 0, 0],
    'Color_Black': [0, 1, 0],
    'Color_Gray': [0, 0, 1],
    'Shipping Type_Standard': [1, 0, 0],
    'Shipping Type_Express': [0, 1, 0],
    'Shipping Type_Free Shipping': [0, 0, 1],
    'Category_Clothing': [1, 0, 0],
    'Category_Outerwear': [0, 1, 0],
    'Category_Footwear': [0, 0, 1],
    'Season_Spring': [1, 0, 0],
    'Season_Winter': [0, 1, 0],
    'Season_Summer': [0, 0, 1],
    'Gender_1': [1, 0, 1]
}
customer_df = pd.DataFrame(data)

确保所有特征列都完整

all_possible_features = [
    'Previous Purchases', 'Age', 'Review Rating',
    'Color_Black', 'Color_Blue', 'Color_Brown', 'Color_Charcoal', 'Color_Cyan', 'Color_Gold',
    'Color_Gray', 'Color_Green', 'Color_Indigo', 'Color_Lavender', 'Color_Magenta', 'Color_Maroon',
    'Color_Olive', 'Color_Orange', 'Color_Peach', 'Color_Pink', 'Color_Purple', 'Color_Red',
    'Color_Silver', 'Color_Teal', 'Color_Turquoise', 'Color_Violet', 'Color_White', 'Color_Yellow',
    'Shipping Type_Express', 'Shipping Type_Free Shipping', 'Shipping Type_Next Day Air',
    'Shipping Type_Standard', 'Shipping Type_Store Pickup',
    'Size_M', 'Size_S', 'Size_XL',
    'Category_Clothing', 'Category_Footwear', 'Category_Outerwear',
    'Season_Spring', 'Season_Summer', 'Season_Winter',
    'Gender_1'
]
for feature in all_possible_features:
    if feature not in customer_df.columns:
        customer_df[feature] = 0
customer_df = customer_df[all_possible_features]

预测新客户的购买金额

predictions = rf.predict(customer_df)
print(f"客户1的预测购买金额: ${predictions[0]:.2f}")
print(f"客户2的预测购买金额: ${predictions[1]:.2f}")
print(f"客户3的预测购买金额: ${predictions[2]:.2f}")

        在当前的研究中,主要采用了机器学习技术,结合了数据挖掘方法,对校园青年的消费行为进行了深入分析。通过建立和应用机器学习模型,能够较全面地识别和理解青年消费偏好的多样性和复杂性,并揭示年龄、性别、季节等因素对其消费选择的显著影响。随机森林模型以其出色的准确性和健壮性,在多个领域已被广泛应用,例如在金融风险评估、医疗病情诊断、市场趋势预测等方面均展现出了极高的实用性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值