几个机器学习的sklearn算法代码 下面展示一些 内联代码片 。
from sklearn import tree
import numpy as np
from preprocess import prepare_data
from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import confusion_matrix
import os
class classifier():
def __init__(self, feature_type = "mcbp", classifier_type = 'dt', target_names = ["apple","banana"]):
self.target_names = target_names
self.feature_type = feature_type
self.classifier_type = classifier_type
self.result_save_path = feature_type+'_'+classifier_type+'.txt'
if(classifier_type =='rf'):
self.clf = RandomForestClassifier(oob_score=False, random_state=2020, class_weight='balanced', n_jobs=8, n_estimators=50)
elif(classifier_type =='dt'):
self.clf = tree.DecisionTreeClassifier(splitter='best', max_depth=200,random_state=2020)
elif(classifier_type =='knn'):
self.clf = KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None,
n_jobs=8, n_neighbors=2, p=2, weights='uniform')
elif(classifier_type =='svm'):
self.clf = svm.SVC(C=20.0, kernel='rbf', class_weight='balanced', random_state=2020)
elif(classifier_type =='mlp'):
self.clf = MLPClassifier(hidden_layer_sizes=(512, 256), max_iter=50)
def run(self,train_dataset,test_dataset):
train_x, train_y, test_x, test_y = prepare_data(self.feature_type,train_dataset,test_dataset)
self.clf = self.clf.fit(train_x, train_y)
print(self.feature_type)
print(self.classifier_type)
train_result_y = self.clf.predict(train_x)
test_result_y = self.clf.predict(test_x)
train_report = metrics.classification_report(train_y, train_result_y,target_names=self.target_names, digits=6)
test_report = metrics.classification_report(test_y, test_result_y,target_names=self.target_names, digits=6)
confusion_matrix1 = confusion_matrix(test_y, test_result_y)
print(confusion_matrix1)
print('train')
print(train_report)
print('test')
print(test_report)
np.savetxt('confusion_matrix1.txt', confusion_matrix1)
with open(self.result_save_path, "w") as f2:
f2.write(test_report)
if __name__ == '__main__':
categories_path = r"X:\AMD_135\AMD_135_data"
train_dataset = np.load('./data/AMD135_train_compare.npy')
test_dataset = np.load('./data/AMD135_test_compare.npy')
# categories_path = r"X:\8407\8407_data"
# train_dataset = np.load('./data/8407_train_compare.npy')
# test_dataset = np.load('./data/8407_test_compare.npy')
# categories_path = r"X:\AMD_71\AMD_71_data"
# train_dataset = np.load('./data/AMD71_train_compare.npy')
# test_dataset = np.load('./data/AMD71_test_compare.npy')
target_names = os.listdir(categories_path)
# for feature_type in ["mcbp","fcg","image"]:
# for classifier_type in ['rf','svm','knn','mlp']:
# model = classifier(feature_type = feature_type, classifier_type = classifier_type,target_names = target_names)
# model.run(train_dataset,test_dataset)
# mcbp_mlp = classifier(feature_type = "mcbp", classifier_type = 'mlp',target_names = target_names)
# mcbp_mlp.run(train_dataset,test_dataset)
# mcbp_dt = classifier(feature_type = "mcbp", classifier_type = 'dt',target_names = target_names)
# mcbp_dt.run(train_dataset,test_dataset)
# mcbp_rf = classifier(feature_type = "mcbp", classifier_type = 'rf',target_names = target_names)
# mcbp_rf.run(train_dataset,test_dataset)
mcbp_svm = classifier(feature_type = "mcbp", classifier_type = 'svm',target_names = target_names)
mcbp_svm.run(train_dataset,test_dataset)
mcbp_knn = classifier(feature_type = "mcbp", classifier_type = 'knn',target_names = target_names)
mcbp_knn.run(train_dataset,test_dataset)
# fcg_dt = classifier(feature_type = "fcg", classifier_type = 'dt',target_names = target_names)
# fcg_dt.run(train_dataset,test_dataset)
# fcg_rf = classifier(feature_type = "fcg", classifier_type = 'rf',target_names = target_names)
# fcg_rf.run(train_dataset,test_dataset)
# fcg_svm = classifier(feature_type = "fcg", classifier_type = 'svm',target_names = target_names)
# fcg_svm.run(train_dataset,test_dataset)
# fcg_knn = classifier(feature_type = "fcg", classifier_type = 'knn',target_names = target_names)
# fcg_knn.run(train_dataset,test_dataset)
# fcg_mlp = classifier(feature_type = "fcg", classifier_type = 'mlp',target_names = target_names)
# fcg_mlp.run(train_dataset,test_dataset)
# image_dt = classifier(feature_type = "image", classifier_type = 'dt',target_names = target_names)
# image_dt.run(train_dataset,test_dataset)
# image_rf = classifier(feature_type = "image", classifier_type = 'rf',target_names = target_names)
# image_rf.run(train_dataset,test_dataset)
# image_svm = classifier(feature_type = "image", classifier_type = 'svm',target_names = target_names)
# image_svm.run(train_dataset,test_dataset)
# image_knn = classifier(feature_type = "image", classifier_type = 'knn',target_names = target_names)
# image_knn.run(train_dataset,test_dataset)
# image_mlp = classifier(feature_type = "image", classifier_type = 'mlp',target_names = target_names)
# image_mlp.run(train_dataset,test_dataset)
|