<jupyter_start><jupyter_text>Feature engineering<jupyter_code>car_names = df['car name']
df.drop(['model year', 'car name'], inplace=True, axis=1)
df.head()
for col in df.columns:
    sns.scatterplot(x=col,y='mpg', data=df)
    plt.show()
df.columns
df['power_weight'] = df['horsepower'] / df['weight']
df.drop(['horsepower','weight'], axis=1, inplace=True)
df.columns
df['displacement_per_cylinder'] = df['displacement'] / df['cylinders']
df.drop(['displacement','cylinders'], axis=1, inplace=True)
for col in df.columns:
    sns.scatterplot(x=col,y='mpg', data=df, hue='origin')
    plt.show()
df.columns
true_labels = df['origin']
df.drop(['origin','mpg'], inplace=True, axis=1)
df.head()<jupyter_output><empty_output><jupyter_text>Clustering<jupyter_code>cluster_list = [2,3,4,5]
inertia_list = list()
for cluster in cluster_list:
    kmeans = KMeans(n_clusters=cluster)
    kmeans.fit(df.values)
    inertia_list.append(kmeans.inertia_)
sns.lineplot(x=cluster_list, y=inertia_list)
plt.show()
k3 = KMeans(n_clusters=3)
k3.fit(df.values)
prediction = k3.predict(df.values)
df.shape
k3.cluster_centers_.shape
k3.cluster_centers_
np.unique(prediction)
df['prediction'] = prediction
df['true_classes'] = true_labels
df['car_names'] = car_names
df.head()
origin1 = df[df['true_classes'] ==1]
origin2 = df[df['true_classes'] ==2]
origin3 = df[df['true_classes'] ==3]
origin1.describe()
origin1
origin1['prediction'].mode()
origin2
origin2['prediction'].mode()
origin3
origin3['prediction'].mode()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18032786885245902, 'fraction_numerical': 0.017704918032786884, 'mean_word_length': 7.72, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '606421', 'license': 'MIT', 'n_tokens_mistral': 608, 'n_tokens_neox': 596, 'n_words': 93, 'path': 'Clustering_auto_mpg.ipynb', 'repo_name': 'thefullstackninja/practical_data_science_projects', 'size': '1525'}