Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- from sklearn.preprocessing import StandardScaler
- from sklearn.decomposition import PCA
- from sklearn.model_selection import train_test_split
- from sklearn.linear_model import LogisticRegression
- from sklearn.metrics import confusion_matrix
- import matplotlib.pyplot as plt
- import seaborn as sns
- ---------------------------------------------------------------------------
- data = {
- 'Height': [170, 165, 180, 175, 160, 172, 168, 177, 162, 158],
- 'Weight': [65, 59, 75, 68, 55, 70, 62, 74, 58, 54],
- 'Age': [30, 25, 35, 28, 22, 32, 27, 33, 24, 21],
- 'Gender': [1, 0, 1, 1, 0, 1, 0, 1, 0, 0],
- }
- df = pd.DataFrame(data)
- print(df)
- ---------------------------------------------------------------------------
- X = df.drop('Gender', axis=1)
- y = df['Gender']
- scaler = StandardScaler()
- X_scaled = scaler.fit_transform(df)
- ---------------------------------------------------------------------------
- pca = PCA(n_components=2)
- X_pca = pca.fit_transform(X_scaled)
- X_train, X_test, y_train, y_test = train_test_split(X_pca, y, test_size=0.3, random_state=42)
- model = LogisticRegression()
- model.fit(X_train, y_train)
- y_pred = model.predict(X_test)
- ---------------------------------------------------------------------------
- cm = confusion_matrix(y_test, y_pred)
- plt.figure(figsize=(5,4))
- sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Female', 'Male'], yticklabels=['Female', 'Male'])
- plt.xlabel('Predicted Label')
- plt.ylabel('True Label')
- plt.title('Confusion Matrix')
- plt.show()
- ---------------------------------------------------------------------------
- y_numeric = pd.factorize(y)[0]
- plt.figure(figsize=(12, 5))
- plt.subplot(1, 2, 1)
- plt.scatter(X_scaled[:, 0], X_scaled[:, 1], c=y_numeric, cmap='coolwarm', edgecolor='k', s=80)
- plt.xlabel('Original Feature 1')
- plt.ylabel('Original Feature 2')
- plt.title('Before PCA: Using First 2 Standardized Features')
- plt.colorbar(label='Target classes')
- plt.subplot(1, 2, 2)
- plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y_numeric, cmap='coolwarm', edgecolor='k', s=80)
- plt.xlabel('Principal Component 1')
- plt.ylabel('Principal Component 2')
- plt.title('After PCA: Projected onto 2 Principal Components')
- plt.colorbar(label='Target classes')
- plt.tight_layout()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment