Skip to content

Instantly share code, notes, and snippets.

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