ValueError: количество функций модели должно соответствовать входным. Модель n_features - 30, а входная n_features - 2
Я новичок в науке о данных и машинном обучении. Итак, я пытаюсь визуализировать выброс, используя алгоритм Isolation Forest Algorithm, на который я ссылался здесь . Я использую набор данных о мошенничестве с кредитными картами из Kaggle, X = столбец 1-30 и y = класс столбца
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.35)
# Menetapkan X dan y
columns = data1.columns.tolist()
# Filter columns
columns = [c for c in columns if c not in ["Class"]]
# Saving label Class in target
target = "Class"
# Identify state, value of X, y, X_outliers
state = np.random.RandomState(42)
X = data1[columns]
y = data1[target]
X_outliers = state.uniform(low=0, high=1, size=(X.shape[0], X.shape[1]))
Я немного изменил указанное, вот полный код
rng = np.random.RandomState(42)
clf = IsolationForest(n_estimators=100, max_samples='auto',
contamination=outlier_fraction,
random_state=state, verbose=0)
clf.fit(X_train)
y_pred_train = clf.predict(X_train)
y_pred_test = clf.predict(X_test)
y_pred_outliers = clf.predict(X_outliers)
# 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))
vrb = np.c_[xx.ravel(), yy.ravel()]
Z = clf.decision_function(vrb)
Z = Z.reshape(xx.shape)
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((-5, 5))
plt.ylim((-5, 5))
plt.legend([b1, b2, c],
["training observations",
"new regular observations", "new abnormal observations"],
loc="upper left")
plt.show()
Я думаю, это из-за y в vrb.shape (2500, 2), который отличается от X.shape (28481, 30). Но я не знаю, как сделать то же самое
Я попытался изменить (xx.shape) на X_train, X_test, но ничего не работает, я все время получаю ошибку
ValueError: Number of features of the model must match the input. Model n_features is 30 and input n_features is 2.
Это мой полный код