有什么办法可以将单数组转换为一种热编码吗?

Is there any way to convert Single array to one hot encoding?

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

  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

试试下面的方法:

# 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.]]
``