Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
assignments/7159CEM-Portfolio-main/ACO/Tree's problem.txt
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
60 lines (41 sloc)
1.71 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
from sklearn.model_selection import train_test_split | |
from sklearn.tree import DecisionTreeClassifier | |
from sklearn import tree | |
from sklearn.metrics import accuracy_score | |
import warnings | |
warnings.filterwarnings("ignore") | |
data1 = pd.read_csv('datasets/weather.numeric.csv') | |
data2 = pd.read_csv('datasets/weather.nominal.csv') | |
X= data1.drop(columns=['play'], axis=1) | |
y= data1['play'] | |
X_train, X_test, y_train, y_test= train_test_split(X,y,test_size= 0.3) | |
d1tree= DecisionTreeClassifier() | |
d1tree.fit(X_train,y_train) | |
predictions= d1tree.predict(X_test) | |
tree.plot_tree(d1tree) | |
plt.show() | |
from sklearn.metrics import classification_report, confusion_matrix | |
print("Accuracy:",accuracy_score(y_test,predictions)) | |
print(confusion_matrix(y_test,predictions)) | |
print('\n') | |
print(classification_report(y_test,predictions)) | |
from sklearn import preprocessing | |
string_to_int= preprocessing.LabelEncoder() #encode your data | |
data2=data2.apply(string_to_int.fit_transform) #fit and transform it | |
#To divide our data into attribute set and Label: | |
feature_cols = ['outlook','temperature','humidity','windy'] | |
X = data2[feature_cols] #contains the attribute | |
y = data2.play #contains the label | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30) | |
d2tree =DecisionTreeClassifier(criterion="entropy", random_state=100) # create a classifier object | |
d2tree.fit(X_train, y_train) | |
y_pred= d2tree.predict(X_test) | |
print("Accuracy:",accuracy_score(y_test, y_pred)) | |
print(confusion_matrix(y_test, y_pred)) | |
print(classification_report(y_test, y_pred)) | |
tree.plot_tree(d2tree) | |
plt.show() |