IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 几个机器学习的sklearn算法代码 -> 正文阅读

[游戏开发]几个机器学习的sklearn算法代码

几个机器学习的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)


  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-09-14 13:41:42  更:2021-09-14 13:41:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 16:18:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码