import pandas as pd
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.tree import export_graphviz
from six import StringIO
import pydotplus
from sklearn.tree import DecisionTreeClassifier

dataset = pd.read_csv('FarmingData/DataCollected.csv')
dataset.shape

dataset['chemicalsType'].replace({'organic': 1}, inplace=True)
tableData = dataset[(dataset["SeedType"] == 'Nadu')]

dataset['WasteAmount'] = (dataset['wastage'] / 100) * dataset['production']

tableData = dataset.head()
print(tableData)

# Preparing the Data
feature_cols = ['Seeds', 'DailyWater', 'chemicalsType']

X = dataset[['Seeds', 'DailyWater', 'chemicalsType']]
y = dataset['WasteAmount']

# Training and Making Predictions 0.80 tranning

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

# set  DecisionTreeClassifier algorithm
classifier = DecisionTreeClassifier()
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)

dot_data = StringIO()
export_graphviz(classifier, out_file=dot_data,
                filled=True, rounded=True,
                special_characters=True,feature_names = feature_cols,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

# used Seeds Kg , DailyWater , chemicalsType Used
new_input = [[18000,1000,2]]
pred = classifier.predict(new_input)
print(pred)

# measure Accuracy
print("Accuracy:",metrics.accuracy_score(y_test, y_pred) * 100 )