学习知识

多标签(multi-label)数据的学习问题,常用的分类器或者分类策略有哪些?
深度学习模型处理多标签(multi_label)分类任务——keras实战
[xgboost 与 lightgbm 多分类 与 多标签 任务 python代码实例]https://blog.csdn.net/lrs1353281004/article/details/103350559

xgboost 多分类任务

  1. from xgboost import XGBClassifier
  2. import numpy as np
  3. clf_multiclass = XGBClassifier()
  4. train_data = np.random.rand(500, 100) # 500 entities, each contains 100 features
  5. train_label = np.random.randint(5, size=500) # 5 targets
  6. val_data = np.random.rand(100, 100)
  7. clf_multiclass.fit(train_data,train_label)
  8. val_pred = clf_multiclass.predict(val_data)

lightgbm 多分类任务

  1. from lightgbm import LGBMClassifier
  2. import numpy as np
  3. clf_multiclass = LGBMClassifier()
  4. train_data = np.random.rand(500, 100) # 500 entities, each contains 100 features
  5. train_label = np.random.randint(5, size=500) # 5 targets
  6. val_data = np.random.rand(100, 100)
  7. clf_multiclass.fit(train_data,train_label)
  8. val_pred = clf_multiclass.predict(val_data)

xgboost 多标签任务

  1. from xgboost import XGBClassifier
  2. from sklearn.multiclass import OneVsRestClassifier
  3. import numpy as np
  4. clf_multilabel = OneVsRestClassifier(XGBClassifier())
  5. train_data = np.random.rand(500, 100) # 500 entities, each contains 100 features
  6. train_label = np.random.randint(2, size=(500,20)) # 20 targets
  7. val_data = np.random.rand(100, 100)
  8. clf_multilabel.fit(train_data,train_label)
  9. val_pred = clf_multilabel.predict(val_data)

lightgbm 多标签任务

  1. from lightgbm import LGBMClassifier
  2. from sklearn.multiclass import OneVsRestClassifier
  3. import numpy as np
  4. clf_multilabel = OneVsRestClassifier(LGBMClassifier())
  5. train_data = np.random.rand(500, 100) # 500 entities, each contains 100 features
  6. train_label = np.random.randint(2, size=(500,20)) # 20 targets
  7. val_data = np.random.rand(100, 100)
  8. clf_multilabel.fit(train_data,train_label)
  9. val_pred = clf_multilabel.predict(val_data)
  1. # ExtraTreeClassifier
  2. from sklearn.ensemble import BaggingClassifier
  3. from sklearn.tree import ExtraTreeClassifier
  4. X_train, X_test, y_train, y_test = train_test_split( X, y, random_state=0)
  5. extra_tree = ExtraTreeClassifier(random_state=0)
  6. cls = BaggingClassifier(extra_tree, random_state=0).fit( X_train, y_train)
  7. cls.score(X_test, y_test)
  8. # ExtraTreesClassifier
  9. from sklearn.ensemble import ExtraTreesClassifier
  10. clf = ExtraTreesClassifier(n_estimators=100, random_state=0)
  11. clf.fit(X_train, y_train)
  12. clf.predict(X_test)
  13. #
  14. from sklearn.neighbors import KNeighborsClassifier
  15. neigh = KNeighborsClassifier(n_neighbors=3)
  16. neigh.fit(X_train, y_train)
  17. print(neigh.predict(X_test))
  18. # predict_proba(X)返回测试数据X的返回概率估计
  19. print(neigh.predict_proba(y_test))
  20. #
  21. from sklearn.neighbors import RadiusNeighborsClassifier
  22. neigh = RadiusNeighborsClassifier(radius=1.0)
  23. neigh.fit(X_train, y_train)
  24. print(neigh.predict(X_test)
  25. # predict_proba(X)返回测试数据X的返回概率估计
  26. print(neigh.predict_proba(X_test)
  27. #
  28. from sklearn.ensemble import RandomForestClassifier
  29. from sklearn.datasets import make_classification
  30. X, y = make_classification(n_samples=1000, n_features=4,
  31. ... n_informative=2, n_redundant=0,
  32. ... random_state=0, shuffle=False)
  33. clf = RandomForestClassifier(max_depth=2, random_state=0)
  34. clf.fit(X_train, y_train)
  35. # predict_log_proba(X)预测X的类对数概率。将输入样本的预测类别对数概率计算为森林中树木的平均预测类别对数的对数
  36. print(clf.predict(X_test)
  37. # predict_proba(X)预测X的类概率。将输入样本的预测类别概率计算为森林中树木的平均预测类别概率。一棵树的类别概率是叶子中同一类别的样本的分数。
  38. print(predict_proba(X_test)