将数组转换为MultiLabelBinarizer的列表

时间:2019-02-06 11:39:34

标签: python arrays list scikit-learn

我有以下数组:"['book', 'read']" "['cup', 'drink']"等,我想将其转换为一个列表,使我可以应用MultiLabelBinarizer

目前,它要么给我单独的字符,要么仅输出0。

Y = train_labels.iloc[:, 0].values
values = np.array(Y)

mlb = MultiLabelBinarizer(classes=("drink","cup","book", "read"))
output = mlb.fit_transform(values)
print(output)  

预期结果:

[0 0 1 1]
[1 1 0 0]

实际结果:

 [0 0 0 0]
 [0 0 0 0]

2 个答案:

答案 0 :(得分:1)

我怀疑您需要注意class Example extends StatefulWidget { // code @override EventPageState createState() => ExampleState(); } class ExampleState extends State<Example> { @override Widget build(BuildContext context) { return Container( child: Column( children: <Widget>[ Text("Static Widget") CustomDynamicWidget(), ], ), ) } Widget CustomDynamicWidget() { if (...) { return new Text("Dynamic Widget IF block") } else if (...) { return new Text("Dynamic Widget ELSE IF block") } return Container(); } } 输入的正确格式。

  

y:可迭代的迭代器
  每个样本的一组标签(任何可排序和可哈希的对象)。

证明:

<html>

<head>
  <script type="text/javascript">
    var clientPlus = {
      "Client A": {
        "Transactional": {
          "Single": ["Successful", "Busted"],
          "Third Party": ["Successful", "Busted"],
          "Joint": ["Successful", "Busted"]
        },
        "Monthly": {
          "Single": ["Successful", "Busted"],
          "Third Party": ["Successful", "Busted"],
          "Joint": ["Successful", "Busted"]
        }
      },
      "Client B": {
        "Transactional": {
          "Single": ["Successful", "Busted"],
          "Third Party": ["Successful", "Busted"],
          "Joint": ["Successful", "Busted"]
        },
        "Monthly": {
          "Single": ["Successful", "Busted"],
          "Third Party": ["Successful", "Busted"],
          "Joint": ["Successful", "Busted"]
        }
      },
      "Client C": {
        "Transactional": {
          "Single": ["Successful", "Busted"],
          "Third Party": ["Successful", "Busted"],
          "Joint": ["Successful", "Busted"]
        },
        "Monthly": {
          "Single": ["Successful", "Busted"],
          "Third Party": ["Successful", "Busted"],
          "Joint": ["Successful", "Busted"]
        }
      }
    }


    window.onload = function() {

      //Get html elements
      var clientSel = document.getElementById("clientSel");
      var invoicetypeSel = document.getElementById("invoicetypeSel");
      var payerSel = document.getElementById("payerSel");
      var sorbSel = document.getElementById("sorbSel");

      //Load clients
      for (var client in clientPlus) {
        clientSel.options[clientSel.options.length] = new Option(client, client);
      }

      //client Changed
      clientSel.onchange = function() {

        invoicetypeSel.length = 1; // remove all options bar first
        payerSel.length = 1; // remove all options bar first
        sorbSel.length = 1; // remove all options bar first

        if (this.selectedIndex < 1)
          return; // done

        for (var invoicetype in clientPlus[this.value]) {
          invoicetypeSel.options[invoicetypeSel.options.length] = new Option(invoicetype, invoicetype);
        }
      }
      //Invoice Type Changed
      invoicetypeSel.onchange = function() {

        payerSel.length = 1; // remove all options bar first
        sorbSel.length = 1; // remove all options bar first

        if (this.selectedIndex < 1)
          return; // done

        for (var payer in clientPlus[this.value]) {
          payerSel.options[payerSel.options.length] = new Option(payer, payer);
        }
      }

      //Payer Changed
      payerSel.onchange = function() {
        sorbSel.length = 1; // remove all options bar first

        if (this.selectedIndex < 1)
          return; // done

        var sorbs = clientPlus[clientSel.value][invoicetypeSel.value][this.value];
        for (var i = 0; i < sorbs.length; i++) {
          sorbSel.options[sorbSel.options.length] = new Option(sorbs[i], sorbs[i]);
        }
      }
    }
  </script>
</head>

<body>
  <form name="myform" id="myForm">
    <select id="clientSel" size="1">
      <option value="" selected="selected">Select Client:</option>
    </select>
    <br>
    <br>

    <select id="invoicetypeSel" size="1">
      <option value="" selected="selected">Select Invoice Type:</option>
    </select>
    <br>
    <br>
    <select id="payerSel" size="1">
      <option value="" selected="selected">Select Payer Type:</option>
    </select>
    <br>
    <br>
    <select id="sorbSel" size="1">
      <option value="" selected="selected">Successful or Busted?</option>
    </select>
  </form>

</body>

</html>

请让我们知道这是否可以解决您的问题。

有关数据格式的注释

您是否应该坚持将数据放置在帖子中指定的数组中?

MultiLabelBinarizer

以下代码段会将其转换为正确的格式:

txt = [['book', 'read'],['cup', 'drink']]
mlb = MultiLabelBinarizer(classes=("drink","cup","book", "read"))
mlb.fit_transform(txt)
array([[0, 0, 1, 1],
       [1, 1, 0, 0]])

答案 1 :(得分:0)

另一种方法:

您可以使用ast.literal_eval()

转换数组
arr = ["['book', 'read']","['cup', 'drink']","['book', 'read']","['book', 'read']"]

import ast 
X = [ast.literal_eval(i) for i in arr]

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer(classes=("drink","cup","book", "read"))
output = mlb.fit_transform(X)
print(output) 

输出:

[[0 0 1 1]
 [1 1 0 0]
 [0 0 1 1]
 [0 0 1 1]]