import numpy as np from sklearn.ensemble import IsolationForest import matplotlib.pyplot as plt
np.random.seed(29) x = np.random.normal(size=[100,2]) x = 0.05*x x1 = x+2 x2 = x-2 训练数据 x_train = np.vstack([x1,x2]) 异常数据 x_error = np.random.uniform(-3.5,3.5,size=[20,2]) 测试数据 x_test = 0.05 *np.random.normal(size=[20,2]) x_test = np.vstack([x_test+2,x_test-2])
istf = IsolationForest(n_estimators=100, random_state=28, contamination=0.05) istf.fit(x_train)
x_train_pr=istf.predict(x_train) x_error_pr=istf.predict(x_error) x_test_pr=istf.predict(x_test) print(“正常数据的预测值:{}”.format(x_train_pr)) print(“测试正常数据的预测值:{}”.format(x_test_pr)) print(“测试异常数据的预测值:{}”.format(x_error_pr))
train_decision = istf.decision_function(x_train) test_decision = istf.decision_function(x_test) outliers_decision = istf.decision_function(x_error) print(“正常数据的预测值:\n{}”.format(train_decision)) print(“测试正常数据的预测值:\n{}”.format(test_decision)) print(“测试异常数据的预测值:\n{}”.format(outliers_decision))
estimators_ = istf.estimators_ print(estimators_) estimators_features_ = istf.estimators_features_ print(estimators_features_)
t1 = np.linspace(-4, 4, 50) t2 = np.linspace(-4, 4, 50) x1, x2 = np.meshgrid(t1, t2) # 产生网格点的坐标 x_show = np.dstack((x1.flat, x2.flat))[0] z = istf.decision_function(x_show) z = z.reshape(x1.shape) plt.contourf(x1, x2, z, cmap=plt.cm.Blues_r) plt.plot(x_train[:, 0], x_train[:, 1], ‘ro’) plt.plot(x_test[:, 0], x_test[:, 1], ‘go’) plt.plot(x_error[:, 0], x_error[:, 1], ‘bo’) plt.xlim((-4, 4)) plt.ylim((-4, 4)) plt.show()