-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPlay Tennis.py
30 lines (27 loc) · 1.29 KB
/
Play Tennis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Load libraries
import numpy as np
import pandas as pd
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
df=pd.read_csv("Play Tennis.csv")
from sklearn import preprocessing
string_to_int= preprocessing.LabelEncoder() #encode your data
df=df.apply(string_to_int.fit_transform) #fit and transform it
#To divide our data into attribute set and Label:
feature_cols = ['Outlook','Temprature','Humidity','Wind']
X = df[feature_cols ] #contains the attribute
y = df.Play_Tennis #contains the label
#To divide our data into training and test sets:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# perform training
from sklearn.tree import DecisionTreeClassifier # import the classifier
classifier = DecisionTreeClassifier(criterion="entropy") # create a classifier object
classifier.fit(X_train, y_train) # fit the classifier with X and Y data
#Predict the response for test dataset
y_pred= classifier.predict(X_test)
print(y_pred)
pred= classifier.predict([[0,1,0,1]])
print(pred)
# Model Accuracy, how often is the classifier correct?
from sklearn.metrics import accuracy_score
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))