分类与预测
loc\iloc\ix三种索引的区别与用法
loc 在index的标签上进行索引,范围包括start和end.
iloc 在index的位置上进行索引,不包括end.
ix 先在index的标签上索引,索引不到就在index的位置上索引(如果index非全整数),不包括end.
Logistic回归
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pandas as pd
filename='E:\pycharm\python数据分析与挖掘实战数据及源码\chapter5\demo\data\bankloan.xls'
data=pd.read_excel(filename)
x=data.iloc[:,:8].as_matrix()
y=data.iloc[:,8].as_matrix()
from sklearn.linear_model import LogisticRegression as LR
from sklearn.linear_model import RandomizedLogisticRegression as RLR
rlr = RLR() #建立随机逻辑回归模型,筛选变量
rlr.fit(x, y) #训练模型
rlr.get_support() #获取特征筛选结果,也可以通过.scores_方法获取各个特征的分数
print(u'通过随机逻辑回归模型筛选特征结束。')
print(u'有效特征为:%s' % ','.join(data.columns[rlr.get_support()]))
x = data[data.columns[rlr.get_support()]].as_matrix() #筛选好特征
lr = LR() #建立逻辑货柜模型
lr.fit(x, y) #用筛选后的特征数据来训练模型
print(u'逻辑回归模型训练结束。')
print(u'模型的平均正确率为:%s' % lr.score(x, y)) #给出模型的平均正确率,本例为81.4%

本文探讨了Python数据分析中分类与预测的几个关键模型,包括Logistic回归、决策树和人工神经网络。在Logistic回归部分,提到了RLR算法已被淘汰以及DataFrame.values的使用。在决策树部分,讨论了文件路径错误和数据类型问题。最后在人工神经网络部分,介绍了cm_plot函数的安装问题以及tensorflow模块的必要性,并提到了一个TensorFlow报错的解决方案。
最低0.47元/天 解锁文章
2415

被折叠的 条评论
为什么被折叠?



