<commit_before>#!/usr/bin/python3
import pickle
import operator
import time
import datetime
from vectorize_data import file_to_xs_ys, load_tag_index_map
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
import pydotplus
from sklearn import tree

random_state = 1


# filename = "../../res/head_safebooru.xml"
filename = "../../res/sample_safebooru.xml"
tag_index_map = load_tag_index_map("../../res/tag_index_map.p")
feature_names = [x[0] for x in sorted(tag_index_map.items(), key=operator.itemgetter(1))]
xs, ys = file_to_xs_ys(filename, tag_index_map)


X_other, X_valid, y_other, y_valid = train_test_split(xs, ys, test_size=10.0/100, random_state=random_state)
X_train, X_test, y_train, y_test = train_test_split(X_other, y_other, test_size=10.0/90, random_state=random_state)

def save_model(filename, regr):
	pickle.dump(regr, open(filename, "wb"))

def load_model(filename):
	regr = pickle.load(open(filename, "rb"))
	return regr

def timestamp():
	return datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')


#decision tree regressor
from sklearn.tree import DecisionTreeRegressor
regr = DecisionTreeRegressor(max_depth=10, min_samples_split=500, min_samples_leaf=25, random_state=random_state)
'''
AREAS OF INTEREST:
"criterion":["mse","mae"]
"max_features":["auto","sqrt","log2",1,2]
"max_depth":[1,3,5,10]
"min_samples_split":[2,4]
"min_samples_leaf":[1,4]
"max_leaf_nodes":[None,10]
"random_state":[random_state]
'''
print("[{}] training decision tree regressor".format(timestamp()))
regr.fit(X_train, y_train)
print("[{}] training complete".format(timestamp()))
save_model("../../res/models/decision_tree.p", regr)

# regr = load_model("../../res/models/decision_tree.p")

# print("inputs:",X_test)
# print("truth:",y_test)
# print("predictions:",regr.predict(X_test))
print("mean_absolute_error:", mean_absolute_error(y_test, regr.predict(X_test)))

print("exporting decision tree regressor visualization")
dot_data = tree.export_graphviz(regr, feature_names=feature_names, filled=True, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("../../figures/regression_tree.pdf")


#DEBUG
#DEBUG
#DEBUG
exit()
#DEBUG
#DEBUG
#DEBUG



#random forest of decision trees regressors
from sklearn.ensemble import RandomForestRegressor
regr = RandomForestRegressor(n_estimators=10, max_depth=6, min_samples_split=50, min_samples_leaf=15, random_state=random_state)
'''
AREAS OF INTEREST:
"n_estimators":[10]
"criterion":["mse","mae"]
"max_features":["auto","sqrt","log2",1,2]<commit_msg>added model and note to train_models.py<commit_after>#!/usr/bin/python3
import pickle
import operator
import time
import datetime
from vectorize_data import file_to_xs_ys, load_tag_index_map
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error
import pydotplus
from sklearn import tree

random_state = 1

'''
TODO
----
Choosing what model(s) to train should be controlled by the program flow, rather than messily commenting out code.
'''


filename = "../../res/head_safebooru.xml"
# filename = "../../res/sample_safebooru.xml"
tag_index_map = load_tag_index_map("../../res/tag_index_map.p")
feature_names = [x[0] for x in sorted(tag_index_map.items(), key=operator.itemgetter(1))]
xs, ys = file_to_xs_ys(filename, tag_index_map)


X_other, X_valid, y_other, y_valid = train_test_split(xs, ys, test_size=10.0/100, random_state=random_state)
X_train, X_test, y_train, y_test = train_test_split(X_other, y_other, test_size=10.0/90, random_state=random_state)

def save_model(filename, regr):
	pickle.dump(regr, open(filename, "wb"))

def load_model(filename):
	regr = pickle.load(open(filename, "rb"))
	return regr

def timestamp():
	return datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')



from sklearn.decomposition import PCA
pca = PCA(n_components=100)
pca.fit(X_train)
X_train = pca.transform(X_train)
X_test = pca.transform(X_test)





from sklearn.linear_model import LinearRegression
regr = LinearRegression()
print("[{}] training linear regressor".format(timestamp()))
regr.fit(X_train, y_train)
print("[{}] training complete".format(timestamp()))
print("mean_absolute_error:", mean_absolute_error(y_test, regr.predict(X_test)))
save_model("../../res/models/linear_regression.p", regr)


#DEBUG
#DEBUG
#DEBUG
exit()
#DEBUG
#DEBUG
#DEBUG

#decision tree regressor
from sklearn.tree import DecisionTreeRegressor
# regr = DecisionTreeRegressor(max_depth=4, min_samples_split=500, min_samples_leaf=25, random_state=random_state)
# regr = DecisionTreeRegressor(max_depth=4, min_samples_split=10000, min_samples_leaf=5000, random_state=random_state)
'''
AREAS OF INTEREST:
"criterion":["mse","mae"]
"max_features":["auto","sqrt","log2",1,2]
"max_depth":[1,3,5,10]
"min_samples_split":[2,4]
"min_samples_leaf":[1,4]
"max_leaf_nodes":[None,10]
"random_state":[random_state]
'''
# print("[{}] training decision tree regressor".format(timestamp()))
# regr.fit(X_train, y_train)
# print("[{}] training complete".format(timestamp()))
# save_model("../../res/models/decision_tree.p", regr)

regr = load_model("../../res/models/decision_tree.p")

# print("inputs:",X_test)
# print("truth:",y_test)
# print("predictions:",regr.predict(X_test))
# print("mean_absolute_error:", mean_absolute_error(y_test, regr.predict(X_test)))

print("exporting decision tree regressor visualization")
dot_data = tree.export_graphviz(regr, feature_names=feature_names, filled=True, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf("../../figures/regression_tree.pdf")



#DEBUG
#DEBUG
#DEBUG
exit()
#DEBUG
#DEBUG
#DEBUG



#random forest of decision trees regressors
from sklearn.ensemble import RandomForestRegressor
regr = RandomForestRegressor(n_estimators=10, max_depth=4, min_samples_split=500, min_samples_leaf=25, random_state=random_state)
'''
AREAS OF INTEREST:
"n_estimators":[10]
"criterion":["mse","mae"]
"max_features":["auto","sqrt","log2",1,2]
{'fraction_non_alphanumeric': 0.15107084019769357, 'fraction_numerical': 0.017957166392092258, 'mean_word_length': 7.6852646638054365, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6520881', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2146, 'n_words': 404}