利用pandas將非數(shù)值數(shù)據(jù)轉(zhuǎn)換成數(shù)值的方式
handle non numerical data
舉個(gè)例子,將性別屬性男女轉(zhuǎn)換成0-1,精通ML的小老弟們可以略過本文~~,
這里不考慮稀疏向量的使用,僅提供一些思路。本來想直接利用pandas的DataFrame.iloc加上for循環(huán)直接轉(zhuǎn)換,但試過一遍之后,原數(shù)據(jù)并有改變。。。。蛋疼寫了一個(gè)比較 菜的函數(shù),如下。
# 非數(shù)值列處理函數(shù)
def handel_non_numerical_data(df,name): #----------------name是需要處理的列名稱(str),暫不考慮列表
nrows = len(df[name]) #----------------數(shù)據(jù)集的行數(shù)
old_col = df.columns.tolist() #----------------初始的列名集合
name_index = old_col.index(name) #---------要處理的列的在數(shù)據(jù)集中的索引值
name_data = df[name].values.tolist()#-----------將要處理煩人列復(fù)制成一個(gè)列表
df.drop([name],axis =1,inplace =True)
unique_kinds = set(name_data)
convert_dict = {}; x = 0 #構(gòu)造對應(yīng)種類數(shù)值轉(zhuǎn)化字典
for i in unique_kinds:
convert_dict[i] = x
x += 1
def convert(val):
return convert_dict[val]
name_data = list(map(convert,name_data))#利用map函數(shù)直接迭代轉(zhuǎn)化
new_col = df.columns.tolist()
new_col.insert(name_index,name)
df.reindex(columns = new_col) #----------------重構(gòu)數(shù)據(jù)的列
df[name] = name_data
跑了一遍沒有出錯(cuò),注意這只是baseline…,如果對數(shù)值有要求的話,需要自行改動(dòng)
原本是想直接用youtube上sentdex老哥ml35期視頻里的代碼的,但發(fā)現(xiàn)了幾個(gè)較為嚴(yán)重的bug,而且總是運(yùn)行出錯(cuò) ,如下
def handle_non_numerical_data(df):
columns = df.columns.values
for column in columns:
text_digit_vals = {}
def convert_to_int(val):
return text_digit_vals[val]
if df[column].dtype != np.int64 and df[column].dtype != np.float64:
column_content = df[column].values.tolist()
unique_elements = set(column_content)
print(unique_elements)
x =0
for unique in unique_elements:
if unique not in text_digit_vals:
text_digit_vals[unique] = x
x+=1
df[column] = list(map(convert_to_int,df[column]))
可見,非常暴力,注意到他的if條件,有的數(shù)據(jù)集中會(huì)出現(xiàn)字母數(shù)字組合的情況【會(huì)出現(xiàn)dtype=object的情況】,set之后種類會(huì)草雞多…,這樣的話數(shù)值轉(zhuǎn)換也就失去了意義【當(dāng)然,如果你的樣本量是億級的,幾千幾百個(gè)種類無所謂我也無fuck說,這種情況我認(rèn)為必須使用稀疏向量了】,另外這個(gè)代碼一直報(bào)錯(cuò),不知道為什么,有興趣的老哥可以復(fù)制跑一下幫我解答一下。。。
---------------------------2019-08-21分割:
https://www.kaggle.com/nroman/recursive-feature-elimination
LabelEncoder方法
from sklearn.preprocessing import LabelEncoder

注:tqdm是進(jìn)度條庫,不需要關(guān)注。另外沒有去看這個(gè)接口的源碼,應(yīng)該也是最簡單的one-hot
以上這篇利用pandas將非數(shù)值數(shù)據(jù)轉(zhuǎn)換成數(shù)值的方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何用python?GUI(tkinter)寫一個(gè)鬧鈴小程序(思路詳解)
這篇文章主要介紹了用python?GUI(tkinter)寫一個(gè)鬧鈴小程序思路詳解,涉及到tkinter一些函數(shù)控件,數(shù)據(jù)的類的封裝,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
Linux下使用python自動(dòng)修改本機(jī)網(wǎng)關(guān)代碼分享
這篇文章主要介紹了Linux下使用python自動(dòng)修改本機(jī)網(wǎng)關(guān)代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
關(guān)于pycharm中pip版本10.0無法使用的解決辦法
近期在利用 pycharm 安裝第三方庫時(shí)會(huì)提示 pip 不是最新版本, 因此對 pip 進(jìn)行更新,但是生成最新版本之后, pip 中由于缺少 main 函數(shù),導(dǎo)致在 pycharm 中無法自動(dòng)安裝第三方庫。本文就介紹一下如何解決2019-10-10
Python報(bào)錯(cuò)ModuleNotFoundError: No module named&
在嘗試導(dǎo)入TensorBoard模塊時(shí),你可能會(huì)遇到ModuleNotFoundError: No module named 'tensorboard'的錯(cuò)誤,下面我們來分析這個(gè)問題并提供解決方案,需要的朋友可以參考下2024-09-09
通過python實(shí)現(xiàn)Google的精準(zhǔn)搜索功能
這篇文章主要介紹了通過python實(shí)現(xiàn)Google的精準(zhǔn)搜索功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-05-05

