Datawhale -数据挖掘 - task 1 : 数据探索与分析

前言

要求:数据切分方式 - 三七分,其中测试集30%,训练集70%,随机种子设置为2018

任务1:对数据进行探索和分析。时间:2天

数据类型的分析
无关特征删除
数据类型转换
缺失值处理

这里面数据集大家可以换成其他的,本文章代码依旧可以适用。

1、了解数据集

import pandas as pd
import numpy as np

dataPath = "D:\\Data\\dataAnalyse\\"
dataFile = "data.csv"
data = pd.read_csv(dataPath+dataFile,encoding='gbk')

print(data.shape)
#(4754, 90)
print(data.head(10))
data.info()
# 查看数据类型及对应类型个数
print(data.dtypes.value_counts())

# 这个代码主要是为了计算每个特征的的每个值出现的次数
label = data.columns
#label = ['trade_no']
for col in label:
    print('{} 不同值个数:{}\r\n'.format(col,len(data[col].value_counts())))
    print(data[col].value_counts())

结果如下:

(4754, 90)
RangeIndex: 4754 entries, 0 to 4753
Data columns (total 90 columns):
Unnamed: 0                                    4754 non-null int64
custid                                        4754 non-null int64
trade_no                                      4754 non-null object
bank_card_no                                  4754 non-null object
low_volume_percent                            4752 non-null float64
middle_volume_percent                         4752 non-null float64
take_amount_in_later_12_month_highest         4754 non-null int64
trans_amount_increase_rate_lately             4751 non-null float64
trans_activity_month                          4752 non-null float64
trans_activity_day                            4752 non-null float64
transd_mcc                                    4752 non-null float64
trans_days_interval_filter                    4746 non-null float64
trans_days_interval                           4752 non-null float64
regional_mobility                             4752 non-null float64
student_feature                               1756 non-null float64
repayment_capability                          4754 non-null int64
is_high_user                                  4754 non-null int64
number_of_trans_from_2011                     4752 non-null float64
first_transaction_time                        4752 non-null float64
historical_trans_amount                       4754 non-null int64
historical_trans_day                          4752 non-null float64
rank_trad_1_month                             4752 non-null float64
trans_amount_3_month                          4754 non-null int64
avg_consume_less_12_valid_month               4752 non-null float64
abs                                           4754 non-null int64
top_trans_count_last_1_month                  4752 non-null float64
avg_price_last_12_month                       4754 non-null int64
avg_price_top_last_12_valid_month             4650 non-null float64
reg_preference_for_trad                       4752 non-null object
trans_top_time_last_1_month                   4746 non-null float64
trans_top_time_last_6_month                   4746 non-null float64
consume_top_time_last_1_month                 4746 non-null float64
consume_top_time_last_6_month                 4746 non-null float64
cross_consume_count_last_1_month              4328 non-null float64
trans_fail_top_count_enum_last_1_month        4738 non-null float64
trans_fail_top_count_enum_last_6_month        4738 non-null float64
trans_fail_top_count_enum_last_12_month       4738 non-null float64
consume_mini_time_last_1_month                4728 non-null float64
max_cumulative_consume_later_1_month          4754 non-null int64
max_consume_count_later_6_month               4746 non-null float64
railway_consume_count_last_12_month           4742 non-null float64
pawns_auctions_trusts_consume_last_1_month    4754 non-null int64
pawns_auctions_trusts_consume_last_6_month    4754 non-null int64
jewelry_consume_count_last_6_month            4742 non-null float64
status                                        4754 non-null int64
source                                        4754 non-null object
first_transaction_day                         4752 non-null float64
trans_day_last_12_month                       4752 non-null float64
id_name                                       4478 non-null object
apply_score                                   4450 non-null float64
apply_credibility                             4450 non-null float64
query_org_count                               4450 non-null float64
query_finance_count                           4450 non-null float64
query_cash_count                              4450 non-null float64
query_sum_count                               4450 non-null float64
latest_query_time                             4450 non-null object
latest_one_month_apply                        4450 non-null float64
latest_three_month_apply                      4450 non-null float64
latest_six_month_apply                        4450 non-null float64
loans_score                                   4457 non-null float64
loans_credibility_behavior                    4457 non-null float64
loans_count                                   4457 non-null float64
loans_settle_count                            4457 non-null float64
loans_overdue_count                           4457 non-null float64
loans_org_count_behavior                      4457 non-null float64
consfin_org_count_behavior                    4457 non-null float64
loans_cash_count                              4457 non-null float64
latest_one_month_loan                         4457 non-null float64
latest_three_month_loan                       4457 non-null float64
latest_six_month_loan                         4457 non-null float64
history_suc_fee                               4457 non-null float64
history_fail_fee                              4457 non-null float64
latest_one_month_suc                          4457 non-null float64
latest_one_month_fail                         4457 non-null float64
loans_long_time                               4457 non-null float64
loans_latest_time                             4457 non-null object
loans_credit_limit                            4457 non-null float64
loans_credibility_limit                       4457 non-null float64
loans_org_count_current                       4457 non-null float64
loans_product_count                           4457 non-null float64
loans_max_limit                               4457 non-null float64
loans_avg_limit                               4457 non-null float64
consfin_credit_limit                          4457 non-null float64
consfin_credibility                           4457 non-null float64
consfin_org_count_current                     4457 non-null float64
consfin_product_count                         4457 non-null float64
consfin_max_limit                             4457 non-null float64
consfin_avg_limit                             4457 non-null float64
latest_query_day                              4450 non-null float64
loans_latest_day                              4457 non-null float64
dtypes: float64(70), int64(13), object(7)
memory usage: 3.3+ MB
float64    70
int64      13
object      7
dtype: int64


Unnamed: 0 不同值个数:4754

custid 不同值个数:4754

trade_no 不同值个数:4754

bank_card_no 不同值个数:1

low_volume_percent 不同值个数:40

middle_volume_percent 不同值个数:90

take_amount_in_later_12_month_highest 不同值个数:166

trans_amount_increase_rate_lately 不同值个数:782

trans_activity_month 不同值个数:84

trans_activity_day 不同值个数:512

transd_mcc 不同值个数:41

trans_days_interval_filter 不同值个数:147

trans_days_interval 不同值个数:114

regional_mobility 不同值个数:5

student_feature 不同值个数:2

repayment_capability 不同值个数:2390

is_high_user 不同值个数:2

number_of_trans_from_2011 不同值个数:70

first_transaction_time 不同值个数:1693

historical_trans_amount 不同值个数:4524

historical_trans_day 不同值个数:476

rank_trad_1_month 不同值个数:20

trans_amount_3_month 不同值个数:3524

avg_consume_less_12_valid_month 不同值个数:12

abs 不同值个数:1697

top_trans_count_last_1_month 不同值个数:8

avg_price_last_12_month 不同值个数:330

avg_price_top_last_12_valid_month 不同值个数:20

reg_preference_for_trad 不同值个数:5

trans_top_time_last_1_month 不同值个数:28

trans_top_time_last_6_month 不同值个数:97

consume_top_time_last_1_month 不同值个数:28

consume_top_time_last_6_month 不同值个数:94

cross_consume_count_last_1_month 不同值个数:19

trans_fail_top_count_enum_last_1_month 不同值个数:15

trans_fail_top_count_enum_last_6_month 不同值个数:25

trans_fail_top_count_enum_last_12_month 不同值个数:26

consume_mini_time_last_1_month 不同值个数:1971

max_cumulative_consume_later_1_month 不同值个数:863

max_consume_count_later_6_month 不同值个数:29

railway_consume_count_last_12_month 不同值个数:6

pawns_auctions_trusts_consume_last_1_month 不同值个数:572

pawns_auctions_trusts_consume_last_6_month 不同值个数:2730

jewelry_consume_count_last_6_month 不同值个数:7

status 不同值个数:2

source 不同值个数:1

first_transaction_day 不同值个数:1693

trans_day_last_12_month 不同值个数:132

id_name 不同值个数:4309

apply_score 不同值个数:205

apply_credibility 不同值个数:41

query_org_count 不同值个数:46

query_finance_count 不同值个数:25

query_cash_count 不同值个数:17

query_sum_count 不同值个数:74

latest_query_time 不同值个数:207

latest_one_month_apply 不同值个数:36

latest_three_month_apply 不同值个数:56

latest_six_month_apply 不同值个数:65

loans_score 不同值个数:247

loans_credibility_behavior 不同值个数:25

loans_count 不同值个数:134

loans_settle_count 不同值个数:123

loans_overdue_count 不同值个数:26

loans_org_count_behavior 不同值个数:41

consfin_org_count_behavior 不同值个数:19

loans_cash_count 不同值个数:32

latest_one_month_loan 不同值个数:14

latest_three_month_loan 不同值个数:31

latest_six_month_loan 不同值个数:67

history_suc_fee 不同值个数:171

history_fail_fee 不同值个数:151

latest_one_month_suc 不同值个数:19

latest_one_month_fail 不同值个数:41

loans_long_time 不同值个数:202

loans_latest_time 不同值个数:232

loans_credit_limit 不同值个数:54

loans_credibility_limit 不同值个数:33

loans_org_count_current 不同值个数:32

loans_product_count 不同值个数:32

loans_max_limit 不同值个数:91

loans_avg_limit 不同值个数:961

consfin_credit_limit 不同值个数:327

consfin_credibility 不同值个数:24

consfin_org_count_current 不同值个数:19

consfin_product_count 不同值个数:20

consfin_max_limit 不同值个数:175

consfin_avg_limit 不同值个数:1677

latest_query_day 不同值个数:210

loans_latest_day 不同值个数:235


将城市 中文换成数字


reg_preference_for_trad = 'reg_preference_for_trad'
city_label = []
# 把 nan转换为 ""
for city in data[reg_preference_for_trad].fillna(""): 
    if city not in city_label:
        city_label.append(city)

print(city_label)
#print(data[reg_preference_for_trad].head(10))

city_dic = {'一线城市':1, '二线城市':2, '三线城市':3, '境外':4, '其他城市':5, '':5}
city_num =[]
for city in data[reg_preference_for_trad].fillna(""): 
    city_num.append(city_dic[city])

print(city_num[0:20])

# 新增一列
reg_preference_for_trad_num = reg_preference_for_trad+"_num"
data[reg_preference_for_trad_num] = city_num

print(data[reg_preference_for_trad_num].head(10))
print(data[reg_preference_for_trad_num].value_counts())

结果如下:

['一线城市', '三线城市', '境外', '二线城市', '其他城市', '']
[1, 1, 1, 3, 1, 3, 1, 1, 3, 1, 1, 1, 3, 1, 1, 1, 4, 1, 4, 1]
0    1
1    1
2    1
3    3
4    1
5    3
6    1
7    1
8    3
9    1
Name: reg_preference_for_trad_num, dtype: int64
1    3403
3    1064
4     150
2     131
5       6
Name: reg_preference_for_trad_num, dtype: int64

其中吧缺省值全部设置为了“”空字符,一方面是因为确实不好判断这个nan,所以转换后就很方便来判断缺省值,另一个方面就是利用词典来对数据数据,获得的新的一列,然后把新的一列插入到原数据结构中。

3、删除列

# 删除列
data = data.drop(['bank_card_no'],axis=1)

data = data.drop(['Unnamed: 0'],axis=1)

print('形状:',data.shape) 

结果:

形状:(4754, 89)

删除这两个,第一个是因为数值都是一样的,第二个是因为没有列名,应该是脏数据。

4、切割数据

from sklearn.model_selection import train_test_split

train_data, test_data = train_test_split(data, test_size=0.3, random_state=2018)
print("train_data",train_data.shape)
print("test_data",test_data.shape)

结果如下:

train_data (3327, 91)
test_data (1427, 91)

其中:
test_size:样本占比,如果是整数的话就是样本的数量
random_state:是随机数的种子。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值