I'm studying with this book How to make AI / machine learning / deep learning apps with Python
scikit-learn A classic machine learning framework for Python http://scikit-learn.org/
It has the following features
--Supports various algorithms used in machine learning -Sample data is included so that you can try machine learning immediately --Has a function to verify the results of machine learning --High affinity with other libraries often used in machine learning (Pnadas, Numpy, scipy, Matplotlib etc) --Free commercial use due to BSD licensed open source
https://scikit-learn.org/stable/tutorial/machine_learning_map/index.html You can select an algorithm by following the conditions such as what kind of machine learning you want to do and what kind of data you are preparing.
and.py
#Import the library
from sklearn.svm import LinearSVC #Package for utilizing the LinearSVC algorithm(sklearn.svm.LinearSVC)
from sklearn.metrics import accuracy_score #Package for evaluating test results(sklearn.metrics.accuracy_score)
#Prepare learning data
learn_data = [[0, 0], [1, 0], [0, 1], [1, 1]] #Input data for learning(AND input value)
learn_label = [0, 0, 0, 1] #Result data for learning(AND output value)
#Algorithm specification
clf = LinearSVC()
#learn(Pass input data and result data for learning)
clf.fit(learn_data, learn_label) #Pass input data and result data for learning
#Verify learning
test_data = [[0, 0], [1, 0], [0, 1], [1, 1]] #Prepare input data for test data
test_label = clf.predict(test_data) #Get test data results
#Display test data results
print(test_data, "Prediction result", test_label)
#Display correct answer rate
Accuracy_rate= accuracy_score([0, 0, 0, 1], test_label) # accuracy_score(Correct answer data,Test results)
print("Correct answer rate:", Accuracy_rate)
[[0, 0], [1, 0], [0, 1], [1, 1]]Prediction result[0 0 0 1]
Correct answer rate: 1.0
You are learning AND logical operations correctly. Those that can be classified linearly can be solved by the LinearSVC algorithm. But what about XOR?
--Linear SVM Classification ... Linear SVM Classification --accuracy ... accuracy --classification ... classification --classifier ... classifier --fit ... learn (fit) --predict ... predict
xor.py
#Import the library
from sklearn.svm import LinearSVC #Package for utilizing the LinearSVC algorithm(sklearn.svm.LinearSVC)
from sklearn.metrics import accuracy_score #Package for evaluating test results(sklearn.metrics.accuracy_score)
#Prepare learning data
learn_data = [[0, 0], [1, 0], [0, 1], [1, 1]] #Input data for learning(XOR input value)
learn_label = [0, 1, 1, 0] #Result data for learning(XOR output value)
#Algorithm specification
clf = LinearSVC()
#learn(Pass input data and result data for learning)
clf.fit(learn_data, learn_label) #Pass input data and result data for learning
#Verify learning
test_data = [[0, 0], [1, 0], [0, 1], [1, 1]] #Prepare input data for test data
test_label = clf.predict(test_data) #Get test data results
#Display test data results
print(test_data, "Prediction result", test_label)
#Display correct answer rate
Accuracy_rate= accuracy_score([0, 1, 1, 0], test_label) # accuracy_score(Correct answer data,Test results)
print("Correct answer rate:", Accuracy_rate)
[[0, 0], [1, 0], [0, 1], [1, 1]]Prediction result[0 0 0 0]
Correct answer rate: 0.5
Well, it can't be classified correctly by linear.
xor2.py
#Import the library
from sklearn.neighbors import KNeighborsClassifier #Package for utilizing the LinearSVC algorithm(sklearn.svm.LinearSVC)
from sklearn.metrics import accuracy_score #Package for evaluating test results(sklearn.metrics.accuracy_score)
#Prepare learning data
learn_data = [[0, 0], [1, 0], [0, 1], [1, 1]] #Input data for learning(XOR input value)
learn_label = [0, 1, 1, 0] #Result data for learning(XOR output value)
#Algorithm specification
clf = KNeighborsClassifier(n_neighbors = 1)
#learn(Pass input data and result data for learning)
clf.fit(learn_data, learn_label) #Pass input data and result data for learning
#Verify learning
test_data = [[0, 0], [1, 0], [0, 1], [1, 1]] #Prepare input data for test data
test_label = clf.predict(test_data) #Get test data results
#Display test data results
print(test_data, "Prediction result", test_label)
#Display correct answer rate
Accuracy_rate= accuracy_score([0, 1, 1, 0], test_label) # accuracy_score(Correct answer data,Test results)
print("Correct answer rate:", Accuracy_rate)
[[0, 0], [1, 0], [0, 1], [1, 1]]Prediction result[0 1 1 0]
Correct answer rate: 1.0
I was able to learn using the KNeighborsClassifier algorithm.