流程

1、数据提取

import pandas

import pymysql

db = pymydql.connet(host='192.168.10.136',port=3316,user='root',passwd='1234',db='库名',charset='gbk')

mysql = 'select * from student'

data = pd.read_sql(sql,db,index_col='')

2、查看缺失值

check_null = data.isnull().sum(axis=0).sort_values(ascending=False)/float(len(data))

3、分类统计数据类型

type = data.dtypes.value_counts()

4、剔除分类变量只有1的变量

data = data.loc([:,data.apply(pd.Series.nunique)!=1]

5、缺失值处理

#插值法填取缺失值
from sklearn.preprocessing import Imputer
Imputerimr = Imputer(missing_values='NaN',strategy='mean',axis = 0)
loansdata[numcolumns]=Imputerimr.fit_transform(loansdata[numcolumns])

6、数据过滤

7、特征工程

a.特征衍生

b.特征抽象

from sklearn.processing import LableEnode

from skleaen.processing import OneHot

c.特征缩放

from sklearn.processing import StandardScaler

d.特征选择

  1. 过滤方法(filter approach): 通过自变量之间或自变量与目标变量之间的关联关系选择特征。

from sklearn.feature_selection import RFE

from sklearn.linear_model import LogisticRegression

# 建立逻辑回归分类器

model = LogisticRegression()

# 建立递归特征消除筛选器

rfe = RFE(model, 30) #通过递归选择特征,选择30个特征

rfe = rfe.fit(x_val, y_val)

# 打印筛选结果

print(rfe.support_)

print(rfe.ranking_) #ranking 为 1代表被选中,其他则未被代表未被选中

col_filter = x_val.columns[rfe.support_] #通过布尔值筛选首次降维后的变量

col_filter # 查看通过递归特征消除法筛选的变量

  1. 嵌入方法(embedded approach): 通过学习器自身自动选择特征。(通过皮尔森相关性图谱找出冗余特征并将其剔除)
  1. colormap = plt.cm.viridis
  2. plt.figure(figsize=(12,12))
  3. plt.title('Pearson Correlation of Features', y=1.05, size=15)
  4. sns.heatmap(loans_ml_df[col_filter].corr(),linewidths=0.1,vmax=1.0, square=True, cmap=colormap, linecolor='white', annot=True)
  5. col_new = col_filter.drop(drop_col) #剔除冗余特征
  1. 包装方法(wrapper approacch): 通过目标函数(AUC/MSE)来决定是否加入一个变量。
  1. names = loans_ml_df[col_new].columns
  2. from sklearn.ensemble import RandomForestClassifier
  3. clf=RandomForestClassifier(n_estimators=10,random_state=123)#构建分类随机森林分类器
  4. clf.fit(x_val[col_new], y_val) #对自变量和因变量进行拟合
  5. names, clf.feature_importances_
  6. for feature in zip(names, clf.feature_importances_):
  7. print(feature)

8、模型训练

a.处理不平衡数据

(1)欠采样:去除一些负样本使得正、负样本数目接近,然后再进行学习。

(2)过采样:增加正样本使得正、负样本数目接近,然后再进行学习。

SMOET的基本原理是:采样最邻近算法,计算出每个少数类样本的K个近邻,从K个近邻中随机挑选N个样本进行随机线性插值,构造新的少数样本,同时将新样本与原数据合成,产生新的训练集。

  1. from imblearn.over_sampling import SMOTE # 导入SMOTE算法模块
  2. # 处理不平衡数据
  3. sm = SMOTE(random_state=42) # 处理过采样的方法
  4. X, y = sm.fit_sample(X, y)
  5. print('通过SMOTE方法平衡正负样本后')
  6. n_sample = y.shape[0]
  7. n_pos_sample = y[y == 0].shape[0]
  8. n_neg_sample = y[y == 1].shape[0]
  9. print('样本个数:{}; 正样本占{:.2%}; 负样本占{:.2%}'.format(n_sample,
  10. n_pos_sample / n_sample,
  11. n_neg_sample / n_sample))

b.构建分类器进行训练

9、模型评估

a.查看预测结果的准确率

  1. from sklearn.metrics import accuracy_score
  2. print("Test set accuracy score: {:.5f}".format(accuracy_score(predicted1, y,)))

b.计算precision、recall、f1-score的值

  1. from sklearn.metrics import classification_report
  2. print(classification_report(y, predicted1))

 

  1. from sklearn.metrics import roc_auc_score
  2. roc_auc1 = roc_auc_score(y, predicted1)
  3. print("Area under the ROC curve : %f" % roc_auc1)

10、模型优化

将数据集划分为训练集和测试集有3种处理方法:
1、留出法(hold-out)
2、交叉验证法(cross-validation)
3、自助法(bootstrapping)

让模型在训练集进行学习,在验证集上进行参数调优,最后使用测试集数据评估模型的性能。

模型调优我们采用网格搜索调优参数(grid search),通过构建参数候选集合,然后网格搜索会穷举各种参数组合,根据设定评定的评分机制找到最好的那一组设置

  1. from sklearn.model_selection import GridSearchCV
  2. from sklearn.cross_validation import train_test_split
  3.  
  4. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0) # random_state = 0 每次切分的数据都一样
  5. # 构建参数组合
  6. param_grid = {'C': [0.01,0.1, 1, 10, 100, 1000,],
  7. 'penalty': [ 'l1', 'l2']}
  8.  
  9. grid_search = GridSearchCV(LogisticRegression(), param_grid, cv=10) # 确定模型LogisticRegression,和参数组合param_grid ,cv指定5折
  10. grid_search.fit(X_train, y_train) # 使用训练集学习算法
  11. results = pd.DataFrame(grid_search.cv_results_)
    print(results)
    print(results.columns)
    best = np.argmax(results.mean_test_score.values)
    print(best)

模型性能评估

  1. print("Best parameters: {}".format(grid_search.best_params_))
  2. print("Best cross-validation score: {:.5f}".format(grid_search.best_score_))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值