<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Decision Tree Classifier - random_state
In the previous notebook we got an accuracy score of just over 40%.
Lets just do that again.
Step1:  and again.
Step2:  one more time 
Step3:  We see that the results are not the same. This is because the Decision Tree Classifier chooses a feature at random in order to try to avoid overfitting. As we are about to start trying to improve the results by trying different strategies of preparing and loading data having varying will be unhelpful.
To aviod this we can manually set the random_state.
<ASSISTANT_TASK:>
Python Code:
# Imports
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
# Training Data
training_raw = pd.read_table("../data/training_data.dat")
df_training = pd.DataFrame(training_raw)
# test Data
test_raw = pd.read_table("../data/test_data.dat")
df_test = pd.DataFrame(test_raw)
# target names
target_categories = ['Unclassified','Art','Aviation','Boating','Camping /Walking /Climbing','Collecting']
# Extract target results from panda
target = df_training["CategoryID"].values
# Create classifier class
model_dtc = DecisionTreeClassifier()
# features
feature_names_integers = ['Barcode','UnitRRP']
# Extra features from panda (without description)
training_data_integers = df_training[feature_names_integers].values
training_data_integers[:3]
# train model
model_dtc.fit(training_data_integers, target)
# Extract test data and test the model
test_data_integers = df_test[feature_names_integers].values
test_target = df_test["CategoryID"].values
expected = test_target
predicted_dtc = model_dtc.predict(test_data_integers)
print(metrics.classification_report(expected, predicted_dtc, target_names=target_categories))
print(metrics.confusion_matrix(expected, predicted_dtc))
metrics.accuracy_score(expected, predicted_dtc, normalize=True, sample_weight=None)
Explanation: Decision Tree Classifier - random_state
In the previous notebook we got an accuracy score of just over 40%.
Lets just do that again.
End of explanation
model_dtc = DecisionTreeClassifier()
model_dtc.fit(training_data_integers, target)
predicted_dtc = model_dtc.predict(test_data_integers)
metrics.accuracy_score(expected, predicted_dtc, normalize=True, sample_weight=None)
Explanation: and again.
End of explanation
model_dtc = DecisionTreeClassifier()
model_dtc.fit(training_data_integers, target)
predicted_dtc = model_dtc.predict(test_data_integers)
metrics.accuracy_score(expected, predicted_dtc, normalize=True, sample_weight=None)
Explanation: one more time :)
End of explanation
model_dtc = DecisionTreeClassifier(random_state=511)
model_dtc.fit(training_data_integers, target)
predicted_dtc = model_dtc.predict(test_data_integers)
metrics.accuracy_score(expected, predicted_dtc, normalize=True, sample_weight=None)
model_dtc = DecisionTreeClassifier(random_state=511)
model_dtc.fit(training_data_integers, target)
predicted_dtc = model_dtc.predict(test_data_integers)
metrics.accuracy_score(expected, predicted_dtc, normalize=True, sample_weight=None)
Explanation: We see that the results are not the same. This is because the Decision Tree Classifier chooses a feature at random in order to try to avoid overfitting. As we are about to start trying to improve the results by trying different strategies of preparing and loading data having varying will be unhelpful.
To aviod this we can manually set the random_state.
End of explanation
<END_TASK>