Import Necessary Packages
import pandas as pd
import numpy as np
Load Dataset
df = pd.read_csv("Q1_training-tic-tac-toe.data/tic-tac-toe.data.txt")
df
Output:
Data Preprocessing
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
df['x'] = labelencoder.fit_transform(df['x'])
df['x']
output:
df['x.1'] = labelencoder.fit_transform(df['x.1'])
df['x.3'] = labelencoder.fit_transform(df['x.3'])
df['x.4'] = labelencoder.fit_transform(df['x.4'])
df['o'] = labelencoder.fit_transform(df['o'])
df['o.1'] = labelencoder.fit_transform(df['o.1'])
df['o.2'] = labelencoder.fit_transform(df['o.2'])
df['o.3'] = labelencoder.fit_transform(df['o.3'])
df['positive'] = labelencoder.fit_transform(df['positive'])
df
X = df.iloc[:, 0:9].values
y = df.iloc[:, 9].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
from sklearn.preprocessing import StandardScaler
feature_scaler = StandardScaler()
X_train = feature_scaler.fit_transform(X_train)
X_test = feature_scaler.transform(X_test)
2. Create a Random Forest Model (random_state = 0) using k- Cross-Validation Technique
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators=100, random_state=0)
from sklearn.model_selection import cross_val_score
all_accuracies = cross_val_score(estimator=classifier, X=X_train, y=y_train, cv=5)
all_accuracies
output:
array([0.91034483, 0.92307692, 0.9020979 , 0.93006993, 0.98601399])
Accuracy = all_accuracies[4]
Accuracy = round(Accuracy, 2)
3. Apply Ada Boost algorithm to improve the accuracy score (random_state = 0).
from sklearn.ensemble import AdaBoostClassifier
from sklearn import metrics
abc = AdaBoostClassifier(n_estimators=50,
learning_rate=1, random_state = 0)
# Train Adaboost Classifer
model = abc.fit(X_train, y_train)
y_pred = model.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
output:
Accuracy: 0.7708333333333334
Accuracy_Adaboost = metrics.accuracy_score(y_test, y_pred)
Accuracy_Adaboost = round(Accuracy_Adaboost, 2)
list_of_result_values = []
list_of_result_values.append(Accuracy)
list_of_result_values.append(Accuracy_Adaboost)
list_of_result_values
output:
[0.99, 0.77]
In [78]:
Save Result into csv file
file = open('./output/output.csv','a+')
file.write("Question1 Output" +"\n")
file.write(str(list_of_result_values) +"\n")
file.close()
Comments