image_output_1.png
from sklearn.feature_selection import SelectKBest, chi2

from sklearn.manifold import TSNE

from sklearn.preprocessing import LabelEncoder

from sklearn.preprocessing import LabelEncoder

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df = pd.read_csv('../input/mushroom-classification/mushrooms.csv')

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df = df.apply(LabelEncoder().fit_transform)

f, ax = plt.subplots(1, 2, figsize = (15, 7))
df['class'].value_counts().plot.bar(ax=ax[0])
df['class'].value_counts().plot.pie(ax=ax[1], autopct = "%.2f%%");

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df = df.apply(LabelEncoder().fit_transform)

pca_2d = PCA(n_components=2)
pca_3d = PCA(n_components=3)

PCs_2d = pd.DataFrame(pca_2d.fit_transform(df.drop(['class'], axis=1)))
PCs_3d = pd.DataFrame(pca_3d.fit_transform(df.drop(['class'], axis=1)))

PCs_2d.columns = ['PC1_2d', 'PC2_2d']
PCs_3d.columns = ['PC1_3d', 'PC2_3d', 'PC3_3d']

from mpl_toolkits.mplot3d import Axes3D
x = PCs_3d.iloc[:, 1]
y = PCs_3d.iloc[:, 2]
c = df['class']
ax.set_zlabel('PC3')

x = df.drop(['class'], axis=1)
y = df['class']

from sklearn.feature_selection import SelectKBest, chi2
sb = SelectKBest(chi2, k=5)
X_new = sb.fit_transform(x, y)
X_new.shape

mask = sb.get_support()
new_features = x.columns[mask]

model = TSNE(n_components=2, random_state=0)
tsne_model = model.fit_transform(x)

tsne_model[:, 0].T