Ich versuche, die Entscheidungsgrenze der logistischen Regression in scikit zu plottenWie die Entscheidungsgrenze der logistischen Regression in scikit plotten lernen
features_train_df : 650 columns, 5250 rows
features_test_df : 650 columns, 1750 rows
class_train_df = 1 column (class to be predicted), 5250 rows
class_test_df = 1 column (class to be predicted), 1750 rows
Klassifikator Code lernen;
tuned_logreg = LogisticRegression(penalty = 'l2', tol = 0.0001,C = 0.1,max_iter = 100,class_weight = "balanced")
tuned_logreg.fit(x_train[sorted_important_features_list[0:650]].values, y_train['loss'].values)
y_pred_3 = tuned_logreg.predict(x_test[sorted_important_features_list[0:650]].values)
Ich bekomme die richtige Ausgabe für den Klassifizierungscode.
Got diesen Code online:
code:
X = features_train_df.values
# evenly sampled points
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 50),
np.linspace(y_min, y_max, 50))
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
#plot background colors
ax = plt.gca()
Z = tuned_logreg.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
cs = ax.contourf(xx, yy, Z, cmap='RdBu', alpha=.5)
cs2 = ax.contour(xx, yy, Z, cmap='RdBu', alpha=.5)
plt.clabel(cs2, fmt = '%2.1f', colors = 'k', fontsize=14)
# Plot the points
ax.plot(Xtrain[ytrain == 0, 0], Xtrain[ytrain == 0, 1], 'ro', label='Class 1')
ax.plot(Xtrain[ytrain == 1, 0], Xtrain[ytrain == 1, 1], 'bo', label='Class 2')
# make legend
plt.legend(loc='upper left', scatterpoints=1, numpoints=1)
Fehler:
ValueError: X has 2 features per sample; expecting 650
Bitte mir empfehlen, wo ich falsch
Wo ist der Klassifikationscode für die logistische Regression? Ich denke, das Problem liegt in der Vorhersage-Methode des Klassifikators. –
@WasiAhmad Ich habe den Klassifikator Code hinzugefügt, aber ich bekomme keinen Fehler darin. – vikky
können Sie erklären, was Sie mit dieser Anweisung tun wollen - 'ax.plot (Xtrain [ytrain == 0, 0], Xtrain [ytrain == 0, 1], 'ro', label = 'Klasse 1') '? –