【问题标题】:Is there any way to convert Single array to one hot encoding?有没有办法将单个数组转换为一种热编码?
【发布时间】:2020-07-17 07:00:59
【问题描述】:

我已将输入字符串转换为数组,但现在我希望这个数组在一个热编码中运行在模型中。

  from sklearn.preprocessing import OneHotEncoder
  Smi = input("Enter Smile")
  inn = [Smi]
  details = np.array(inn)
  details = details.reshape(1,-1)
  encoder = OneHotEncoder(handle_unknown='ignore')
  encoder.fit(X)
  me = encoder.transform(details).toarray()
  me

  ValueError: Expected 2D array, got 1D array instead:
  

我还分享了我的 google colab,以便更好地理解。

Code done on google colab

【问题讨论】:

    标签: python-3.x machine-learning scikit-learn one-hot-encoding


    【解决方案1】:

    试试下面的方法:

    # Let fit an enocode first
    X = np.array([":)",":(",":!",":}",":|"]).reshape(-1,1)
    print (X)
    
    encoder = OneHotEncoder(handle_unknown='ignore')
    encoder.fit(X)
    
    Smi = input("Enter Smile")
    # Assuming that you typed :!
    details = np.array([Smi]).reshape(1,-1)
    print (encoder.transform(details).toarray())
    

    输出:

    [[':)']
     [':(']
     [':!']
     [':}']
     [':|']]
    Enter Smile:!
    [[1. 0. 0. 0. 0.]]
    ``
    

    【讨论】:

      猜你喜欢
      • 2020-07-12
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      • 2015-08-01
      • 2020-04-16
      • 2010-10-04
      相关资源
      最近更新 更多