text_plain_output_1.png
from plotly.subplots import make_subplots

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

from sklearn.neighbors import KNeighborsClassifier

import pandas as pd

import plotly.express as px

import plotly.graph_objects as go

glass = pd.read_csv('/kaggle/input/glass/glass.csv')

X = glass.copy().drop(['Type'], axis=1)
y = glass['Type'].copy()

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda_model = LinearDiscriminantAnalysis(n_components=2)
lda_model.fit(X, y)
reduced_X = lda_model.transform(X).T

fig = px.scatter(
    glass, 
    x=reduced_X[0], 
    y=reduced_X[1], 
    color="Type", 
    hover_data=['Type'],
    color_continuous_scale='portland')
fig.show()

knnmodel = KNeighborsClassifier(n_neighbors=3)
knnmodel.fit(reduced_X.T, y)

fig = px.scatter(glass, x="Mg", y="Fe", color='Type', color_continuous_scale='portland')
fig.show()

fig = make_subplots(rows=1, cols=1)

fig.add_trace(go.Contour(
    x=X['Mg'],
    y=X['Fe'],
    z=lda_model.predict(X),
    showscale=False,
    opacity=0.40,
    colorscale='portland'
), row=1, col=1)

fig.add_trace(go.Scatter(
    x=X['Mg'], 
    y=X['Fe'],
    text=y,
    mode='markers',
    marker_symbol=y,
    marker=dict(color=y, colorscale='portland')
), row=1, col=1)

fig.update_layout(showlegend=False)

fig.show()

fig = make_subplots(rows=1, cols=1)
fig.add_trace(go.Contour(x=reduced_X[0], y=reduced_X[1], z=lda_model.predict(X), showscale=False, opacity=0.4, colorscale='portland'), row=1, col=1)
fig.add_trace(go.Scatter(x=reduced_X[0], y=reduced_X[1], text=y, mode='markers', marker_symbol=y, marker=dict(color=y, colorscale='portland')), row=1, col=1)
fig.show()