<jupyter_start><jupyter_text>Auto Similarity Feature analysis of automobiles.Dataset can be found at the UCI Machine Learning Repository: [Auto MPG Dataset](https://archive.ics.uci.edu/ml/datasets/auto+mpg)This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University. The dataset was used in the 1983 American Statistical Association Exposition.<jupyter_code>%matplotlib inline

import numpy as np 
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt 

# Modify the style of the plots 
sns.set_context('notebook')
sns.set_style('whitegrid')<jupyter_output><empty_output><jupyter_text>Data Loading Download the `auto.data` file from the UCI ML Repository and store it in a folder called `data` that is in the same directory as this notebook.The data loader creates a Pandas DataFrame with named columns and adds a feature for the make of the vehicle by parsing the `car name` field. It also replaces `"?"` with `np.nan` for fields with missing values.<jupyter_code>feature_names = [
    "mpg",
    "cylinders",
    "displacement",
    "horsepower",
    "weight",
    "acceleration", 
    "model year",
    "origin",
    "car name",
]

def load_data(path="data/auto.data"):
    data = pd.read_csv('data/auto.data', delimiter='\s+', header=None)
    data.columns = feature_names
    
    # Get the make of the vehicle as the first word in the name 
    data['make'] = data['car name'].apply(lambda s: s.split()[0])
    
    # Replace '?' with np.nan 
    data['horsepower'] = pd.to_numeric(data['horsepower'], errors='coerce')
    
    return data 
df = load_data() 
df.dtypes<jupyter_output><empty_output><jupyter_text>Data Exploration<jupyter_code>df['make'].unique()
_, ax = plt.subplots(figsize=(9,6))
df.plot(kind='scatter', x='weight', y='mpg', ax=ax)

ax.set_title("MPG to Weight")
ax.set_xlabel("weight (kg)")
_, ax = plt.subplots(figsize=(9,9))
sns.barplot(y='make', x='mpg', hue='model year', ax=ax, data=df[df['model year']>=80])
numeric_features = [
    "mpg",
    "displacement",
    "horsepower",
    "weight",
    "acceleration", 
]

sns.pairplot(data=df[numeric_features].dropna(), diag_kind='kde')
df[numeric_features].head()
sns.jointplot(data=df[numeric_features].dropna(), x='horsepower', y='displacement', kind='reg')
sns.boxplot(data=df)
feature_names = [
    "cylinders",
    "displacement",
    "horsepower",
    "weight",
    "acceleration", 
    "model year",
    "origin",
]

target_name = 'mpg'

X = df.dropna()[feature_names]
y = df.dropna()[target_name]
from sklearn.decomposition import PCA, TruncatedSVD 

Xt = PCA(n_components=2).fit_transform(X)
_, ax = plt.subplots(figsize=(9,6))

s  = ax.scatter(Xt[:,0], Xt[:,1], c=y, cmap='RdYlGn', label='PCA Projection')
cb = plt.colorbar(s)

ax.set_title('PCA Projection of Auto Data')
ax.set_xlabel('PC 1')
ax.set_ylabel('PC 2')
ax.set_xticklabels([])
ax.set_yticklabels([])
Xt = TruncatedSVD(n_components=2).fit_transform(X)
_, ax = plt.subplots(figsize=(9,6))

s  = ax.scatter(Xt[:,0], Xt[:,1], c=y, cmap='RdYlGn', label='SVD Projection')
cb = plt.colorbar(s)

ax.set_title('Truncated SVD Projection of Auto Data')
ax.set_xlabel('PC 1')
ax.set_ylabel('PC 2')
ax.set_xticklabels([])
ax.set_yticklabels([])
from yellowbrick.features import Manifold
from yellowbrick.features import PCADecomposition
oz = PCADecomposition(scale=True, proj_features=True)
oz.fit_transform(X, y)
oz.poof()
oz = Manifold()
oz.fit_transform(X, y)
oz.poof()
oz = Manifold(manifold='tsne')
oz.fit_transform(X, y)
oz.poof()
oz = Manifold(manifold='isomap')
oz.fit_transform(X, y)
oz.poof()
oz = Manifold(manifold='spectral')
oz.fit_transform(X, y)
oz.poof()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1505143475906876, 'fraction_numerical': 0.00676773145641581, 'mean_word_length': 5.305460750853243, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '141635', 'license': 'MIT', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1344, 'n_words': 316, 'path': 'diagnostics/auto/auto-analysis-2.ipynb', 'repo_name': 'georgetown-analytics/XBUS-506-01.Visual_Analytics', 'size': '3694'}