文章目录
引入包名
import matplotlib.pyplot as plt
import numpy as np
import os.path
from sklearn.preprocessing import Imputer
import csv
import pandas as pd
import warnings
import seaborn as sns
warnings.simplefilter("ignore")
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.tree import export_graphviz
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import pydotplus
from IPython.display import Image
from sklearn.model_selection import train_test_split
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import plotly_express as px
from sklearn import preprocessing
变量类型转化
提取object变量
cat_cols = [col for col in X.columns.values if X[col].dtype == 'O']
转换百分号变量
for i in data.columns:
try:
if data[i].dtype == 'O' and data[i][0][-1]=='%':
print(i)
data[i] = data[i].apply(lambda x:float(x[:-1]))
except:
continue
标准化
from sklearn import preprocessing
scaler = MinMaxScaler()
for i in data.columns:
if data[i].dtype != 'O':
data[i] = preprocessing.minmax_scale(data[i])
或者
x = preprocessing.scale(x)
缺失值处理
查看
missing_values_table(X)
numeric
num = X.drop(cat_cols,axis=1).fillna(X.mean())
num = X.drop(cat_cols,axis=1).fillna(X.median())
num = X.drop(cat_cols,axis=1).fillna(X.mode())
特征工程
下采样
sub_sample
def lower_sample_data(df, class_):
'''
percent:多数类别下采样的数量相对于少数类别样本数量的比例
'''
data0 = df[df['retention'] == class_] # 将少数类别的样本放在data0
data1 = df[df['retention'] != class_]
index = np.random.randint(
len(data1