#Holdout method
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
#k-Partition cross validation(Cross-validation)
from sklearn.model_selection import cross_val_score
cores = cross_val_score(svc, X, y, cv=5)
Random Forest
from sklearn.ensemble import RandomForestClassifier
rfc= RandomForestClassifier(max_depth=100)
rfc.fit(X_train, y_train)
predicted= rfc.predict(X_test)
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(random_state=42)
lr.fit(X_train, y_train)
predicted= lr.predict(X_test)
from sklearn.svm import SVC
svc = SVC()
svc.fit(X_train,y_train)
predicted= svc.predict(X_test)
model.score(X_train, y_train)
model.score(X_test, y_test)
#Confusion matrix
from sklearn.metrics import confusion_matrix
confmat = confusion_matrix(y_test,predicted)
#Correct answer rate
from sklearn import metrics
accuracy = metrics.accuracy_score(y_test, predicted)
#accuracy
from sklearn.metrics import precision_score
precision = metrics.precision_score(y_test, predicted)
#Recall
from sklearn.metrics import recall_score
recall = metrics.recall_score(y_test, predicted)
#F value
from sklearn.metrics import f1_score
f1 = metrics.f1_score(y_test, predicted)
When evaluating other classifications, micro-average or macro-average may be specified as an argument.
f1 = metrics.f1_score(y_test, predicted, average='micro')
#Save model
import pickle
name = 'test.sav'
pickle.dump(model, open(name, 'wb'))
#Load model
loaded_model = pickle.load(open(name, 'rb'))
result = loaded_model.score(X_test, y_test)
--svm code
https://qiita.com/sotetsuk/items/3a5718bb1f945a383ceb
--Model evaluation
https://aidemy.net/courses/2010
--Save model
https://localab.jp/blog/save-and-load-machine-learning-models-in-python-with-scikit-learn/