当前位置: 首页 > news >正文

python与人工智能代码基础

使用线性回归模型训练、测试和预测数据

make_regression() 实际上相当于做了这样的事:
真实斜率 = 随机生成(比如 7.8),真实截距 = 随机生成(比如 1.2)

for 每个x值:
理想y值 = 7.8 * x + 1.2
实际y值 = 理想y值 + 随机噪声(范围±6)

import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression import joblib from sklearn import datasets from sklearn.model_selection import train_test_split #1.生成数据集 x,y = datasets.make_regression(n_samples=200,n_features=1,n_targets=1,noise=6) #生成数据点以散点图显示 print("1.生成数据的散点图(左),训练数据和预测数据拟合线(右)") plt.scatter(x,y) plt.xticks(fontsize=16) plt.yticks(fontsize=16) plt.xlabel('x轴-x',fontproperties="SimHei",fontsize=18) plt.ylabel('y轴-y',fontproperties="SimHei",fontsize=18) plt.show() #2.拆分数据集,70%训练,30%测试 train_x,test_x,train_y,test_y = train_test_split(x,y,test_size=0.3,random_state=0) #3.训练模型 linearmodel = LinearRegression() linearmodel.fit(train_x,train_y) fitted_y = linearmodel.predict(train_x) #图形显示训练和预测数据 plt.plot(train_x,train_y,'bo') plt.plot(train_x,fitted_y,'r',linewidth=4.0) plt.xlabel('x轴-train_x',fontproperties="SimHei",fontsize=18) plt.ylabel('y轴-train_y/fitted_y',fontproperties="SimHei",fontsize=18) plt.legend(['train_y','fitted_y'],fontsize=16) plt.show() #4.模型评估 print("2.模型评估值:%.6f."%linearmodel.score(test_x,test_y)) #5.保存模型 joblib.dump(linearmodel,'TrainModel.m') #6.使用模型 linearmodeluse = joblib.load('TrainModel.m') testx = [[1.6]] print("3.%f的预测结果为%f."%(testx[0][0],linearmodeluse.predict([[1.6]])))

TensorFlow框架测试

Tensorflow 1.x的写法:

import tensorflow as tf a = tf.constant([[1,2]])#一行二列 b = tf.constant([[2],[4]])#二行一列矩阵 c = tf.matmul(a,b) sess = tf.Session() result = sess.run(c) print("result=",result) sess.close() import tensorflow as tf a = tf.Variable(3) b = tf.Variable(4) c = tf.add(a,b) init_op = tf.global_variables_initiializer() sess = tf.InteractiveSession() sess.run(init_op) print("c = ",sess.run(c)) sess.close()

TensorFlow 2.x的写法

import tensorflow as tf # 第一段代码 - TF 2.x 写法 a = tf.constant([[1,2]]) b = tf.constant([[2],[4]]) c = tf.matmul(a, b) print("result=", c.numpy()) # 直接获取numpy值 # 第二段代码 - TF 2.x 写法 a = tf.Variable(3) b = tf.Variable(4) c = tf.add(a, b) print("c = ", c.numpy())

案例:识别模糊的手写数字图片

import tensorflow as tf import matplotlib.pyplot as plt import time #1.下载安装MNIST数据集 import tensorflow.examples.tutorials.mnist.input_data as inputdata mnist = inputdata.read_Data_Sets("MNIST_data/",one_hot=True) #2.构建模型 x = tf.placeholder(tf.float32,[None,784]) y_real = tf.placeholder("float", [None,10]) #学习参数:参数矩阵W,偏置b W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) y_predict = tf.nn.softmax(tf.matmul(x,W) + b) #反向传播结构 train_cost = -tf.reduce_sum(y_real*tf.log(y_predict)) #优化器 optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(train_cost) #设置模型保存路径 saver = tf.train.Saver() model_path = "model/mnist_model.ckpt" train_epochs = 2000 batch_size = 100 #创建字典,保存训练过程中的参数信息 train_info = {"epoch":[],"train_cost":[],"train_accuracy":[]} #启动会话 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) #3.训练模型 start_time = time.time() print("1.训练模型") for epoch in range(1,train_epochs+1): batch_xs,batch_ys = mnist.train.next_batch(batch_size) opt,cost = sess.run([optimizer,train_cost],feed_dict={x:batch_xs,y_real:batch_ys}) #计算识别准确率 train_cor_pred = tf.equal(tf.argmax(y_predict,1),tf.argmax(y_real,1)) train_accuracy = tf.reduce_mean(tf.cast(train_cor_pred,tf.float32)) train_acc = train_accuracy.eval({x:batch_xs,y_real:batch_ys}) #保存训练信息 train_info["epoch"].append(epoch) train_info["train_cost"].append(cost) train_info["train_accuracy"].append(train_acc) end_time = time.time() print("模型训练时间:%.8f秒"%(end_time-start_time)) #图形显示训练过程和识别准确率变化情况 plt.figure() plt.plot(train_info["epoch"],train_info["train_cost"],"r") plt.xlabel('x轴-轮数',fontproperties='SimHei',fontdict=18) plt.ylabel('y轴-轮数',fontproperties='SimHei',fontdict=18) plt.xticks(fontsize=16) plt.yticks(fontsize=16) plt.legend(['train_cost','line'],fontsize=16) plt.figure() plt.plot(train_info["epoch"],train_info["train_accuracy"],"b") plt.xlabel('x轴-轮数',fontproperties='SimHei',fontdict=18) plt.ylabel('y轴-识别准确率',fontproperties='SimHei',fontdict=18) plt.xticks(fontsize=16) plt.yticks(fontsize=16) plt.legend(['train_accuracy', 'line'], fontsize=16) plt.show() #4.测试模型 print("2.测试模型") test_cor_pred = tf.equal(tf.argmax(y_predict,1),tf.argmax(y_real,1)) test_accuracy = tf.reduce_mean(tf.cast(test_cor_pred,tf.float32)) test_acc = test_accuracy.eval({x:mnist.test.images,y_real:mnist.test.labels}) print("测试识别准确率:",test_acc) #5.保存模型 save_path = saver.save(sess,model_path) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) #6.读取模型 saver.restore(sess,model_path) #7.验证模型 print("3.验证模型") #计算识别准确率 valid_cor_prediction = tf.equal(tf.argmax(y_predict,1)) valid_accuracy = tf.reduce_mean(tf.cast(valid_cor_prediction,tf.float32)) print("验证识别准确率:",valid_accuracy.eval({x:mnist.test.images,y_real:mnist.test.labels})) #输出验证结果 output = tf.argmax(y_predict,1) batch_xs,batch_ys = mnist.train.next_batch(2) res,pred_v = sess.run([output,y_predict],feed_dict={x:batch_xs}) print("识别结果:",res) print("标签:",batch_ys) print("手写图像:") plt.imshow(batch_xs[0].reshape(-1, 28)) plt.show() plt.imshow(batch_xs[1].reshape(-1, 28)) plt.show()
http://www.jsqmd.com/news/405961/

相关文章:

  • 收藏这份STELLA自进化LLM智能体指南,轻松入门大模型生物医学研究[特殊字符]
  • 2026年AI大模型应用开发保姆级教程:从入门到精通,这一篇开始
  • GPU显存不足?AI架构师的8个优化技巧,让大模型运行更顺畅
  • 怎样用IDEA上传代码到gitee(码云)?-比较详细
  • 基于SpringBoot+Vue的二手数码产品回收与交易平台设计与实现
  • 让普通人轻松学会AI大模型的5个技巧:从入门到精通的实用指南
  • [SAP] SAP MM模块学习路径
  • 大数据建模中的反规范化技术详解
  • 小白程序员轻松入门LLM Agent,解锁AI智能体高级玩法
  • Go Lang之md5加密方式
  • 2026年3月TikTok外贸SNS社媒体推广公司避坑指南:两家靠谱服务商深度测评,看完再决定 - 深圳昊客网络
  • Zero-Copy零拷贝技术详解
  • 告别数据膨胀:TDengine 帮助企业节省 90% 存储成本
  • 2026年3月TikTok社媒外贸推广公司/服务商深度评测推荐:五强对比与中立决策 - 深圳昊客网络
  • 生活困境 --- 为什么一个慢吞吞的人不受shehui待见
  • Xbox shake off, Xbox领导人交接,有感而发
  • Qt与海康威视工业相机整合:实时采集转换Halcon变量并智能展示(支持多种相机模式与彩色黑白切换)
  • 程序员收藏指南!网络安全五大专业深度解析:黑客技术与高薪就业路径
  • P3385 【模板】负环
  • C++ STL 迭代器详解
  • 2026年想转行网络安全?这篇收藏级攻略带你了解真实网安职场!
  • 应用安全 --- 安卓加固 之 一个简单的安卓ctf
  • BSC节点发现协议全解析:UDP发现、Bootnode引导与Gossip交易广播 - 若
  • 告别数据膨胀:TDengine 的高压缩比如何节省您的存储成本
  • 【建议收藏】大模型的“寒窗苦读“与“应用实践“:训练与推理详解
  • 【GitHub项目推荐--Escrcpy:基于AI的下一代Android设备智能控制平台】⭐
  • 【GitHub项目推荐--Tunnelto:高性能本地服务隧道工具】⭐⭐⭐
  • 大模型开发必备:Langchain框架全面解析
  • Domain Admin 从零开始搭建教程
  • Apache ZooKeeper 简介