人工智能备考2.2.1-2.2.5总结

此为备考记录,主要记录常用或不熟的语法用法,后续会更新调整的

迭代次数

训练Logistic回归模型(最大迭代次数为1000次)

max_iter为最大迭代次数,所以填入LogisticRegression(max_iter=1000)

训练模型

训练 Logistic 回归模型

数据变量名.fit(X_train,y_train)

保存模型

pickle.dump(前文的模型名,上文的as后的变量)

分析测试结果

(y_test == 上文新生成的ytes变量名).mean

预测数据

模型变量名.predict(X_test)

转换数据

转换成数值类型pd.to_numeric(需要转换的数据,errors='coerce')

X = df['Your age'].apply(X) # 将分类变量转为数值变量

y = df['Your age'].apply(lambda x: int(x.split(' ')[0])) # 假设年龄段为整数

X = pd.get_dummies(X) # 将分类变量转为数值变量

选择特征值建模

选择对应的变量放入对应的位置,去文中找(类似下文)

X = df[['cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'model year', 'origin']]

y = df['mpg']

将数据集划分为训练集和测试集(测试集占比20%

一般固定为以下格式

train_test_split(X,y, random_state=42)

决策树数量

创建随机森林回归模型实例(创建的决策树的数量为100)

RandomForestRegressor(n_estimators=100, random_state=42)

2.2.3导入类错位

import xgboost as xgb修改为:from xgboost import XGBRegressor

测试工具对模型进行测试

train_score = rf_model.score(X_train,y_train) #训练集分数

test_score = rf_model.fit(X_test,y_test) #测试集分数

mse = mean_squared_error(y_test,y_pred) #均方误差

r2 = r2_score(y_test,y_pred) #决定系数

结果

with open('2.2.3_report_xgb.txt', 'w') as xgb_report_file:

xgb_report_file.write(f'XGBoost训练集得分: {xgb_model.score(X_train,y_train) }\n')

xgb_report_file.write(f'XGBoost测试集得分: {xgb_model.score(X_test,y_test) }\n')

xgb_report_file.write(f'XGBoost均方误差(MSE): {mean_squared_error(y_test,y_pred) }\n')

xgb_report_file.write(f'XGBoost决定系数(R^2): {r2_score(y_test,y_pred)}\n')

相关推荐
workflower39 分钟前
用硬件换时间”与“用算法降成本”之间的博弈
人工智能·算法·安全·集成测试·无人机·ai编程
Cx330❀1 小时前
一文吃透Linux System V共享内存:原理+实操+避坑指南
大数据·linux·运维·服务器·人工智能
OPHKVPS1 小时前
Anthropic 为 Claude Code 推出“自动模式”:AI 编码工具迈向更高自主性
网络·人工智能·安全·ai
Allen_LVyingbo1 小时前
斯坦福HAI官网完整版《2025 AI Index Report》全面解读
人工智能·数学建模·开源·云计算·知识图谱
金融小师妹1 小时前
基于AI通胀预期建模与能源冲击传导机制的政策分析:高频信号下的风险再评估
人工智能·svn·能源
胡摩西1 小时前
当大模型遇上毫米级定位:机器人将拥有“空间思维”?
人工智能·机器人·slam·gps·室内定位·roomaps
志栋智能2 小时前
超自动化运维的终极目标:让系统自治运行
运维·网络·人工智能·安全·自动化
彬鸿科技2 小时前
bhSDR Studio/Matlab入门指南(九):FMCW雷达测距实验界面全解析
人工智能·matlab·软件定义无线电
补三补四2 小时前
Prompt工程实践指南:从基础概念到高级应用
人工智能·chatgpt·prompt
weixin_408099672 小时前
图片去水印 API 接口实战:网站如何实现自动去水印(Python / PHP / C#)
图像处理·人工智能·python·c#·php·api·图片去水印