Tree Portfolio Project

This project took me quite a while (around 6+ hours) due to the output nodes being 1 and 2 instead of 7 (I constantly got errors about the shape). I managed to finally get the graph working by creating a validation in the fit function as well:

import pandas as pd
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split
from collections import Counter
from sklearn.compose import ColumnTransformer
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, InputLayer
from sklearn.metrics import classification_report
from tensorflow.keras.utils import to_categorical
import numpy as np
from sklearn.preprocessing import Normalizer
from sklearn import preprocessing
from tensorflow.keras.optimizers import Adam
import matplotlib.pyplot as plt

data = pd.read_csv(‘cover_data.csv’)
#data = preprocessing.normalize([data])
print(data.info())
print(Counter(data[‘class’]))
y = data.iloc[:,-1] # 55 Classes
x = data.iloc[:,0:-1]

x = pd.get_dummies(x)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
numerical_x = x.select_dtypes(include=[‘float64’, ‘int64’])
numerical_columns = numerical_x.columns
my_transformer = ColumnTransformer([(“numeric”, Normalizer(), [‘Elevation’, ‘Aspect’,
‘Slope’, ‘Horizontal_Distance_To_Hydrology’, ‘Vertical_Distance_To_Hydrology’, ‘Horizontal_Distance_To_Roadways’,
‘Hillshade_9am’, ‘Hillshade_Noon’, ‘Hillshade_3pm’, ‘Horizontal_Distance_To_Fire_Points’])])
le = LabelEncoder()
x_train = my_transformer.fit_transform(x_train)
x_test = my_transformer.transform(x_test)
y_train = le.fit_transform(y_train.astype(str))
y_test = le.transform(y_test.astype(str))
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

opt = Adam(learning_rate=0.01)
model = Sequential()
model.add(InputLayer(input_shape=(x_train.shape[1],)))
model.add(Dense(8, activation=‘relu’))
model.add(Dense(7, activation=‘softmax’))
model.compile(loss=‘categorical_crossentropy’, optimizer=opt, metrics=[‘accuracy’])
history = model.fit(x_test, y_test, epochs=35, batch_size=1, verbose=1, validation_data=(x_test,y_test))

loss, acc = model.evaluate(x_test, y_test, verbose=0)
print("Loss: ", loss, "Accuracy: ", acc)

model.save(‘AI_Tree’)

list all data in history

print(history.history.keys())

summarize history for accuracy

plt.plot(history.history[‘accuracy’])
plt.plot(history.history[‘val_accuracy’])
plt.title(‘model accuracy’)
plt.ylabel(‘accuracy’)
plt.xlabel(‘epoch’)
plt.legend([‘train’, ‘test’], loc=‘upper left’)
plt.show()

summarize history for loss

plt.plot(history.history[‘loss’])
plt.plot(history.history[‘val_loss’])
plt.title(‘model loss’)
plt.ylabel(‘loss’)
plt.xlabel(‘epoch’)
plt.legend([‘train’, ‘test’], loc=‘upper left’)
plt.show()