汽车状态分类器代码详解(未完待续)
先是data_processing文件import pandas as pd from urllib.request import urlretrieve def load_data(downloadTrue): # download data from : http://archive.ics.uci.edu/ml/datasets/CarEvaluation if download: data_path, _ urlretrieve(http://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data, car.csv) print(Downloaded to car.csv) # use pandas to view the data structure col_names [buying, maint, doors, persons, lug_boot, safety, class] data pd.read_csv(car.csv, namescol_names) return data def convert2onehot(data): # covert data to onehot representation return pd.get_dummies(data, prefixdata.columns) if __name__ __main__: data load_data(downloadTrue) new_data convert2onehot(data) print(data.head()) print(\nNum of data: , len(data), \n) # 1728 # view data values for name in data.keys(): print(name, pd.unique(data[name])) print(\n, new_data.head(2)) new_data.to_csv(car_onehot.csv, indexFalse)1.pd.read_csv功能给数据添加一行列索引也就是横在数据上面的表头返回一个诡异的数据类型data: class ‘pandas.core.frame.DataFrame’col_names [buying, maint, doors, persons, lug_boot, safety, class] data pd.read_csv(car.csv, namescol_names)2.get_dummies(data, prefixdata.columns)pandas的功能真的很强大可以直接以data.columns为标准生成独热码形式https://blog.csdn.net/qq_35290785/article/details/91415240return pd.get_dummies(data, prefixdata.columns)3.data.head()读取前五行数据https://blog.csdn.net/qq_18649781/article/details/89033749print(data.head())4.pd.unique用法自己琢磨把…for name in data.keys(): print(name, pd.unique(data[name]))5.to_csvdt.to_csv() #默认dt是DataFrame的一个实例indexFalse表示不保留行索引也就是不保留最左侧的竖列索引new_data.to_csv(car_onehot.csv, indexFalse)综上所述data_processing代码完成了one-hot的csv文件的建立接下来 我们来看model文件import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import data_processing data data_processing.load_data(downloadTrue) new_data data_processing.convert2onehot(data) # prepare training data new_data new_data.values.astype(np.float32) # change to numpy array and float32 np.random.shuffle(new_data) sep int(0.7*len(new_data)) train_data new_data[:sep] # training data (70%) test_data new_data[sep:] # test data (30%) # build network tf_input tf.placeholder(tf.float32, [None, 25], input) tfx tf_input[:, :21] tfy tf_input[:, 21:] l1 tf.layers.dense(tfx, 128, tf.nn.relu, namel1) l2 tf.layers.dense(l1, 128, tf.nn.relu, namel2) out tf.layers.dense(l2, 4, namel3) prediction tf.nn.softmax(out, namepred) loss tf.losses.softmax_cross_entropy(onehot_labelstfy, logitsout) accuracy tf.metrics.accuracy( # return (acc, update_op), and create 2 local variables labelstf.argmax(tfy, axis1), predictionstf.argmax(out, axis1))[1] opt tf.train.GradientDescentOptimizer(learning_rate0.1) train_op opt.minimize(loss) sess tf.Session() sess.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) # training plt.ion() fig, (ax1, ax2) plt.subplots(1, 2, figsize(8, 4)) accuracies, steps [], [] for t in range(4000): # training batch_index np.random.randint(len(train_data), size32) sess.run(train_op, {tf_input: train_data[batch_index]}) if t % 50 0: # testing acc_, pred_, loss_ sess.run([accuracy, prediction, loss], {tf_input: test_data}) accuracies.append(acc_) steps.append(t) print(Step: %i % t,| Accurate: %.2f % acc_,| Loss: %.2f % loss_,) # visualize testing ax1.cla() for c in range(4): bp ax1.bar(c0.1, heightsum((np.argmax(pred_, axis1) c)), width0.2, colorred) bt ax1.bar(c-0.1, heightsum((np.argmax(test_data[:, 21:], axis1) c)), width0.2, colorblue) ax1.set_xticks(range(4), [accepted, good, unaccepted, very good]) ax1.legend(handles[bp, bt], labels[prediction, target]) ax1.set_ylim((0, 400)) ax2.cla() ax2.plot(steps, accuracies, labelaccuracy) ax2.set_ylim(ymax1) ax2.set_ylabel(accuracy) plt.pause(0.01) plt.ioff() plt.show()1.tf.nn.softmax将所选择的一维数组归一化也就是把每一个元素都变成0-1之间的数字2.tf.metrics.accuracytf.metrics.accuracy返回两个值accuracy为到上一个batch为止的准确度update_op为更新本批次后的准确度。https://blog.csdn.net/lyb3b3b/article/details/83047148因为有局部变量所以要初始化局部变量sess.run(tf.local_variables_initializer())这就是为什么最后有初始化局部变量的原因3.plt.ion()打开交互模式若想动态显示图像则需要使用交互interactive模式。https://www.cnblogs.com/wmy-ncut/p/10172601.html4.plt.subplots前两个数字是子图的行列数figsize(8, 4)估计就是确定figure的大小fig, (ax1, ax2) plt.subplots(1, 2, figsize(8, 4))5.numpy.random.randint(low, highNone, sizeNone, dtype‘l’)函数的作用是返回一个随机整型数范围从低包括到高不包括即[low, high)。如果没有写参数high的值则返回[0,low)的值。size: int or tuple of ints(可选)输出随机数的尺寸比如size (m * n* k)则输出同规模即m * n* k个随机数。默认是None的仅仅返回满足要求的单一随机数。https://blog.csdn.net/u011851421/article/details/83544853即整数生成随机元素的个数或者元组数组的行和列也就是这里生成的是array([1,5,6,8,4,3…]) 共32个元素batch_index np.random.randint(len(train_data), size32)6.现在不用feed_dict{}这种形式了用sess.run(train_op, {tf_input: train_data[batch_index]})7.ax1.cla()plt.cla() # 清除axes轴即清楚当前 figure 中的活动的axes但其他axes保持不变。暂时存疑8.ax1.barhttps://blog.csdn.net/liangzuojiayi/article/details/78187704leftx轴的位置序列一般采用arange函数产生一个序列heighty轴的数值序列也就是柱形图的高度一般就是我们需要展示的数据alpha透明度width为柱形图的宽度一般这是为0.8即可color或facecolor柱形图填充的颜色edgecolor图形边缘颜色label解释每个图像代表的含义linewidth or linewidths or lw边缘or线的宽度bp ax1.bar(c0.1, heightsum((np.argmax(pred_, axis1) c)), width0.2, colorred)后面的先不写了matplotlib有点麻烦补充1.dataframe格式建立https://blog.csdn.net/qq_39161737/article/details/78866399https://blog.csdn.net/weixin_40240670/article/details/80506402加入了save并且单独摘2个测试 data_processing:import pandas as pd from urllib.request import urlretrieve import numpy as np def load_data(downloadTrue): # download data from : http://archive.ics.uci.edu/ml/datasets/CarEvaluation if download: data_path, _ urlretrieve(http://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data, car.csv) print(Downloaded to car.csv) # use pandas to view the data structure col_names [buying, maint, doors, persons, lug_boot, safety, class] data pd.read_csv(car.csv, namescol_names) data_test pd.read_csv(car_test.csv, namescol_names) # print(data_test) #class pandas.core.frame.DataFrame return data, data_test def convert2onehot(data, train): # covert data to onehot representation if train True: print(type(pd.get_dummies(data, prefixdata.columns))) return pd.get_dummies(data, prefixdata.columns) else: x [[0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0],[0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0]] return pd.DataFrame(x) if __name__ __main__: data, data_test load_data(downloadTrue) new_data convert2onehot(data, trainTrue) new_data_test convert2onehot(data_test, trainFalse) # new_data_test new_data_test.values.astype(np.float32) # print(自己建立的是, new_data_test) # print(new:, new_data) print(data.head()) print(\nNum of data: , len(data), \n) # 1728 也就是1728行 # view data values for name in data.keys(): print(name, pd.unique(data[name])) print(\n, new_data.head(2)) new_data.to_csv(car_onehot.csv, indexFalse) # new_data_test.to_csv(car_onehot_test.csv, indexFalse)model:import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import data_processing import os tf.app.flags.DEFINE_integer(is_train, 2, 指定是否是训练模型还是拿数据去预测) FLAGS tf.app.flags.FLAGS data, data_test data_processing.load_data(downloadTrue) new_data data_processing.convert2onehot(data, trainTrue) new_data_test data_processing.convert2onehot(data, trainFalse) # prepare training data new_data new_data.values.astype(np.float32) # change to numpy array and float32 转换为numpy数组形式并规定为浮点数 new_data_test new_data_test.values.astype(np.float32) #print(打印, new_data) np.random.shuffle(new_data) sep int(0.7*len(new_data)) train_data new_data[:sep] # training data (70%) test_data new_data[sep:] # test data (30%) realtest_data new_data_test[::] np.random.shuffle(new_data_test) # build network tf_input tf.placeholder(tf.float32, [None, 25], input) #其中最后4个是汽车状态评价 tfx tf_input[:, :21] tfy tf_input[:, 21:] # tfx_test tf_input[:, :21] # tfy_test tf_input[:, 21:] l1 tf.layers.dense(tfx, 128, tf.nn.relu, namel1) l2 tf.layers.dense(l1, 128, tf.nn.relu, namel2) out tf.layers.dense(l2, 4, namel3) prediction tf.nn.softmax(out, namepred) # print(prediction:, prediction) loss tf.losses.softmax_cross_entropy(onehot_labelstfy, logitsout) accuracy tf.metrics.accuracy( # return (acc, update_op), and create 2 local variables labelstf.argmax(tfy, axis1), predictionstf.argmax(out, axis1))[1] opt tf.train.GradientDescentOptimizer(learning_rate0.1) train_op opt.minimize(loss) #进行模型保存 # (2)收集要显示的变量 # 先收集损失和准确率 tf.summary.scalar(losses, loss) tf.summary.scalar(accuracy, accuracy) # (3)合并所有变量op merged tf.summary.merge_all() # 创建模型保存与加载 saver tf.train.Saver() with tf.Session() as sess: sess.run(tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())) # (1)创建一个events文件实例 file_writer tf.summary.FileWriter(./tmp/summary2/, graphsess.graph) # 加载模型 if os.path.exists(./tmp/modelckpt2/checkpoint): saver.restore(sess, ./tmp/modelckpt2/cnn_model) # 注意modelckpt2这个文件夹要自己建立 # 也就是说 模型保存和加载的时候 也就是saver.save或saver.restore的路径需要自己建立 否则会蓝屏 # 但是创建envents实例化的路径可以不用自己建立 # trainingtest1 if FLAGS.is_train 1: accuracies, steps [], [] for t in range(4000): # training batch_index np.random.randint(len(train_data), size32) #生成32个随机数返回array数组 sess.run(train_op, {tf_input: train_data[batch_index]}) #注意feeddict可以不用了 # 运行合变量op写入事件文件当中 summary sess.run(merged, {tf_input: train_data[batch_index]}) file_writer.add_summary(summary, t) if t % 2 0: saver.save(sess, ./tmp/modelckpt2/cnn_model) if t % 50 0: # testing acc_, pred_, loss_ sess.run([accuracy, prediction, loss], {tf_input: test_data}) accuracies.append(acc_) steps.append(t) print(Step: %i % t,| Accurate: %.2f % acc_,| Loss: %.2f % loss_,) # test2 else: for t in range(2): # training batch_index np.random.randint(len(realtest_data), size1) # sess.run(train_op, {tf_input_test: test_real[batch_index]}) acc_, pred_, loss_ sess.run([accuracy, prediction, loss], {tf_input: realtest_data[batch_index]}) print(真实值为, sess.run(tf.argmax(realtest_data[batch_index][:, 21:], axis1))) print(预测值为, sess.run(tf.argmax(pred_, axis1)))

相关新闻

Adobe GenP 3.0终极方案:跨版本激活Adobe Creative Cloud的深度解析

Adobe GenP 3.0终极方案:跨版本激活Adobe Creative Cloud的深度解析

Adobe GenP 3.0终极方案:跨版本激活Adobe Creative Cloud的深度解析 【免费下载链接】Adobe-GenP Adobe CC 2019/2020/2021/2022/2023 GenP Universal Patch 3.0 项目地址: https://gitcode.com/gh_mirrors/ad/Adobe-GenP 在数字创意领域,Adobe C…

2026/11/1 10:40:16 阅读更多 →
手语翻译革命:用AI打破无声世界的沟通壁垒

手语翻译革命:用AI打破无声世界的沟通壁垒

手语翻译革命:用AI打破无声世界的沟通壁垒 【免费下载链接】Sign-Language-Interpreter-using-Deep-Learning A sign language interpreter using live video feed from the camera. 项目地址: https://gitcode.com/gh_mirrors/si/Sign-Language-Interpreter-usi…

2026/10/24 0:07:01 阅读更多 →
工程车辆识别挖掘机推土机洒水车压路机等识别分割数据集labelme格式3823张13类别

工程车辆识别挖掘机推土机洒水车压路机等识别分割数据集labelme格式3823张13类别

数据集格式:labelme格式(不包含mask文件,仅仅包含jpg图片和对应的json文件)图片数量(jpg文件个数):3823标注数量(json文件个数):3823标注类别数:13标注类别名称:["articulated_truck","backhoe_loader&…

2026/10/27 19:19:01 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/2 0:25:10 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/2 10:46:11 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/2 10:47:40 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/2 1:16:51 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/2 10:46:31 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/2 10:46:26 阅读更多 →

日新闻

周新闻

月新闻