Created
June 28, 2020 17:28
-
-
Save gmyrianthous/59ce220c2785225d2efd4dd8cc57730f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.datasets import load_wine | |
from sklearn.model_selection import train_test_split | |
from sklearn.decomposition import PCA | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.naive_bayes import GaussianNB | |
from sklearn.metrics import accuracy_score | |
import matplotlib.pyplot as plt | |
features, target = load_wine(return_X_y=True) | |
X_train, X_test, y_train, y_test = train_test_split( | |
features, target, test_size=0.3, random_state=42 | |
) | |
# Scale the data | |
scaler = StandardScaler() | |
X_train_scaled = scaler.fit_transform(X_train) | |
X_test_scaled = scaler.transform(X_test) | |
# Apply dimensionality reduction | |
pca = PCA(n_components=2) | |
X_train_dim_red = pca.fit_transform(X_train_scaled) | |
X_test_dim_red = pca.transform(X_test_scaled) | |
# Visualise results | |
fig, ax = plt.subplots(figsize=(10, 7)) | |
for label, color in zip(set(y_train), ('orange', 'blue', 'brown')): | |
ax.scatter( | |
X_train_dim_red[y_train == label, 0], | |
X_train_dim_red[y_train == label, 1], | |
color=color, label=f'Class {label}' | |
) | |
ax.set_title('Dataset after Principal Component Analysis ') | |
ax.set_xlabel('PC1') | |
ax.set_ylabel('PC2') | |
ax.legend(loc='upper right') | |
# Train and evaluate a model | |
model = GaussianNB() | |
model.fit(X_train_dim_red, y_train) | |
predictions = model.predict(X_test_dim_red) | |
print(f'Model Accuracy: {accuracy_score(y_test, predictions):.2f}') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment