Skip to content

Instantly share code, notes, and snippets.

@jsun
Created September 16, 2018 03:58
Show Gist options
  • Save jsun/a6c23779b2bd4b4954d31f0f3382d794 to your computer and use it in GitHub Desktop.
Save jsun/a6c23779b2bd4b4954d31f0f3382d794 to your computer and use it in GitHub Desktop.
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
cancer = datasets.load_breast_cancer()
x = cancer.data
y = cancer.target
print(x.shape)
## (569, 30)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
scaler = StandardScaler()
scaler.fit(x_train)
x_train_scaled = scaler.transform(x_train)
lda = LDA(n_components=2)
lda.fit(x_train_scaled, y_train)
x_train_scaled_lda = lda.transform(x_train_scaled)
clf = RandomForestClassifier(max_depth=2)
clf.fit(x_train_scaled_lda, y_train)
x_test_scaled = scaler.transform(x_test)
x_test_scaled_lda = lda.transform(x_test_scaled)
y_pred = clf.predict(x_test_scaled_lda)
confusion_matrix(y_test, y_pred)
## array([[41, 4],
## [ 1, 68]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment