Pyhton数据挖掘-电力窃漏电用户的自动识别

概述

本来主要是对博主在Python数据分析与挖掘实战的上第六章实践过程中所出现问题的总结,看本文的之前最好是看过这本书的第六章。

问题一:采用anaconda环境下安装tensorFlow后,pycharm识别不了TensorFlow。

情况是这样的,博主在首先安装好tensorFlow后,再安装keras成功后,pycharm识别不了TensorFlow。
然后设置python解析器路径,如图:
这里写图片描述
有好几个路径,之前不管是设置envs下面的TensorFlow下面的python.exe,还是anaconda下面的,都会出错,其中设置TensorFlow下面的时候,TensorFlow是正常的,但是运行代码会出现找不到anaconda下面的包,比如pandas,keras等,设置anaconda后,又会找不到TensorFlow下面的包ModuleNotFoundError: No module named 'tensorflow'。两者不能共存,之后再这篇博客下找到了答案,http://blog.csdn.net/zuochao_2013/article/details/72453416,非常感谢博主的帮助,其中有非常大的一个坑,现在anaconda官网的64位版本是4.4.0,千万不要安装这个版本,要安装64位4.2.0版本。安装TensorFlow也推荐那个博客链接。

问题二:使用书本上的代码跑不起来。

书上代码如下

#-*- coding: utf-8 -*-

import pandas as pd
from random import shuffle

datafile = '../data/model.xls'
data = pd.read_excel(datafile)
data = data.as_matrix()
shuffle(data)

p = 0.8 #设置训练数据比例
train = data[:int(len(data)*p),:]
test = data[int(len(data)*p):,:]

#构建LM神经网络模型
from keras.models import Sequential #导入神经网络初始化函数
from keras.layers.core import Dense, Activation #导入神经网络层函数、激活函数

netfile = '../tmp/net.model' #构建的神经网络模型存储路径

net = Sequential() #建立神经网络
net.add(Dense(units = 10,input_dim = 3)) #添加输入层(3节点)到隐藏层(10节点)的连接
net.add(Activation('relu')) #隐藏层使用relu激活函数
net.add(Dense(units = 1)) #添加隐藏层(10节点)到输出层(1节点)的连接
net.add(Activation('sigmoid')) #输出层使用sigmoid激活函数
net.compile(loss = 'binary_crossentropy', optimizer = 'adam', class_mode = "binary") #编译模型,使用adam方法求解 此方法已经废掉了。要改成如下方法。
# net.compile(loss='mse', optimizer='sgd', metrics=['accuracy'])

net.fit(train[:,:3], train[:,3], epochs=1000, batch_size=1) #训练模型,循环1000次
net.save_weights(netfile) #保存模型

predict_result = net.predict_classes(train[:,:3]).reshape(len(train)) #预测结果变形
#'''这里要提醒的是,keras用predict给出预测概率,predict_classes才是给出预测类别,而且两者的预测结果都是n x 1维数组,而不是通常的 1 x n'''

from code.cm_plot import * #导入自行编写的混淆矩阵可视化函数
cm_plot(train[:,3], predict_result).show() #显示混淆矩阵可视化结果

from sklearn.metrics import roc_curve #导入ROC曲线函数
import matplotlib.pyplot as plt

predict_result = net.predict(test[:,:3]).reshape(len(test))
fpr, tpr, thresholds = roc_curve(test[:,3], predict_result, pos_label=1)
plt.plot(fpr, tpr, linewidth=2, label = 'ROC of LM') #作出ROC曲线
plt.xlabel('False Positive Rate') #坐标轴标签
plt.ylabel('True Positive Rate') #坐标轴标签
plt.ylim(0,1.05) #边界范围
plt.xlim(0,1.05) #边界范围
plt.legend(loc=4) #图例
plt.show() #显示作图结果

这时会报错

Traceback (most recent call last):
  File "C:/Users/黄海峰/PycharmProjects/anaconda/code/6-3_lm_model.py", line 29, in <module>
    net.fit(train[:,:3], train[:,3], epochs=1000, batch_size=1) #训练模型,循环1000次
  File "D:\Program Files\Anaconda3\lib\site-packages\keras\models.py", line 863, in fit
    initial_epoch=initial_epoch)
  File "D:\Program Files\Anaconda3\lib\site-packages\keras\engine\training.py", line 1430, in fit
    initial_epoch=initial_epoch)
  File "D:\Program Files\Anaconda3\lib\site-packages\keras\engine\training.py", line 1079, in _fit_loop
    outs = f(ins_batch)
  File "D:\Program Files\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2268, in __call__
    **self.session_kwargs)
TypeError: run() got an unexpected keyword argument 'class_mode'

这个可以查看keras的文档解决http://keras-cn.readthedocs.io/en/latest/other/objectives/,发现最新版本的keras2.0.2class_mode已经没了,取而代之的是metrics=[‘accuracy’],
还会报错

You are passing a target array of shape (4231, 1) while using as loss `categorical_crossentropy`. `categorical_crossentropy` expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:

from keras.utils.np_utils import to_categorical
y_binary = to_categorical(y_int)

Alternatively, you can use the loss function 
`sparse_categorical_crossentropy` instead, which does expect integer 
targets.

最后经过查文档改代码,终于可以运行了。这里写图片描述
这里写图片描述
这里写图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值