sklearn随机森林 测试 路面点云分类+python代码

17 篇文章 26 订阅

一、特征5个坐标

坐标-特征-类别
训练数据

二、模型训练

记录分享给有需要的人,代码质量勿喷

import numpy as np
import pandas as pd
import joblib


#region 1 读取数据
dir = 'D:\\py\\RandomForest\\'
filename1 = 'trainRS'
filename2 = '.csv'
path = dir+filename1+filename2

rawdata = pd.read_csv(path, encoding='gbk')

print('=== 1 读取数据')
#endregion


#region 2 构造数据集
x = rawdata.drop(columns=['x','y','z','Classification'])
y = rawdata['Classification']

#训练集6:验证集4
from sklearn.model_selection import train_test_split
indices = np.arange(x.shape[0]) #索引
x_train,x_test,y_train, y_test, indices_train, indices_test = train_test_split(x,y,indices,test_size=0.4,random_state=0)

print('=== 2 构造训练集和验证集')
#endregion


#region 3 Random Forest 模型训练与保存------------------------最耗时间
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier() #随机森林实例化 默认参数
rfc.fit(x_train, y_train) #模型训练

# 保存模型
joblib.dump(rfc,"modelRS.m")
# rfc2 = joblib.load("modelRS.m") #调用

print('=== 3 Random Forest 模型训练与保存')
#endregion


#region 4 模型评分与验证结果
score_rfc = rfc.score(x_test,y_test)
print('score_rfc =',score_rfc)

#验证集预测
yPre = rfc.predict(x_test)

print('=== 4 模型评分与验证集预测')
#endregion


#region 5 查看特征的重要性占比
feature_importance = rfc.feature_importances_
cols = rawdata.columns

fi = pd.DataFrame({'特征':np.array(cols)[3:-1], '重要性占比':feature_importance}).sort_values(by='重要性占比',axis=0, ascending=False)

print('=== 5 查看特征列的重要性')
print(fi)
#endregion


#region 6 输出验证集结果
test_data = rawdata.loc[indices_test]
test_data_np = test_data.to_numpy()

#合并原始数据和预测结果
test_data_pre = np.hstack((test_data_np, yPre.reshape(-1, 1))) #水平(沿着列方向)合并数组

output_file = filename1 + "_ValidateResult.txt"
np.savetxt(output_file, test_data_pre, fmt="%f", delimiter="\t")

print('=== 6 输出验证集结果')
#endregion

三、验证集结果

验证集98%,hhhhhhhhh

特征占比有点超乎想象

四、测试

记录分享给有需要的人,代码质量勿喷

import numpy as np
import pandas as pd
import joblib


#region 1 读取数据
dir = 'D:\\py\\RandomForest\\'
filename1 = 'testRS2'
filename2 = '.csv'
path = dir+filename1+filename2
data = pd.read_csv(path,encoding='gbk')

test = data.drop(columns=['x','y','z','Classification'])

print('=== 1 读取数据')
#endregion


#region 2 调用模型预测
rfc = joblib.load("modelRS.m") #调用
pre = rfc.predict(test)

print('=== 2 调用模型预测')
#endregion


#region 3 输出结果
data_np = data.to_numpy()
data_pre = np.hstack((data_np, pre.reshape(-1, 1))) #水平(沿着列方向)合并数组

output_file = filename1 + "_PreResult.txt"
np.savetxt(output_file, data_pre, fmt="%f", delimiter="\t")

print('=== 3 输出结果')
#endregion

还是有效果的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

累了就要打游戏

把我养胖,搞代码

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值