<filename>Type Classification/mc-2.py
# -*- coding: utf-8 -*-
"""Mush-class-2.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1qQgfppO6uOnSFkAcZLSXxW-1KLEsS94-
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import secrets
import tensorflow as tf

df=pd.read_csv('mushrooms.csv')
df=df.dropna(how='any')

b=list (df['stalk-root'].unique())
b

df['stalk-root']=df['stalk-root'].replace('?',(secrets.choice(b)))
df['Type']=df['class']
df=df.drop(columns=['class'])

from sklearn.preprocessing import LabelEncoder
le1=LabelEncoder()
le2=LabelEncoder()
df['cap-shape']=le1.fit_transform(df['cap-shape'])
df['cap-surface']=le1.fit_transform(df['cap-surface'])
df['cap-color']=le1.fit_transform(df['cap-color'])
df['bruises']=le1.fit_transform(df['bruises'])
df['odor']=le1.fit_transform(df['odor'])
df['gill-attachment']=le1.fit_transform(df['gill-attachment'])
df['gill-spacing']=le1.fit_transform(df['gill-spacing'])
df['gill-size']=le1.fit_transform(df['gill-size'])
df['gill-color']=le1.fit_transform(df['gill-color'])
df['stalk-shape']=le1.fit_transform(df['stalk-shape'])
df['stalk-root']=le1.fit_transform(df['stalk-root'])
df['stalk-surface-above-ring']=le1.fit_transform(df['stalk-surface-above-ring'])
df['stalk-surface-below-ring']=le1.fit_transform(df['stalk-surface-below-ring'])
df['stalk-color-above-ring']=le1.fit_transform(df['stalk-color-above-ring'])
df['stalk-color-below-ring']=le1.fit_transform(df['stalk-color-below-ring'])
df['veil-type']=le1.fit_transform(df['veil-type'])
df['veil-color']=le1.fit_transform(df['veil-color'])
df['ring-number']=le1.fit_transform(df['ring-number'])
df['ring-type']=le1.fit_transform(df['ring-type'])
df['spore-print-color']=le1.fit_transform(df['spore-print-color'])
df['population']=le1.fit_transform(df['population'])
df['habitat']=le1.fit_transform(df['habitat'])
df['Type']=le2.fit_transform(df['Type'])
df

X=df.iloc[:,:-1].values
Y=df.iloc[:,-1].values

y=Y.reshape(len(Y),1)
y

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units=30, activation='relu'))
ann.add(tf.keras.layers.Dense(units=60, activation='relu'))
ann.add(tf.keras.layers.Dense(units=90, activation='relu'))
ann.add(tf.keras.layers.Dense(units=1,activation='sigmoid'))

ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
history=ann.fit(X_train, y_train, batch_size=15, epochs=150)
ann.save("MC.h5")

plt.figure(0)
plt.plot(history.history['accuracy'], label='training accuracy')
plt.title('Accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend()
plt.savefig('Accuracy.png')

plt.figure(1)
plt.plot(history.history['loss'], label='training loss')
plt.title('Loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.savefig('Loss.png')
print("Saved Model & Graph to disk")

y_pred = ann.predict(X_test)
y_pred=np.round(y_pred)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

from sklearn.metrics import accuracy_score
print("Accuracy Score for the algorithm=>{}%".format(accuracy_score(y_test,y_pred)*100))
<filename>Type Classification/mc-2.py
# -*- coding: utf-8 -*-
"""Mush-class-2.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1qQgfppO6uOnSFkAcZLSXxW-1KLEsS94-
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import secrets
import tensorflow as tf

df=pd.read_csv('mushrooms.csv')
df=df.dropna(how='any')

b=list (df['stalk-root'].unique())
b

df['stalk-root']=df['stalk-root'].replace('?',(secrets.choice(b)))
df['Type']=df['class']
df=df.drop(columns=['class'])

from sklearn.preprocessing import LabelEncoder
le1=LabelEncoder()
le2=LabelEncoder()
df['cap-shape']=le1.fit_transform(df['cap-shape'])
df['cap-surface']=le1.fit_transform(df['cap-surface'])
df['cap-color']=le1.fit_transform(df['cap-color'])
df['bruises']=le1.fit_transform(df['bruises'])
df['odor']=le1.fit_transform(df['odor'])
df['gill-attachment']=le1.fit_transform(df['gill-attachment'])
df['gill-spacing']=le1.fit_transform(df['gill-spacing'])
df['gill-size']=le1.fit_transform(df['gill-size'])
df['gill-color']=le1.fit_transform(df['gill-color'])
df['stalk-shape']=le1.fit_transform(df['stalk-shape'])
df['stalk-root']=le1.fit_transform(df['stalk-root'])
df['stalk-surface-above-ring']=le1.fit_transform(df['stalk-surface-above-ring'])
df['stalk-surface-below-ring']=le1.fit_transform(df['stalk-surface-below-ring'])
df['stalk-color-above-ring']=le1.fit_transform(df['stalk-color-above-ring'])
df['stalk-color-below-ring']=le1.fit_transform(df['stalk-color-below-ring'])
df['veil-type']=le1.fit_transform(df['veil-type'])
df['veil-color']=le1.fit_transform(df['veil-color'])
df['ring-number']=le1.fit_transform(df['ring-number'])
df['ring-type']=le1.fit_transform(df['ring-type'])
df['spore-print-color']=le1.fit_transform(df['spore-print-color'])
df['population']=le1.fit_transform(df['population'])
df['habitat']=le1.fit_transform(df['habitat'])
df['Type']=le2.fit_transform(df['Type'])
df

X=df.iloc[:,:-1].values
Y=df.iloc[:,-1].values

y=Y.reshape(len(Y),1)
y

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units=30, activation='relu'))
ann.add(tf.keras.layers.Dense(units=60, activation='relu'))
ann.add(tf.keras.layers.Dense(units=90, activation='relu'))
ann.add(tf.keras.layers.Dense(units=1,activation='sigmoid'))

ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
history=ann.fit(X_train, y_train, batch_size=15, epochs=150)
ann.save("MC.h5")

plt.figure(0)
plt.plot(history.history['accuracy'], label='training accuracy')
plt.title('Accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend()
plt.savefig('Accuracy.png')

plt.figure(1)
plt.plot(history.history['loss'], label='training loss')
plt.title('Loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.savefig('Loss.png')
print("Saved Model & Graph to disk")

y_pred = ann.predict(X_test)
y_pred=np.round(y_pred)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

from sklearn.metrics import accuracy_score
print("Accuracy Score for the algorithm=>{}%".format(accuracy_score(y_test,y_pred)*100))
# -*- coding: utf-8 -*- Mush-class-2.ipynb  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1qQgfppO6uOnSFkAcZLSXxW-1KLEsS94-