С трудом разбираясь в разделах Z = clf.decision_function(np.c_[xx.ravel(), and yy.ravel()])
и Z.reshape(xx.shape)
в приведенном здесь примере: https://scikit -learn.org / stable / auto_examples / ensemble / plot_isolation_forest.html
Мне нужно создать визуал для моего леса изоляции, чтобы я мог улучшить его объяснение для бизнес-пользователей. Мне действительно понравился пример, включенный в пример scikit-learn для реализации IsolationForest, но я не понимаю, как это было получено. Мне представляется следующая ошибка: ValueError: Number of features of the model must match the input. Model n_features is 7 and input n_features is 2
.
Я попытался упростить то, что я желаю построить.
train = pd.read_sql_query(
"SELECT state, \
loan_amount, \
interest_rate, \
rehab_budget, \
loan_term_in_months_effective, \
loan_term_in_months_stated, \
CASE WHEN loan_type = 'Bridge' THEN 1 ELSE 0 END AS 'bridge_flag', \
CASE WHEN loan_type = 'Renovation' THEN 1 ELSE 0 END AS 'renovation_flag' \
FROM [DataMart].[VisionStream].[kpi_MostRecent] \
WHERE gs_eligibility_status = 'Eligible' \
AND pipeline_status <> 'Cancelled' \
AND actual_funding_date >= '2019-01-01' \
AND actual_funding_date <= '2019-06-30'",
conn)
df_dummies = pd.get_dummies(train['state'], prefix = 'state')
train.drop(['state'], axis = 1, inplace = True)
#######################
rng = np.random.RandomState(42)
# fit the model
clf = IsolationForest(max_samples=100, random_state=rng)
clf.fit(train)
y_pred_train = clf.predict(train)
# plot the line, the samples, and the nearest vectors to the plane
xx, yy = np.meshgrid(np.linspace(-5, 5, 50), np.linspace(-5, 5, 50))
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.title("IsolationForest")
plt.contourf(xx, yy, Z, cmap=plt.cm.Blues_r)
b1 = plt.scatter(train[:, 0], train[:, 1], c='white',
s=20, edgecolor='k')
plt.axis('tight')
plt.xlim((-5, 5))
plt.ylim((-5, 5))
plt.legend([b1, b2],
["training observations"],
loc="upper left")
plt.show()
Я ожидаю, что смогу подготовить сюжет такого рода в примере документации scikit-learn длявнедрение изолированного леса.