Skip to content

Instantly share code, notes, and snippets.

Created Nov 23, 2020
What would you like to do?
3 ways to do dimensional reduction techniques in Scikit-learn
from sklearn.preprocessing import StandardScaler
from sklearn.manifold import MDS
# each feature should be centered (zero mean) and with unit variance
X_normalized = StandardScaler().fit(X).transform(X)
mds = MDS(n_components = 2)
X_mds = mds.fit_transform(X_normalized)
print(X.shape, X_mds.shape)
(569, 30) (569, 2)
X_train_mds, X_test_mds, y_train, y_test = train_test_split(X_mds, y, random_state=0)
clf_mds = LogisticRegression(random_state=0), y_train)
print('%s: %.3f' % ('Logreg Train Accuracy', accuracy_score(y_train, clf_mds.predict(X_train_mds))))
print('%s: %.3f' % ('Logreg Test Accuracy', accuracy_score(y_test, clf_mds.predict(X_test_mds))))
print('%s: %.3f' % ('Logreg Train F1 Score', f1_score(y_train, clf_mds.predict(X_train_mds))))
print('%s: %.3f' % ('Logreg Test F1 Score', f1_score(y_test, clf_mds.predict(X_test_mds))))
print(classification_report(y_test, clf_mds.predict(X_test_mds)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment