python与人工智能代码基础

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

make_regression() 实际上相当于做了这样的事:

真实斜率 = 随机生成(比如 7.8),真实截距 = 随机生成(比如 1.2)

for 每个x值:

理想y值 = 7.8 * x + 1.2

实际y值 = 理想y值 + 随机噪声(范围±6)

python 复制代码
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的写法:

python 复制代码
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的写法

python 复制代码
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())

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

python 复制代码
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()
相关推荐
ccLianLian2 小时前
强化学习·贝尔曼方程
人工智能
Coding茶水间2 小时前
基于深度学习的鸡数量统计系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
开发语言·人工智能·深度学习·yolo·目标检测·机器学习
爱可生开源社区2 小时前
2026 年 AI 预言:幻觉监管、GPU 现实撞墙与 “广告版” ChatGPT 的到来
人工智能
海天一色y2 小时前
用Python和Pygame从零打造植物大战僵尸:完整技术解析
开发语言·python·pygame
嫂子的姐夫2 小时前
029-rs5:欧治
爬虫·python·逆向
Eloudy2 小时前
直接法 读书笔记 07 第7章 减少填充的排序
人工智能·arch·hpc
ppppppatrick2 小时前
【深度学习基础篇04】从回归到分类:图像分类与卷积神经网络入门
人工智能·深度学习·分类
yezannnnnn2 小时前
我用Claude Code搭了个四个AI的团队,居然真的能协作开发(附项目地址)
人工智能·claude
数据智能老司机2 小时前
打造 ML/AI 系统的内部开发者平台(IDP)——生产级 LLM 系统设计
机器学习·llm·aiops