0% found this document useful (0 votes)
2 views2 pages

Mll

The document outlines a Python script that performs data analysis and visualization using libraries like pandas, matplotlib, and sklearn. It includes tasks such as reading datasets, preprocessing data, applying polynomial regression and linear regression, and implementing K-Means clustering. The script also generates confusion matrices and classification reports to evaluate model performance.

Uploaded by

sahanasanu9543
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Mll

The document outlines a Python script that performs data analysis and visualization using libraries like pandas, matplotlib, and sklearn. It includes tasks such as reading datasets, preprocessing data, applying polynomial regression and linear regression, and implementing K-Means clustering. The script also generates confusion matrices and classification reports to evaluate model performance.

Uploaded by

sahanasanu9543
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import pandas as pd ;import matplotlib.

pyplot as plt ;from


sklearn.model_selection import train_test_split; from
sklearn.preprocessing import PolynomialFeatures; import matplotlib.pyplot as plt ;from sklearn.datasets
from sklearn.linear_model import LinearRegression; mpg = import fetch_olivetti_faces ;from sklearn.model_selection
pd.read_csv(r"C:\Users\Downloads\auto-mpg.csv"); import train_test_split, cross_val_score ;
mpg['horsepower'] = pd.to_numeric(mpg['horsepower'], from sklearn.naive_bayes import GaussianNB ;from
errors='coerce'); sklearn.metrics import accuracy_score,
mpg.dropna(subset=['horsepower','mpg'], inplace=True); Xtr_m, classification_report, confusion_matrix ;X, y =
Xte_m, ytr_m, yte_m = train_test_split(mpg[['horsepower']], fetch_olivetti_faces(shuffle=True, random_state=42,
mpg['mpg'], test_size=0.2, random_state=42) return_X_y=True) ; Xtr, Xte, ytr, yte = train_test_split(X, y,
poly = PolynomialFeatures(2); Xtr_poly, Xte_poly = test_size=0.3, random_state=42) ;m = GaussianNB().fit(Xtr,
poly.fit_transform(Xtr_m), poly.transform(Xte_m) ypred_mpg = ytr); pred = m.predict(Xte);
LinearRegression().fit(Xtr_poly, ytr_m).predict(Xte_poly) ; print(f"Acc: {accuracy_score(yte,
housing = pred)*100:.2f}%");print("\nReport:\n",
pd.read_csv(r"C:\Users\Downloads\BostonHousing.csv"); Xtr_h, classification_report(yte, pred,
Xte_h, ytr_h, yte_h = train_test_split(housing[['rm']], zero_division=1));print("Confusion:\n",
housing['medv'], test_size=0.2, random_state=42) confusion_matrix(yte, pred));
ypred_h = LinearRegression().fit(Xtr_h, ytr_h).predict(Xte_h); print(f"\nCV Acc: {cross_val_score(m, X, y,
fig, axs = plt.subplots(1, 2, figsize=(12,5)); axs[0].scatter(Xte_m, cv=5).mean()*100:.2f}%");fig, axs = plt.subplots(3, 5,
yte_m, alpha=0.5, label='Actual'); axs[0].scatter(Xte_m, figsize=(12, 8)) ;for ax, img, t, p in zip(axs.ravel(), Xte, yte,
ypred_mpg, c='r', alpha=0.5, label='Predicted') ; pred): ;
axs[0].set(title='Auto MPG: Polynomial Regression', ax.imshow(img.reshape(64, 64), cmap='gray') ;
xlabel='Horsepower', ylabel='MPG'); axs[0].legend(); ax.set_title(f"Test:{t} Pred:{p}", fontsize=9) ; ax.axis('off');
axs[1].scatter(Xte_h, yte_h, alpha=0.5, label='Actual'); plt.tight_layout(); plt.show();
axs[1].plot(Xte_h, ypred_h, c='r', label='Linear Fit');
axs[1].set(title='Boston Housing: Linear Regression', xlabel='Avg
Rooms (rm)', ylabel='Median Value (medv)'); axs[1].legend() ;
plt.tight_layout(); plt.show()
import pandas as pd; import matplotlib.pyplot as plt; import
seaborn as sns;from sklearn.datasets import
import numpy as np, matplotlib.pyplot as pltfrom load_breast_cancer; from sklearn.cluster import KMeans;
sklearn.neighbors import from sklearn.preprocessing import StandardScaler; from
KNeighborsClassifiernp.random.seed(42)X = sklearn.decomposition import PCA; from sklearn.metrics
np.random.rand(100,1)y = (X[:50,0]>0.5).astype(int)ks = import confusion_matrix, classification_report;
[1,2,3,4,5,20,30]plt.figure(figsize=(15,10))for i,k in X, y = load_breast_cancer(return_X_y=True); X_pca =
enumerate(ks,1): p = PCA(2).fit_transform(StandardScaler().fit_transform(X));clus
KNeighborsClassifier(k).fit(X[:50],y).predict(X[50:]) ters = KMeans(2, random_state=42).fit_predict(X_pca) ;
print(f'k={k}:', p) plt.subplot(3,3,i) plt.scatter(X[:50], print("Confusion Matrix:\n", confusion_matrix(y, clusters)) ;
np.zeros(50), c=y, cmap='coolwarm', edgecolors='k') print("\nClassification Report:\n", classification_report(y,
plt.scatter(X[50:], np.ones(50)*0.1, c=p, clusters)) ; df = pd.DataFrame(X_pca, columns=['PC1',
cmap='coolwarm', marker='x') plt.title(k); plt.yticks([]); 'PC2']);df['Cluster'], df['Label'] = clusters, y ; def plot(title,
plt.xlabel('x'); plt.grid() plt.tight_layout(); plt.show() hue, palette, centroids=False): ; plt.figure(figsize=(7,5));
sns.scatterplot(data=df, x='PC1', y='PC2', hue=hue,
palette=palette, s=80, edgecolor='k', alpha=0.7); if
centroids:;
plt.scatter(*KMeans(2).fit(X_pca).cluster_centers_.T,
c='red', s=200, marker='X', label='Centroids'); plt.title(title);
plt.legend(); plt.tight_layout(); plt.show(); plot("K-Means
Clustering", 'Cluster', 'Set1'); plot("True Labels", 'Label',
'coolwarm');
plot("K-Means with Centroids", 'Cluster', 'Set1',
centroids=True);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy