Monday-Saturday 11AM To 6PM
+91 9818193410, +91149043166

Mva Script [new] ⟶ < TRENDING >

return pca_scores if name == " main ": # Simulate data np.random.seed(42) X = np.random.randn(100, 10) y = np.random.choice([0,1], size=100)

# mva_script.py import pandas as pd import numpy as np from sklearn.impute import SimpleImputer from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA from sklearn.cluster import KMeans import matplotlib.pyplot as plt import seaborn as sns def run_mva(data, labels=None, variance_threshold=0.8): """ Complete MVA pipeline. Parameters: data : pd.DataFrame or np.array labels : array-like, optional (for LDA) variance_threshold : float, cumulative variance for PCA """ # Step 1: Impute missing values imputer = SimpleImputer(strategy='median') data_imp = imputer.fit_transform(data)

# Step 3: PCA pca = PCA(n_components=min(data_scaled.shape[1], 10)) pca_scores = pca.fit_transform(data_scaled) cum_var = np.cumsum(pca.explained_variance_ratio_) n_comp = np.argmax(cum_var >= variance_threshold) + 1 print(f"Optimal PCA components: {n_comp} (explained {cum_var[n_comp-1]:.2%})") mva script

# Step 4: Plot scree plt.figure(figsize=(8,4)) plt.bar(range(1, len(pca.explained_variance_ratio_)+1), pca.explained_variance_ratio_) plt.step(range(1, len(cum_var)+1), cum_var, where='mid', color='red') plt.title('Scree Plot with Cumulative Variance') plt.xlabel('Principal Component') plt.ylabel('Variance Ratio') plt.savefig('scree_plot.png')

# Step 2: Scale features scaler = StandardScaler() data_scaled = scaler.fit_transform(data_imp) return pca_scores if name == " main ": # Simulate data np

scores, clusters = run_mva(X, labels=y) We tested the script on a synthetic 100×10 dataset. The PCA scree plot (Fig. 1) showed that 3 components capture 82% of the variance. The LDA projection (Fig. 2) separated the two synthetic classes almost perfectly due to the constructed differences in means. Clustering on unlabeled data suggested an optimal k of 3.

# Step 6: Unsupervised clustering (if no labels) if labels is None: # Elbow method inertias = [] K_range = range(2, min(10, data_scaled.shape[0])) for k in K_range: km = KMeans(n_clusters=k, random_state=42) km.fit(data_scaled) inertias.append(km.inertia_) plt.figure() plt.plot(K_range, inertias, 'bo-') plt.xlabel('k') plt.ylabel('Inertia') plt.title('Elbow for k-means') plt.savefig('elbow.png') best_k = K_range[np.argmin(np.diff(inertias))] # simple heuristic km_final = KMeans(n_clusters=best_k, random_state=42) clusters = km_final.fit_predict(data_scaled) print(f"Optimal clusters: {best_k}") return pca_scores, clusters 1) showed that 3 components capture 82% of the variance

# Step 5: LDA (if labels exist) if labels is not None: lda = LDA(n_components=min(2, len(np.unique(labels))-1)) lda_scores = lda.fit_transform(data_scaled, labels) print("LDA applied. Reduced shape:", lda_scores.shape) # LDA scatter plot plt.figure() for lab in np.unique(labels): subset = lda_scores[labels == lab] plt.scatter(subset[:,0], subset[:,1], label=f'Class {lab}') plt.legend() plt.title('LDA Projection') plt.savefig('lda_plot.png')