Machine Learning Using Jupyter Notebook

The start of my journey to develop machine learning skills as a mechanical engineer.

Lets start with something fun, like classifying Skyrim weapon sets.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, ConfusionMatrixDisplay
df = pd.read_csv(r"C:\Users\harri\OneDrive\Documents\Python Projects\Skyrim_Weapons_Dataset\Skyrim_Weapons.csv")
print(df.head())

Looking at the data I imported, there are a variety of features like Damage, Weight, Gold, Upgrade Material, Perk, Type, Category, and Speed.

If I try to train a model with all of them, it might confuse the accuracy. For example, the Gold (price of the weapon) probably is a poor indicator of what type of weapon it actually is. Therefore, it is best to determine what is relevant.

If I want to build a model that correctly predicts the label of category, then I should pick the best features to train on, such as damage and weight.

X = df[['Damage', 'Weight']]
y = df['Category']
X.describe()
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size = 0.2,
random_state = 42
)
model = KNeighborsClassifier(n_neighbors = 3, weights = 'uniform')
model.fit(X_train, y_train)
predictions = model.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
print('Accuracy: ', accuracy)
print('\nPredictions:')
print(predictions)
print('\nActual Label:')
print(y_test)
cm = confusion_matrix(y_test, predictions)
disp = ConfusionMatrixDisplay(
confusion_matrix = cm,
display_labels = y.unique()
)
disp.plot()
print('Accuracy: ', accuracy)
plt.show()