Created
November 28, 2017 17:15
-
-
Save steveway/db2f681055a4d3c1e482973d04cf25ee to your computer and use it in GitHub Desktop.
This file contains hidden or 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 numpy as np | |
| from os import listdir | |
| from os.path import isfile, join | |
| from PIL import Image | |
| from sklearn.datasets.base import Bunch | |
| from sklearn import svm | |
| from sklearn.naive_bayes import MultinomialNB | |
| def prepare_dataset(): | |
| data_list = [] | |
| target_list = np.array([]) | |
| mypath = r"./test_data/" | |
| onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))] | |
| onlyfiles = [f for f in onlyfiles if f.endswith("txt")] # we only want the txt files | |
| for f in onlyfiles: | |
| output_array = np.fromfile(join(mypath, f), np.uint32)#.reshape((16,16)) # target | |
| # output_file = open(join(mypath, f), "rb") | |
| input_array = np.asarray(Image.open(join(mypath, f.split(".")[0] + ".png")).convert("L")).flatten() # data | |
| # print(len(input_array)) | |
| #input_file = | |
| #data_list = np.append(data_list, input_array) | |
| data_list.append(input_array) | |
| target_list = np.append(target_list, output_array) | |
| #target_list.append(output_array) | |
| return Bunch(data=np.array(data_list), target=target_list) | |
| def main(): | |
| ascii_data = prepare_dataset() | |
| #print(ascii_data.target) | |
| print(ascii_data.data[0].reshape(1,-1) ) | |
| # print(ascii_data.target[0]) | |
| # print(prepare_dataset()) | |
| # clf = svm.SVC(gamma=0.001, C=100.) | |
| #clf = svm.SVC(gamma=0.001) | |
| clf = MultinomialNB() | |
| clf.fit(ascii_data.data[0].reshape(1,-1) , ascii_data.target[0]) | |
| print(ascii_data.data[0]) | |
| print(clf.predict(ascii_data.data[0])) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment