0x00場(chǎng)景事件說明:
與有監(jiān)督學(xué)習(xí)相比,有監(jiān)督學(xué)習(xí)有一條最佳擬合曲線巩割,曲線的一邊被認(rèn)為是正常數(shù)據(jù)裙顽,曲線的另一邊被認(rèn)為是異常數(shù)據(jù)。
無監(jiān)督學(xué)習(xí)是個(gè)分類集合的過程宣谈,根據(jù)特征對(duì)輸入的數(shù)據(jù)進(jìn)行劃分愈犹,根據(jù)數(shù)據(jù)所屬的簇進(jìn)行預(yù)測(cè)。
實(shí)踐場(chǎng)景說明:
用戶階段性行為數(shù)據(jù)的判斷闻丑,如果不采用規(guī)則的形式漩怎,是否可用算法替代或補(bǔ)充。
考慮到使用有監(jiān)督將帶來巨大的制造樣本壓力嗦嗡,并且樣本會(huì)極端化勋锤,故考慮無監(jiān)督模型。
0x01選擇算法
參考
https://zhuanlan.zhihu.com/p/33919957
調(diào)研算法優(yōu)劣侥祭,得到幾個(gè)比較符合需求的算法叁执,但是都有不滿足的問題。
-
TSNE聚類算法:
問題1:只能對(duì)數(shù)據(jù)進(jìn)行操作矮冬,沒辦法生成模型再為其他數(shù)據(jù)所用谈宛。 放棄。
-
k-means算法
問題1:對(duì)于孤立點(diǎn)是敏感的胎署,但是需要確認(rèn)k值進(jìn)行聚類吆录。同一時(shí)間段內(nèi)我們無法提前感知需要設(shè)置的k值量。 放棄琼牧。
-
孤立森林算法(傾向):
問題1.孤立森林樣本多特征無法降維放進(jìn)二維圖表展示 問題2.如果異常請(qǐng)求過多恢筝,容易將異常請(qǐng)求決策為為正常結(jié)果
由于比較感興趣孤立森林的決策樹哀卫,在此對(duì)比官網(wǎng)示例了解其運(yùn)行機(jī)制:
0x01示例demo
官方鏈接這里這里~
http://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html
IsolationForest demo:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import IsolationForest
rng = np.random.RandomState(100) #種子數(shù)量
# Generate train data
X = 0.3 * rng.randn(100, 2)
#創(chuàng)建100*2的訓(xùn)練樣本數(shù)組,
X_train = np.r_[X + 2, X - 2]
#將樣本中的所有數(shù)據(jù)分別+2 滋恬,-2聊训,np.r_()并將這兩個(gè)結(jié)果合并到一維數(shù)組。
#關(guān)于np.r_()與np.c_()可見特別說明2
# Generate some regular novel observations
X = 0.3 * rng.randn(20, 2)
X_test = np.r_[X + 2, X - 2]
# Generate some abnormal novel observations
X_outliers = rng.uniform(low=-5, high=5, size=(20, 2))
#生成最大為4恢氯,最小-4的20*2的數(shù)組带斑。
# fit the model
clf = IsolationForest(behaviour='new', max_samples=100,
random_state=rng, contamination='auto')
#創(chuàng)建對(duì)象并對(duì)模型做配置,最大樣本數(shù)量100勋拟。
clf.fit(X_train)
#用訓(xùn)練數(shù)據(jù)擬合模型
y_pred_train = clf.predict(X_train)
#用擬合好的模型去分類訓(xùn)練數(shù)據(jù)
y_pred_test = clf.predict(X_test)
#用擬合好的模型去分類測(cè)試數(shù)據(jù) ,該數(shù)據(jù)樣本與訓(xùn)練數(shù)據(jù)差不多相同勋磕。
y_pred_outliers = clf.predict(X_outliers)
#用擬合好的模型去分類測(cè)試數(shù)據(jù),該數(shù)據(jù)樣本與訓(xùn)練數(shù)據(jù)有些偏差。取的是-5 到5 范圍內(nèi)敢靡。
for i,tree in enumerate(y_pred_test):
print(X_test[i],tree)
#tree為-1為異常挂滓,1為正常。特別說明1中詳述啸胧。
###后面都是圖片展示的功能了赶站,如果無需可視化,可直接注銷掉后面所有代碼纺念。
# plot the line, the samples, and the nearest vectors to the plane
xx, yy = np.meshgrid(np.linspace(-10, 10, 50), np.linspace(-10, 10, 50))
#生成-5到5之間贝椿,包含50個(gè)數(shù)字的等差數(shù)列。如np.linspace(0,100,5) 則生成[0 25 50 75 100]
#meshgrid生成網(wǎng)格函數(shù)陷谱,xx,yy說明為二維網(wǎng)格函數(shù)
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
#.ravel()為降一維-行序優(yōu)先
# np.c_矩陣相加
#decision_function為樣本距離超平面的距離烙博。
Z = Z.reshape(xx.shape)
#reshapce()將某個(gè)數(shù)組維度調(diào)整為和另一數(shù)組維度相同
plt.title("IsolationForest")
plt.contourf(xx, yy,Z, cmap=plt.cm.Blues_r)
b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c='white',
s=20, edgecolor='k')
b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c='green',
s=20, edgecolor='k')
c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='red',
s=20, edgecolor='k')
plt.axis('tight')
plt.xlim((-10, 10))
plt.ylim((-10, 10))
plt.legend([b1, b2, c],
["training observations",
"new regular observations", "new abnormal observations"],
loc="upper left")
plt.show()
特別說明1:
for i,tree in enumerate(y_pred_test): print(X_test[i],tree)
此段代碼可直接對(duì)鼓勵(lì)森林作出結(jié)果的展示。
若此時(shí)選擇展示出y_pred_test的結(jié)果烟逊,如下渣窜。
可見根據(jù)訓(xùn)練X_train的數(shù)據(jù)來說,與他基本相同的數(shù)據(jù)集僅有一小部分為異常數(shù)據(jù)宪躯。[2.18132706 1.72789087] 1 [2.17760698 1.86888068] 1 [2.03053273 2.39250405] -1 [2.41628054 1.47938266] -1 [2.08651323 1.89827437] 1 [2.15821074 1.88770913] 1 [2.14312769 1.5710712 ] 1 [2.12231196 1.75151588] 1 [2.12703718 1.95214479] 1 [2.37232463 2.49906089] -1 [1.80141869 3.03956814] -1 [2.11743159 2.09817361] 1 [1.90659588 1.57260332] 1 [1.81253834 1.96700138] 1 [2.28588783 1.70499121] 1 [1.67163284 1.92759538] 1 [1.82146585 1.70515643] 1 [2.02646898 1.81917219] 1 [2.33766664 2.27811599] -1 [1.3192529 1.89554881] -1 [-1.81867294 -2.27210913] 1 [-1.82239302 -2.13111932] 1 [-1.96946727 -1.60749595] -1 [-1.58371946 -2.52061734] -1 [-1.91348677 -2.10172563] 1 [-1.84178926 -2.11229087] 1 [-1.85687231 -2.4289288 ] 1 [-1.87768804 -2.24848412] 1 [-1.87296282 -2.04785521] 1 [-1.62767537 -1.50093911] -1 [-2.19858131 -0.96043186] -1 [-1.88256841 -1.90182639] 1 [-2.09340412 -2.42739668] 1 [-2.18746166 -2.03299862] 1 [-1.71411217 -2.29500879] 1 [-2.32836716 -2.07240462] 1 [-2.17853415 -2.29484357] 1 [-1.97353102 -2.18082781] 1 [-1.66233336 -1.72188401] 1 [-2.6807471 -2.10445119] -1
可視化如下:
(白色數(shù)據(jù)為訓(xùn)練數(shù)據(jù)集乔宿,綠色為測(cè)試數(shù)據(jù)集)for i,tree in enumerate(y_pred_outliers): print(X_outliers[i],tree)
若這里采用差異較大的X_outliers作為數(shù)據(jù)集,結(jié)果如下访雪∮璨可見除了一個(gè)正常的點(diǎn),其他均為異常點(diǎn)冬阳。
[ 3.39970099 -0.45836448] -1 [-1.78634161 -4.07280133] -1 [-4.39562068 -4.09048833] -1 [1.82706456 1.80735767] 1 (唯一正常數(shù)據(jù)) [-2.56825834 1.40461442] -1 [-4.30860817 3.72919962] -1 [-3.9039305 -3.30944235] -1 [-0.32622009 2.75949219] -1 [ 3.54444516 -2.89613555] -1 [-4.23358131 2.88914797] -1 [0.47500001 2.86254863] -1 [ 4.20047043 -0.19027234] -1 [-0.4044633 0.98979155] -1 [0.99318781 0.04373451] -1 [-1.9312147 0.4135298] -1 [4.24926943 4.70550802] -1 [-1.0420539 2.98745273] -1 [ 1.35088148 -2.70030835] -1 [-4.48792907 -4.71536194] -1 [-3.77152248 -2.79787482] -1
可視化如下:
(白色數(shù)據(jù)為訓(xùn)練數(shù)據(jù)集蛤虐,綠色為差異小的測(cè)試數(shù)據(jù)集,紅色為差異較大的測(cè)試數(shù)據(jù)集)
特別說明2:
np.r_和np.c_的區(qū)別:
>>> import numpy as np
>>> a=np.array([1,2,3])
>>> b=np.array([4,5,6])
>>> print(np.r_[a,b])
[1 2 3 4 5 6]
>>> print(np.c_[a,b])
[[1 4]
[2 5]
[3 6]]
>>> c=np.array([a,b])
>>> c
array([[1, 2, 3],
[4, 5, 6]])
>>> c=np.c_[a,b]
>>> c
array([[1, 4],
[2, 5],
[3, 6]])
>>> print(np.c_[b,c])
[[4 1 4]
[5 2 5]
[6 3 6]]
0x02孤立森林的決策方法調(diào)整
由于這種方法存在兩種前面提過的問題肝陪,考慮能否將此算法決策部分做調(diào)整驳庭。適應(yīng)場(chǎng)景。
待更。
0x03數(shù)據(jù)集準(zhǔn)備
由flink計(jì)算每個(gè)時(shí)間段內(nèi)關(guān)注點(diǎn)的值饲常,傳入算法中得出正負(fù)結(jié)果集蹲堂。
待更。