数据下载:creditcard.csv
1.数据集
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_csv('creditcard.csv')
print(data.head(n=5))
countClasses = pd.value_counts(data['Class'],sort=True).sort_index() #计算class列中不同属性的数据的个数
countClasses.plot(kind='bar') #简单的pandas也可以画图
plt.show()
2.发现Amount这一列数值比较大,在机器学习中特征的重要性偏爱数值比较大的特征,解决办法可以标准化
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler #sklearn数据预处理模块
data = pd.read_csv('creditcard.csv')
dataMatrix = np.mat(data) #将数据变换成矩阵形式
X = dataMatrix[:,:-1] #数据集
y = dataMatrix[:,-1] #标签
scaler = StandardScaler().fit(X) #StandardScaler()这个小括号一定不能忘记,#标准化:(x-mean)/std
dataStand = scaler.transform(X)
print(dataStand[:5,:])
3.发现样本分布非常不均衡
解决方案:下采样(将多的样本随机选取和少样本一样的数目),过采样(将少样本数目增加到多样本数目)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler #sklearn数据预处理模块
data = pd.read_csv('creditcard.csv')
dataMatrix = np.mat(data)
X = dataMatrix[:,:-1]
y = dataMatrix[:,-1]
#print(len(y)) #284807
scaler = StandardScaler().fit(X) #标准化ÿ