全栈工程师开发手册 (作者:栾鹏)
python数据挖掘系列教程

线性回归

import numpy as npfrom keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt# 样本数据集,第一列为x,第二列为y,在x和y之间建立回归模型
data=[[0.067732,3.176513],[0.427810,3.816464],[0.995731,4.550095],[0.738336,4.256571],[0.981083,4.560815],[0.526171,3.929515],[0.378887,3.526170],[0.033859,3.156393],[0.132791,3.110301],[0.138306,3.149813],[0.247809,3.476346],[0.648270,4.119688],[0.731209,4.282233],[0.236833,3.486582],[0.969788,4.655492],[0.607492,3.965162],[0.358622,3.514900],[0.147846,3.125947],[0.637820,4.094115],[0.230372,3.476039],[0.070237,3.210610],[0.067154,3.190612],[0.925577,4.631504],[0.717733,4.295890],[0.015371,3.085028],[0.335070,3.448080],[0.040486,3.167440],[0.212575,3.364266],[0.617218,3.993482],[0.541196,3.891471]
]
#生成X和y矩阵
dataMat = np.array(data)
X = dataMat[:,0:1]   # 变量x
y = dataMat[:,1]   #变量y# 构建神经网络模型
model = Sequential()
model.add(Dense(input_dim=1, units=1))# 选定loss函数和优化器
model.compile(loss='mse', optimizer='sgd')# 训练过程
print('Training -----------')
for step in range(501):cost = model.train_on_batch(X, y)if step % 50 == 0:print("After %d trainings, the cost: %f" % (step, cost))# 测试过程
print('\nTesting ------------')
cost = model.evaluate(X, y, batch_size=40)
print('test cost:', cost)
W, b = model.layers[0].get_weights()
print('Weights=', W, '\nbiases=', b)# 将训练结果绘出
Y_pred = model.predict(X)
plt.scatter(X, y)
plt.plot(X, Y_pred)
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6WzZRTCL-1606145353083)(//img-blog.csdn.net/20180429081624774?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2x1YW5wZW5nODI1NDg1Njk3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

二分类逻辑回归

import numpy as npfrom keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
import matplotlib.pyplot as plt
from sklearn import datasets# 样本数据集,两个特征列,两个分类二分类不需要onehot编码,直接将类别转换为0和1,分别代表正样本的概率。
X,y=datasets.make_classification(n_samples=200, n_features=2, n_informative=2, n_redundant=0,n_repeated=0, n_classes=2, n_clusters_per_class=1)# 构建神经网络模型
model = Sequential()
model.add(Dense(input_dim=2, units=1))
model.add(Activation('sigmoid'))# 选定loss函数和优化器
model.compile(loss='binary_crossentropy', optimizer='sgd')# 训练过程
print('Training -----------')
for step in range(501):cost = model.train_on_batch(X, y)if step % 50 == 0:print("After %d trainings, the cost: %f" % (step, cost))# 测试过程
print('\nTesting ------------')
cost = model.evaluate(X, y, batch_size=40)
print('test cost:', cost)
W, b = model.layers[0].get_weights()
print('Weights=', W, '\nbiases=', b)# 将训练结果绘出
Y_pred = model.predict(X)
Y_pred = (Y_pred*2).astype('int')  # 将概率转化为类标号,概率在0-0.5时,转为0,概率在0.5-1时转为1
# 绘制散点图 参数:x横轴 y纵轴
plt.subplot(2,1,1).scatter(X[:,0], X[:,1], c=Y_pred)
plt.subplot(2,1,2).scatter(X[:,0], X[:,1], c=y)
plt.show()

多分类逻辑回归

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
import matplotlib.pyplot as plt
from keras.utils import np_utils
from sklearn import datasets# 样本数据集,两个特征列,两个分类二分类不需要onehot编码,直接将类别转换为0和1,分别代表正样本的概率。
X,y=datasets.make_classification(n_samples=200, n_features=2, n_informative=2, n_redundant=0,n_repeated=0, n_classes=3, n_clusters_per_class=1)
n_class=3# 转换为one_hot类型
y = np_utils.to_categorical(y, n_class) # 将2分类类标号转化为one-hot编码# 构建神经网络模型
model = Sequential()
model.add(Dense(input_dim=2, units=n_class))
model.add(Activation('softmax'))# 选定loss函数和优化器
model.compile(loss='categorical_crossentropy', optimizer='sgd')# 训练过程
print('Training -----------')
for step in range(501):cost = model.train_on_batch(X, y)if step % 50 == 0:print("After %d trainings, the cost: %f" % (step, cost))# 测试过程
print('\nTesting ------------')
cost = model.evaluate(X, y, batch_size=40)
print('test cost:', cost)
W, b = model.layers[0].get_weights()
print('Weights=', W, '\nbiases=', b)# 将训练结果绘出
Y_pred = model.predict(X)
Y_pred = Y_pred.argmax(axis=1)   # 获取概率最大的分类,获取每行最大值所在的列
print('分类结果:\n',Y_pred)
# 绘制散点图 参数:x横轴 y纵轴
plt.subplot(2,1,1).scatter(X[:,0], X[:,1], c=Y_pred)
plt.subplot(2,1,2).scatter(X[:,0], X[:,1], c=y)
plt.show()

python机器学习库keras——线性回归、逻辑回归、一般逻辑回归相关推荐

  1. python机器学习库sklearn——线性回归

    全栈工程师开发手册 (作者:栾鹏) python数据挖掘系列教程 线性回归的相关的知识内容可以参考 http://blog.csdn.net/luanpeng825485697/article/det ...

  2. python机器学习库keras——CNN卷积神经网络人脸识别

    全栈工程师开发手册 (作者:栾鹏) python教程全解 github地址:https://github.com/626626cdllp/kears/tree/master/Face_Recognit ...

  3. python机器学习库keras——AutoEncoder自编码、特征压缩

    全栈工程师开发手册 (作者:栾鹏) python教程全解 keras使用深度网络实现自编码,也就是说对每个样本的n维特征,使用k为特征来表示,实现编码压缩的功能.也实现了特征选择的功能.比如手写体包含 ...

  4. python机器学习库keras——CNN卷积神经网络识别手写体

    分享一个朋友的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!大家可以看看是否对自己有帮助:点击打开 全栈工程师开发手册 (作者:栾鹏) python教程全解 keras使用CNN识别手写体 其 ...

  5. 【Python学习系列十三】Python机器学习库scikit-learn实现逻辑回归

    逻辑回归基础知识可参考:http://blog.csdn.net/fjssharpsword/article/details/54580552 python内部算法已经实现了,最重要是理解y=f(x) ...

  6. Python 机器学习库 Top 10,你值得拥有!

    随着人工智能技术的发展与普及,Python 超越了许多其他编程语言,成为了机器学习领域中最热门最常用的编程语言之一.有许多原因致使 Python 在众多开发者中如此受追捧,其中之一便是其拥有大量的与机 ...

  7. python机器学习库_Python机器学习库 Top 10,你值得拥有!

    随着人工智能技术的发展与普及,Python超越了许多其他编程语言,成为了机器学习领域中最热门最常用的编程语言之一.有许多原因致使Python在众多开发者中如此受追捧,其中之一便是其拥有大量的与机器学习 ...

  8. [转载] Python机器学习库scikit-learn使用小结(二)

    参考链接: Scikit-learn中的模型构建:Python机器学习库 scikit-learn库(后三小结) 在做数据分析和挖掘的过程中,数据的处理(标准化).划分.快速建模都是必不可少的方式.这 ...

  9. 【转】Python机器学习库

    2019独角兽企业重金招聘Python工程师标准>>> Python 在科学计算领域,有两个重要的扩展模块:Numpy和Scipy.其中Numpy是一个用python实现的科学计算包 ...

最新文章

  1. Merge、Rebase
  2. SAP的程序用客户端连接正常,用C#连接死活连不上问题的解决
  3. function——函数声明头的提升和预解析
  4. keyshot10 pro for mac最新版
  5. 死锁发生的条件和预防
  6. 人人都能有数字替身:量子动力FACEGOOD发布AI交互实时数字人
  7. zookeeper基本原理
  8. [转]oracle分页用两层循环还是三层循环?
  9. svn sync主从同步学习
  10. html文本框虚线并加上文字,文字边框虚线样式用css怎么写?(示例)
  11. Android仿人人客户端(v5.7.1)——新鲜事之完整篇
  12. GoogleStyle编程代码规范
  13. pdf照片显示正常打印时被翻转_2020年上海二级建造师准考证打印常见问题
  14. Result的类型分析和总结
  15. Linux 30岁啦,这些历史你知道多少呢?
  16. 关于微信公众号开发【微信JS-SDK】报错:config invalid url domian
  17. 装饰模式之半透明装饰模式
  18. 基因测序与高通量测序区别
  19. GlobalProtect bupt for mac
  20. 智能控制(第4版)习题

热门文章

  1. 智能机器人语音识别技术
  2. ICRoute 语音识别芯片/声控芯片 用声音去沟通 LD332X系列语音识别芯片
  3. 6.Java反射到底慢在哪
  4. 30轧制过程的计算机控制系统,中厚板轧制过程计算机控制系统结构的研制(1)
  5. Gulp模块报错:Did you forget to signal async completion? 处理
  6. 蓝桥杯笔记:DFS(深度优先搜索)解决问题
  7. wsl2启动桌面_「原创」windows10下自定义WSL安装路径及启动WSL2
  8. c# 火狐浏览器怎么嵌入窗体中_「C#上位机必看」你们想要的练手项目来了
  9. 用matlab进行边缘检测,利用MATLAB进行数字图像的边缘检测
  10. Linux下安装和卸载jdk及环境配置