我正在编写一个脚本,该脚本将预测给定未来日期服务器上已用磁盘空间的百分比。使用百分比从这个命令中获取 1 次/天,如下所示:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 30G 24G 4.4G 85% /
并与日期一起记录。该脚本是在 Python 中编写的,当我使用 LinearRegression 作为我的分类器时,它的缺点是我的得分非常低。代码如下:
import pandas as pd
import numpy as np
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
# list of tuples whose format is (day_of_month, percent_used)
results = [(1, 83), (2, 87), (3, 87), (4, 87), (5, 89), (6, 88), (7, 83), (8, 75), (9, 73), (10, 73), (11, 74), (12, 77), (13, 77), (14, 79), (15, 79), (16, 79), (17, 79), (18, 79), (19, 80), (21, 80), (22, 81), (23, 84), (24, 85), (25, 85), (26, 85), (27, 85), (28, 85)]
labels = ['day', 'pct_used']
df = pd.DataFrame.from_records(results, columns=labels)
# convert list of days into a numpy array and reshape
X = np.array(df['day']).reshape(-1,1)
y = np.array(df['pct_used'])
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.4)
clf = LinearRegression(n_jobs=-1)
clf.fit(X_train, y_train)
print clf.score(X_test, y_test)
# predict day 30's pct_used value
print clf.predict(np.array([30]).reshape(-1,1))
它输出:
-0.19521578836110454
[81.22057369]
其中clf.score
为负各一次。我希望它是正数,并且至少为 0.95 或更高,这样我才能对预测充满信心。我不确定我是否使用了错误的分类器、需要更多数据、需要更多功能,还是在代码中做其他错误。
我发现一些有趣的事情是,如果我将结果的初始列表更改为线性增加的 pct_used,例如:
results = [(1, 73), (2, 73), (3, 74), (4, 75), (5, 76), (6, 77), (7, 78), (8, 78), (9, 80), (10, 80), (11, 81), (12, 82), (13, 83), (14, 84), (15, 85), (16, 85), (17, 85), (18, 86), (19, 86), (21, 87), (22, 88), (23, 89), (24, 89), (25, 90), (26, 91), (27, 91), (28, 92)]
然后这个输出的分数飙升:
0.9852576797564747
[94.37028796]
所以这让我认为LinearRegression
只要 Y 轴数据大部分是线性的,它就可以很好地作为分类器。当然,在现实世界中的磁盘空间会像在我的原始数据集中一样波动,所以这就是为什么我想也许我应该使用不同的分类器,但我尝试过sklearn.svm.SVR()
,结果也很差。
我想逻辑回归方法可以代替线性回归,在接下来的几天里它的使用率可能超过 90%,或者没有。或者我简要地阅读了“时间序列预测”,但我不确定这是否符合标准(我是机器学习的新手......)。我很灵活,只是真的质疑我的设置有什么问题,以及我是否需要完全采用新方法。
感谢您提供任何建议和特定编辑以改进代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句