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
.................
所以,假设我有销售统计数据。我想找出:
Price
/ etc对Sales
的影响?请告知哪些Python库可以提供帮助?任何一个例子都会很棒!
答案 0 :(得分:2)
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)
现在,我们看到只有38个功能中的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')
从图表中我们看到该模型做得非常好:我们的模型已经捕获了“销售”的大部分变化。