生活随笔
收集整理的這篇文章主要介紹了
特征编码的一些技巧
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
buying_dict = {"low":0,"med":1,"high":2,"vhigh":3}
car_num_encoded["buying"] = car_num_encoded["buying"].map(buying_dict)
car_num_encoded["buying"].head(5)from sklearn.preprocessing import LabelEncoder
buying_encoder = LabelEncoder()
encoded_buying = buying_encoder.fit_transform(car["buying"])
buying_encoder.inverse_transform([0,1,2,3])
car_onehot_encoded = pd.DataFrame()for col in ["buying","maint","doors","persons","lug_boot","safety"]:col_encoded = pd.get_dummies(car[col],prefix = col+"_")car_onehot_encoded = pd.concat([car_onehot_encoded, col_encoded],axis=1)car_onehot_encoded.head(5)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, stratify = y, random_state=42)
總結(jié)
以上是生活随笔為你收集整理的特征编码的一些技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。