added some examples
This commit is contained in:
@@ -948,7 +948,45 @@ pca.components_.T[:, 0].
|
||||
Another very useful piece of information is the explained variance ratio of each principal component,
|
||||
available via the $explained\_variance\_ratio$ variable. It indicates the proportion of the dataset’s
|
||||
variance that lies along the axis of each principal component.
|
||||
More material to come here.
|
||||
|
||||
!split
|
||||
===== Back to the Cancer Data =====
|
||||
We can now repeat the above but applied to real data, in this case our breat cancer data.
|
||||
!bc pycod
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
print("Test set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
|
||||
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
|
||||
#thereafter we do a PCA with Scikit-learn
|
||||
from sklearn.decomposition import PCA
|
||||
pca = PCA(n_components = 2)
|
||||
X2D_train = pca.fit_transform(X_train_scaled)
|
||||
X2D_test = pca.fit_transform(X_test_scaled)
|
||||
|
||||
logreg.fit(X2D_train,y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X2D_test,y_test)))
|
||||
!ec
|
||||
|
||||
|
||||
!split
|
||||
===== More on the PCA =====
|
||||
|
||||
@@ -14,7 +14,6 @@ correlation_matrix = cancerpd.corr().round(1)
|
||||
# use the heatmap function from seaborn to plot the correlation matrix
|
||||
# annot = True to print the values inside the square
|
||||
sns.heatmap(data=correlation_matrix, annot=True)
|
||||
plt.show()
|
||||
EigValues, EigVectors = np.linalg.eig(correlation_matrix)
|
||||
print(EigValues)
|
||||
|
||||
@@ -35,6 +34,11 @@ X_test_scaled = scaler.transform(X_test)
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
|
||||
#thereafter we do a PCA with Scikit-learn
|
||||
from sklearn.decomposition import PCA
|
||||
pca = PCA(n_components = 2)
|
||||
X2D_train = pca.fit_transform(X_train_scaled)
|
||||
X2D_test = pca.fit_transform(X_test_scaled)
|
||||
|
||||
|
||||
|
||||
logreg.fit(X2D_train,y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X2D_test,y_test)))
|
||||
|
||||
Reference in New Issue
Block a user