我已经为多类分类执行了标签二进制化,它运行正常:
y_test
1
3
4
2
0
from sklearn.preprocessing import label_binarize
y_test_binarize = label_binarize(y_test, classes=[0, 1, 2, 3, 4])
y_test_binarize
0 1 0 0 0
0 0 0 1 0
0 0 0 0 1
0 0 1 0 0
1 0 0 0 0
接下来,我想做一个反向过程,从y_test
变量中获取y_test_binarize
。
有预定义的方法吗?
答案 0 :(得分:3)
您还可以使用LabelBinarizer,它将label_binarize函数包装在一个类中,并提供转换为二进制数据的方法,并将它们反转换为原始类。
y_test = [1, 3, 4, 2, 0]
from sklearn import preprocessing
lb = preprocessing.LabelBinarizer()
y_test_binarize = lb.fit_transform(y_test)
#Output: y_test_binarize
array([[0 1 0 0 0],
[0 0 0 1 0],
[0 0 0 0 1],
[0 0 1 0 0],
[1 0 0 0 0]])
y_test_original = lb.inverse_transform(y_test_binarize)
#Output: y_test_original
array([1, 3, 4, 2, 0])
希望这会有所帮助。随意询问是否有任何问题。
答案 1 :(得分:1)
一种简单的方法是计算二值化数据和类的矩阵乘积:
@IBAction func signUp(_ sender: AnyObject) {
//I first check to see if the users left any of the fields blank
if firstName.text == "" || lastName.text == "" || email.text == "" || userName.text == "" || password.text == "" {
createAlert(title: "Error in form", message: "Please fill in all text fields")
//If everything is filled in
}else{
let user = PFUser()
user.username = userName.text
user["firstname"] = firstName.text
user["lastname"] = lastName.text
user.email = email.text
user.password = password.text
user.signUpInBackground(block: { (success, error) in
if error != nil {
var displayErrorMessage = "Please try again later."
if let errorMessage = (error! as NSError).userInfo["error"] as? String {
displayErrorMessage = errorMessage
}
self.createAlert(title: "Signup error", message: displayErrorMessage)
}else{
print("User signed up")
}
})
}
}
答案 2 :(得分:1)
使用numpy的argmax函数:
np.argmax(y_test_binarized, axis=1)