diff --git a/pythonProject2/.idea/.gitignore b/pythonProject2/.idea/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..13566b81b018ad684f3a35fee301741b2734c8f4 --- /dev/null +++ b/pythonProject2/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/pythonProject2/.idea/inspectionProfiles/profiles_settings.xml b/pythonProject2/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000000000000000000000000000000000..105ce2da2d6447d11dfe32bfb846c3d5b199fc99 --- /dev/null +++ b/pythonProject2/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/pythonProject2/.idea/misc.xml b/pythonProject2/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..76656f0cbe86d876483a9e96a5f6d60971864bbf --- /dev/null +++ b/pythonProject2/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/pythonProject2/.idea/modules.xml b/pythonProject2/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..b7e217357c07833ed456a0331282b2b3fa653451 --- /dev/null +++ b/pythonProject2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/pythonProject2/.idea/pythonProject2.iml b/pythonProject2/.idea/pythonProject2.iml new file mode 100644 index 0000000000000000000000000000000000000000..2c80e1269497d12e018fd6afa29982e56b0fb70d --- /dev/null +++ b/pythonProject2/.idea/pythonProject2.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/pythonProject2/111 b/pythonProject2/111 new file mode 100644 index 0000000000000000000000000000000000000000..84c40a131379d432691d01da181cc33a8f56e402 --- /dev/null +++ b/pythonProject2/111 @@ -0,0 +1,66 @@ +import pandas as pd +import numpy as np +from sklearn.linear_model import LinearRegression +from sklearn.model_selection import train_test_split +from sklearn.metrics import mean_squared_error +import matplotlib.pyplot as plt + +# 假设我们有过去24个月的价格数据(随机生成) +np.random.seed(42) +dates = pd.date_range(start='2021-01-01', periods=24, freq='M') +products = ['洗发水', '沐浴露'] + +# 生成随机价格数据 +prices_data = pd.DataFrame(index=dates, columns=products) +for product in products: + base_price = np.random.randint(50, 100) + monthly_changes = np.random.normal(0, 5, 24) # 假设每月价格变化标准差为5 + prices = [base_price + sum(monthly_changes[:i+1]) for i in range(24)] + prices_data[product] = prices + +# 转换日期为索引,并添加月份作为特征(这里简单使用月份数字) +prices_data['Month'] = prices_data.index.month + +# 准备数据:特征和标签 +X = prices_data[['Month']] # 特征:月份 +y_shampoo = prices_data['洗发水'] # 标签:洗发水价格 +y_shower_gel = prices_data['沐浴露'] # 标签:沐浴露价格 + +# 分割数据集为训练集和测试集 +# 这里我们简单地将前18个月作为训练集,后6个月作为测试集 +X_train, X_test, y_train_shampoo, y_test_shampoo = train_test_split(X[:18], y_shampoo[:18], test_size=0.33, shuffle=False) +X_train, X_test, y_train_shower_gel, y_test_shower_gel = train_test_split(X[:18], y_shower_gel[:18], test_size=0.33, shuffle=False) + +# 训练线性回归模型 +model_shampoo = LinearRegression().fit(X_train, y_train_shampoo) +model_shower_gel = LinearRegression().fit(X_train, y_train_shower_gel) + +# 预测未来6个月的价格(这里我们简单使用训练集的月份范围外推) +future_months = np.arange(19, 25) +future_X = pd.DataFrame(future_months, columns=['Month']) +predicted_shampoo_prices = model_shampoo.predict(future_X) +predicted_shower_gel_prices = model_shower_gel.predict(future_X) + +# 合并预测结果到DataFrame中 +predictions_df = pd.DataFrame({ + 'Date': pd.date_range(start=dates[-1] + pd.DateOffset(months=1), periods=6, freq='M'), + 'Predicted 洗发水 Price': predicted_shampoo_prices, + 'Predicted 沐浴露 Price': predicted_shower_gel_prices +}) + +# 可视化结果 +plt.figure(figsize=(12, 8)) + +# 洗发水价格 +plt.subplot(2, 1, 1) +plt.plot(prices_data.index, prices_data['洗发水'], label='Historical Price') +plt.plot(predictions_df['Date'], predictions_df['Predicted 洗发水 Price'], label='Predicted Price', linestyle='--') +plt.title('Historical and Predicted Prices for 洗发水') +plt.xlabel('Date') +plt.ylabel('Price') +plt.legend() + +# 沐浴露价格 +plt.subplot(2, 1, 2) +plt.plot(prices_data.index, prices_data['沐浴露'], label='Historical Price') +plt.plot(predictions_df['Date'], predictions_df['Predicted 沐浴露 Price'], label='Predicted Price', linestyle='--') \ No newline at end of file diff --git "a/pythonProject2/\351\242\204\346\265\213\346\227\245\345\214\226\344\273\267\346\240\274.docx" "b/pythonProject2/\351\242\204\346\265\213\346\227\245\345\214\226\344\273\267\346\240\274.docx" new file mode 100644 index 0000000000000000000000000000000000000000..1c5df2cdaaa61d88985bf648523d5cbaad69d9de Binary files /dev/null and "b/pythonProject2/\351\242\204\346\265\213\346\227\245\345\214\226\344\273\267\346\240\274.docx" differ