【问题标题】:Using Python to find correlation pairs [closed]使用 Python 查找相关对 [关闭]
【发布时间】:2015-09-03 01:02:07
【问题描述】:
    NAME    PRICE   SALES   VIEWS   AVG_RATING  VOTES   COMMENTS    
Module 1    $12.00     69   12048           5      3          26    
Module 2    $24.99     12   52858           5      1          14    
Module 3    $10.00      1   1381           -1      0           0    
Module 4    $22.99     46   57841           5      8          24    
.................

所以,假设我有销售统计数据。我想知道:

  1. Price/etc 对Sales有何影响?
  2. 检测哪些功能影响最大?
  3. 应该优化哪个价格以实现最大销量?

请告知哪些 Python 库可以提供帮助?任何例子在这里都会很棒!

【问题讨论】:

  • 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。

标签: python pandas machine-learning data-mining


【解决方案1】:

python 机器学习库 scikit-learn 最适合您的情况。有一个名为 feature_selection 的子模块完全符合您的需求。这是一个例子。

from sklearn.datasets import make_regression

# simulate a dataset with 500 factors, but only 5 out of them are truely 
# informative factors, all the rest 495 are noises. assume y is your response
# variable 'Sales', and X are your possible factors
X, y = make_regression(n_samples=1000, n_features=500, n_informative=5, noise=5)

X.shape
Out[273]: (1000, 500)
y.shape
Out[274]: (1000,)

from sklearn.feature_selection import f_regression
# regressing Sales on each of factor individually, get p-values
_, p_values = f_regression(X, y)
# select significant factors p < 0.05
mask = p_values < 0.05
X_informative = X[:, mask]

X_informative.shape
Out[286]: (1000, 38)

现在,我们看到 500 个特征中只有 38 个被选中。

为了进一步构建预测模型,我们可以考虑流行的 GradientBoostRegression。

from sklearn.ensemble import GradientBoostingRegressor

gbr = GradientBoostingRegressor(n_estimators=100)
# fit our model
gbr.fit(X_informative, y)
# generate predictions
gbr_preds = gbr.predict(X_informative)

# calculate erros and plot it
gbr_error = y - gbr_preds

fig, ax = plt.subplots()
ax.hist(y, label='y', alpha=0.5)
ax.hist(gbr_error, label='errors in predictions', alpha=0.4)
ax.legend(loc='best')

从图表中,我们看到模型做得很好:我们的模型已经捕捉到了“销售”的大部分变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 2013-05-08
    • 2013-05-17
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 2020-12-26
    • 2019-03-16
    相关资源
    最近更新 更多