資料分析集訓營 第二次任務(特徵工程)

2021-09-26 00:07:22 字數 2402 閱讀 7896

特徵衍生

特徵挑選:分別用iv值和隨機森林等進行特徵選擇

……以及你能想到特徵工程處理

def

calcwoe

(dataset, col, target)

:# 對特徵進行統計分組

subdata = df(dataset.groupby(col)

[col]

.count())

# 每個分組中響應客戶的數量

suby = df(dataset.groupby(col)

[target]

.sum()

)# subdata 與 suby 的拼接

data = df(pd.merge(subdata, suby, how=

'left'

, left_index=

true

, right_index=

true))

# 相關統計,總共的樣本數量total,響應客戶總數b_total,未響應客戶數量g_total

b_total = data[target]

.sum()

total = data[col]

.sum()

g_total = total - b_total

# woe公式

data[

"bad"

]= data.

(lambda x:

round

(x[target]

/b_total,

100)

, axis=1)

data[

"good"

]= data.

(lambda x:

round

((x[col]

- x[target]

)/g_total,

100)

, axis=1)

data[

"woe"

]= data.

(lambda x:log(x.bad / x.good)

, axis=1)

return data.loc[:,

["bad"

,"good"

,"woe"]]

defcalciv

(dataset)

: dataset[

"iv"

]= dataset.

(lambda x:

(x["bad"

]- x[

"good"])

* x[

"woe"

], axis=1)

iv =

sum(dataset[

"iv"])

return iv

y = data.status

x= data.drop(

'status'

, axis=1)

col_list =

[col for col in data.drop(labels=

['unnamed: 0'

,'status'

], axis=1)

]data_iv = df(

)fea_iv =

for col in col_list:

col_woe = calcwoe(data, col,

"status"

)# 刪除nan、inf、-inf

# col_woe = col_woe[~col_woe.isin([np.nan, np.inf, -np.inf]).any(1)]

col_iv = calciv(col_woe)

if col_iv >

0.1:

data_iv[col]

=[col_iv]

data_iv.to_csv(

'feature.csv'

, index=

0,encoding=

'gbk'

)print

(fea_iv)

from sklearn.ensemble import randomforestclassifier

rfc = randomforestclassifier(random_state=

2018

)rfc.fit(x, y)

importance = pd.series(rfc.feature_importances_, index=x.columns)

.sort_values(ascending=

false

)rfc_result = importance[:20

].index.tolist(

)print

(rfc_result)

後續還要進一步改進~

第二次任務

一 找到兩個有序陣列的中位數 給定兩個大小為 m 和 n 的有序陣列nums1和nums2。請你找出這兩個有序陣列的中位數,並且要求演算法的時間複雜度為 o log m n 你可以假設nums1和nums2不會同時為空。示例 1 nums1 1,3 nums2 2 則中位數是 2.0示例 2 num...

資料分析集訓營 第三次任務(模型構建)

匯入包import pandas as pd import warnings from sklearn.preprocessing import scale from sklearn.model selection import cross val score from sklearn.linear...

datawhale 資料結構第二次任務

valid parentheses 有效的括號 英文版 中文版 evaluate reverse polish notatio 逆波蘭表示式求值 英文版 中文版 佇列 design circular deque 設計乙個雙端佇列 中文版 class mycirculardeque def init ...