python資料預先處理

來源:互聯網
上載者:User

標籤:length   沒有   int   ppi   pen   balance   ict   ssi   字典   

一、屬性規約

在進行資料預先處理的過程中,如果資料的某一列都是一樣的或者屬性是一樣的,那麼這一列對我們的預測沒有協助,應該將這一列去掉,pandas中如果某一列屬性值相同,但是此列中有缺失值(NaN),pandas會預設其有兩個屬性,我們在進行此操作的過程中應該首先去掉缺失值,然後檢查唯一性。代碼如下;

orig_columns = loans_2007.columnsdrop_columns = []for col in orig_columns:    col_series = loans_2007[col].dropna().unique()    if len(col_series) == 1:        drop_columns.append(col)loans_2007 = loans_2007.drop(drop_columns, axis=1)

二、缺失值

用下面的代碼進行缺失值查看資料中每一列的缺失值情況,對於缺失值比較多的列,可以直接刪除,對於缺失值比較少的列可以刪除其樣本,或者填充其他值代替。

null_counts = loans.isnull().sum()

三、字元轉化

print(loans.dtypes.value_counts())
object_columns_df = loans.select_dtypes(include=["object"]) #選擇字元型的屬性

用上面的代碼看每一列的屬性,sklearn不能處理字元型的,只能接受數值型的。對於字元型的可以進行如下處理:

mapping_dict = {    "emp_length": {        "10+ years": 10,        "9 years": 9,        "8 years": 8,        "7 years": 7,        "6 years": 6,        "5 years": 5,        "4 years": 4,        "3 years": 3,        "2 years": 2,        "1 year": 1,        "< 1 year": 0,        "n/a": 0    }}loans = loans.replace(mapping_dict)loans = loans.drop(["last_credit_pull_d", "earliest_cr_line", "addr_state", "title"], axis=1)loans["int_rate"] = loans["int_rate"].str.rstrip("%").astype("float")

 對於能枚舉的可以做一個字典,然後對資料做一個replace,對於有“%”的列,可以直接去掉百分比符號就好了。

 四、樣本不均衡問題

1、資料增強

2、加權重項

(1)(lr = LogisticRegression(class_weight="balanced"))

(2)自己設定,傳到class_weight中如下:

penalty = {    0: 5,    1: 1}lr = LogisticRegression(class_weight=penalty)

3、多個模型融合

 

python資料預先處理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.