基于机器学习和奇异值分解SVD的电池剩余使用寿命预测(Python)

采用k-最近邻KNN和随机森林算法建立预测模型。

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC  # Support Vector Classifier
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, classification_report
from sklearn.decomposition import TruncatedSVD
from ydata_profiling import ProfileReport
from sklearn.metrics import mean_squared_error
import time


import seaborn as sns
from importlib import reload
import matplotlib.pyplot as plt
import matplotlib
import warnings




from IPython.display import display, HTML
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.io as pio


# Configure Jupyter Notebook
pd.set_option('display.max_columns', None) 
pd.set_option('display.max_rows', 500) 
pd.set_option('display.expand_frame_repr', False)
display(HTML("<style>div.output_scroll { height: 35em; }</style>"))
dataset = pd.read_csv('Battery_RUL.csv')
profile = ProfileReport(dataset)
profile
Summarize dataset:   0%|          | 0/5 [00:00<?, ?it/s]
Generate report structure:   0%|          | 0/1 [00:00<?, ?it/s]
Render HTML:   0%|          | 0/1 [00:00<?, ?it/s]
y = dataset['RUL']
x = dataset.drop(columns=['RUL'])
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

Singular Value Decomposition

# Step 5: Initialize and fit TruncatedSVD to your training data
n_components = 6  # Adjust the number of components based on your desired dimensionality
svd = TruncatedSVD(n_components=n_components, random_state=42)
X_train_svd = svd.fit_transform(X_train)




# Step 6: Transform the test data using the fitted SVD
X_test_svd = svd.transform(X_test)

K-Nearest-Neighbors

from sklearn.neighbors import KNeighborsRegressor
start = time.time()
model = KNeighborsRegressor(n_neighbors=3).fit(X_train_svd,y_train)
end_train = time.time()
y_predictions = model.predict(X_test_svd) # These are the predictions from the test data.
end_predict = time.time()






kNN = [model.score(X_test_svd,y_test), 
       mean_squared_error(y_test,y_predictions,squared=False),
       end_train-start,
       end_predict-end_train,
       end_predict-start]


print('R-squared error: '+ "{:.2%}".format(model.score(X_test_svd,y_test)))
print('Root Mean Squared Error: '+ "{:.2f}".format(mean_squared_error(y_test,y_predictions,squared=False)))
R-squared error: 98.93%
Root Mean Squared Error: 33.30
plt.style.use('seaborn-white')
plt.rcParams['figure.figsize']=5,5 


fig,ax = plt.subplots()
plt.title('Actual vs Predicted')
plt.xlabel('Actual')
plt.ylabel('Predicted')
g = sns.scatterplot(x=y_test,
                y=y_predictions,
                s=20,
                alpha=0.6,
                linewidth=1,
                edgecolor='black',
                ax=ax)
f = sns.lineplot(x=[min(y_test),max(y_test)],
             y=[min(y_test),max(y_test)],
             linewidth=4,
             color='gray',
             ax=ax)


plt.annotate(text=('R-squared error: '+ "{:.2%}".format(model.score(X_test_svd,y_test)) +'\n' +
                  'Root Mean Squared Error: '+ "{:.2f}".format(mean_squared_error(y_test,y_predictions,squared=False))),
             xy=(0,800),
             size='medium')


xlabels = ['{:,.0f}'.format(x) for x in g.get_xticks()]
g.set_xticklabels(xlabels)
ylabels = ['{:,.0f}'.format(x) for x in g.get_yticks()]
g.set_yticklabels(ylabels)
sns.despine()

Random Forest

%%time
from sklearn.ensemble import RandomForestRegressor
start = time.time()
model = RandomForestRegressor(n_jobs=-1,
                              n_estimators=100,
                              min_samples_leaf=1,
                              max_features='sqrt',
                              # min_samples_split=2,
                              bootstrap = True,
                              criterion='mse',
                             ).fit(X_train_svd,y_train)
end_train = time.time()
y_predictions = model.predict(X_test_svd) # These are the predictions from the test data.
end_predict = time.time()


Random_Forest = [model.score(X_test_svd,y_test), 
                                   mean_squared_error(y_test,y_predictions,squared=False),
                                   end_train-start,
                                   end_predict-end_train,
                                   end_predict-start]


print('R-squared error: '+ "{:.2%}".format(model.score(X_test_svd,y_test)))
print('Root Mean Squared Error: '+ "{:.2f}".format(mean_squared_error(y_test,y_predictions,squared=False)))
R-squared error: 99.75%
Root Mean Squared Error: 15.97
CPU times: total: 3.34 s
Wall time: 389 ms
plt.style.use('seaborn-white')
plt.rcParams['figure.figsize']=5,5 


fig,ax = plt.subplots()
plt.title('Actual vs Predicted')
plt.xlabel('Actual')
plt.ylabel('Predicted')
g = sns.scatterplot(x=y_test,
                y=y_predictions,
                s=20,
                alpha=0.6,
                linewidth=1,
                edgecolor='black',
                ax=ax)
f = sns.lineplot(x=[min(y_test),max(y_test)],
             y=[min(y_test),max(y_test)],
             linewidth=4,
             color='gray',
             ax=ax)


plt.annotate(text=('R-squared error: '+ "{:.2%}".format(model.score(X_test_svd,y_test)) +'\n' +
                  'Root Mean Squared Error: '+ "{:.2f}".format(mean_squared_error(y_test,y_predictions,squared=False))),
             xy=(0,800),
             size='medium')


xlabels = ['{:,.0f}'.format(x) for x in g.get_xticks()]
g.set_xticklabels(xlabels)
ylabels = ['{:,.0f}'.format(x) for x in g.get_yticks()]
g.set_yticklabels(ylabels)
sns.despine()

工学博士,担任《Mechanical System and Signal Processing》《中国电机工程学报》《控制与决策》等期刊审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/702871.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

权限维持--小结

权限维持 windows 域环境 基于验证 DLL 加载-SSP 基于验证 DLL 加载-HOOK 基于机制账号启用-DSRM 基于用户属性修改-SID-history 基于登录进程劫持-Skeleton-Key 单机 基于用户-隐藏用户 基于服务 TGT-黄金白银票据 基于软件-GotoHTTP&RustDesk 自启动 粘滞键 …

vue2组件封装实战系列之message组件之方法式调用

组件之 GfMessage 消息提示, 用于页面顶部消息栏的展示, 默认是居中显示, 可以通过设置属性来改变位置 消息组件有点类似 alert 组件,区别是 message 组件是不用写在页面上,而是通过调用方法来弹窗显示 效果预览 属性 参数类型说明可选值默认值visibleBoolean显示隐藏-falses…

Maven增强插件助你开发快人一步

因为之前的工作中一直用的Maven进行jar包管理&#xff0c;每次新加依赖都要去中央仓库上搜索下坐标&#xff0c;这里为了方便以SerchEveryWhere为入口&#xff0c;增加了一个Maven的搜索tab&#xff0c;输入你想要找的包名即可在idea中直接显示最新版对应的坐标以及cve数&#…

线程池处理Runnable任务

1、线程池处理Runnable任务 1.1、ThreadPoolExecutor创建线程池对象示例 ExecutorService pools new ThreadPoolExecutor&#xff08;3&#xff0c;5&#xff0c;8&#xff0c;TimeUnit.SECONDS&#xff0c;new ArrayBlockingQueue<>(6)&#xff0c;Executors.default…

查分易学生成绩查询网站

亲爱的老师们&#xff0c;分享一个超级方便的小程序——查分易小程序&#xff0c;可以让学生查询成绩变得快捷高效。 1.创建表格设置 老师要准备一个Excel表格&#xff0c;表头要设置比如“姓名&#xff0c;学号等。系统只认Sheet1&#xff0c;所以要确保查询的信息在第一个表…

双非本科一年20w,已是人中龙凤了

大家好&#xff0c;我是白露啊。 “双非本科一年20w已经是人中龙凤了”……吗&#xff1f; 牛客上刷到这条帖子&#xff0c;我一开始以为是一个钓鱼、引战贴。看完才觉得他说的很对&#xff0c;现在在求职选择工作的时候&#xff0c;网上都觉得得40万、50万&#xff0c;但当真…

基于DenseNet网络实现Cifar-10数据集分类

目录 1.作者介绍2.Cifar-10数据集介绍3.Densenet网络模型3.1网络背景3.2网络结构3.2.1Dense Block3.2.2Bottleneck层3.2.3Transition层3.2.4压缩 4.代码实现4.1数据加载4.2建立 DenseNet 网络模型4.3模型训练4.4训练代码4.5测试代码 参考链接 1.作者介绍 吴思雨&#xff0c;女…

【python】tkinter GUI开发: Button和Entry的应用实战探索

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…

2024年计算机、信息工程与大数据应用国际会议(CIEBDA 2024)

2024 International Conference on Computer, Information Engineering, and Big Data Applications 【1】大会信息 会议简称&#xff1a;CIEBDA 2024 大会地点&#xff1a;中国青岛 审稿通知&#xff1a;投稿后2-3日内通知 投稿邮箱&#xff1a;ciebdasub-paper.com 【2】会…

【three.js】设置three.js全屏展示,并解决大小动态变化

目录 一、设置全屏 二、canvas画布宽高度动态变化 一、设置全屏 这个很简单,直接用代码读取当前全屏需要的长宽即可。 const width = window.innerWidth; //窗口文档显示区的宽度作为画布宽度 const height = window.innerHeight; //窗口文档显示区的高度作为画布高度 二、…

this关键字,构造函数(构造器)

文章目录 thisthis是什么应用场景 构造器注意事项代码演示 this this是什么 this就是一个变量&#xff0c;可以在方法中&#xff0c;拿到当前对象 应用场景 解决变量名称 冲突问题 构造器 注意事项 必须和类名相同没有返回值只要参数不同&#xff08;个数不同&#xff0…

三:SpringBoot的helloworld和使用Springboot的优点以及快速创建Springboot应用

三&#xff1a;SpringBoot的helloworld和使用Springboot的优点以及快速创建Springboot应用 一&#xff1a;HelloWorld [我们创建的是maven项目或者直接创建一个Spring] 1.1&#xff1a;创建一个maven 项目&#xff08;1】&#xff1a;需要自己手动写一个SpringBoot 的启动类同…

EVA-CLIP实战

摘要 EVA-CLIP,这是一种基于对比语言图像预训练(CLIP)技术改进的模型,通过引入新的表示学习、优化和增强技术,显著提高了CLIP的训练效率和效果。EVA-CLIP系列模型在保持较低训练成本的同时,实现了与先前具有相似参数数量的CLIP模型相比更高的性能。特别地,文中提到的EV…

Flink的简单学习五

一 动态表与连续查询 1.1 动态表 1.是flink的支持流数据Table API 和SQL的核心概念。动态表随时间的变化而变化 2.在流上面定义的表在内部是没有数据的 1.2 连续查询 1.永远不会停止&#xff0c;结果是一张动态表 二 Flink SQL 2.1 sql行 1.先启动启动flink集群 yarn-see…

TOGAF架构介绍

框架组件 软件开发过程中通用能力的集合。 一个完整的框架包括&#xff1a;异常处理组件&#xff0c;数据访问组件&#xff0c;日志组件&#xff0c;错误码组件。

面向对象三大特征之:封装

文章目录 什么是封装&#xff1f;封装的设计规范 什么是封装&#xff1f; 就是用类设计对象处理某一个事物的数据时&#xff0c;应该把要处理的数据&#xff0c;以及处理这些书记的方法设计到一个对象中去。 封装的设计规范 合理隐藏&#xff0c;合理暴露 public就是都能访问…

搭建自己的多平台镜像站

# 1. 拉取代码 $ git clone https://github.com/wzshiming/crproxy.git $ cd crproxy/examples/default# 2. 修改网关域名 使用vim编辑start.sh文件&#xff0c;将第五行的gateway变量值修改为你自己设定的域名。 原&#xff1a;gatewaycr.zsm.io 修改为&#xff1a;gatewayXS…

LeetCode | 66.加一

这道题有多个思路&#xff0c;可以依次取数组的每一位&#xff0c;乘10后加下一位&#xff0c;直到最后一位&#xff0c;就得到我们数组所表示的数字&#xff0c;然后加一&#xff0c;然后把新得到的数字再转化为对应的数组&#xff0c;我的做法是直接取数组的最后一位&#xf…

快速上手 GreatSQL 8.0.32-25 with openEuler 24.03 LTS

5 月底&#xff0c;openEuler 24.03 LTS 发布&#xff0c;详情戳&#xff1a; 恭喜&#xff01;openEuler 24.03 LTS 版本发布&#xff1a;首个AI原生开源操作系统 在诸多亮点特性中&#xff0c;有一条值得注意&#xff1a; 集成 GreatSQL 数据库&#xff0c;适用于金融级应用场…

小程序 js+Canvas 绘制半圆环虚线进度条

效果图&#xff1a; 思路&#xff1a;过程分为三步&#xff0c;第1步&#xff0c;先画虚线底部背景&#xff0c;第2步&#xff0c;画动态的虚线&#xff08;已选虚线蓝颜色&#xff09;&#xff0c;第3步&#xff0c;画动态的外标&#xff08;已选虚线外位置的标&#xff09;&a…