pyspark 随机森林重要性

废话不多说,直接上代码!


```python
from pyspark.ml import Pipeline
from pyspark.ml.classification import RandomForestClassifier
from pyspark.ml.linalg import Vectors
from pyspark.sql import SparkSession
import pandas as  pd
from pyspark.ml.feature import Normalizer, VectorAssembler, StandardScaler, StringIndexer
from pyspark.sql import SparkSession, types
# aa=aa[['tempcabinetnacelle_1sec','blade3tempbattbox_1sec','tempcabinetnacelle_1sec','label']]
# trainData=aa[['tempcabinetnacelle_1sec','blade3tempbattbox_1sec','tempcabinetnacelle_1sec','label']]
spark = SparkSession.builder.appName("appName111").enableHiveSupport().getOrCreate()
df = spark.read.csv('/root/a.csv', header=True)

df=df[['tempcabinetnacelle_1sec','blade3tempbattbox_1sec','blade1tempbattbox_1sec','label']]

##类型转换
df=df.withColumn('tempcabinetnacelle_1sec', df['tempcabinetnacelle_1sec'].cast(types.FloatType()))
df=df.withColumn('blade3tempbattbox_1sec', df['blade3tempbattbox_1sec'].cast(types.FloatType()))
df=df.withColumn('blade1tempbattbox_1sec', df['blade1tempbattbox_1sec'].cast(types.FloatType()))
df=df.withColumn('label', df['label'].cast(types.IntegerType()))
##将特征转化为featute
df_assembler = VectorAssembler(inputCols=['tempcabinetnacelle_1sec','blade3tempbattbox_1sec','blade1tempbattbox_1sec'], outputCol="features")
df = df_assembler.transform(df)
df.show()
rf = RandomForestClassifier(numTrees=int(2), maxDepth=int(4), labelCol='label', seed=11)
# ###训练
model = rf.fit(df)
ff=model.featureImportances
print(len(ff))
importancesList=[float(col) for col in  ff]
colList=['tempcabinetnacelle_1sec','blade3tempbattbox_1sec','blade1tempbattbox_1sec']
result=dict(zip(colList,importancesList))
print(result)`在这里插入代码片`

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用PySpark进行随机森林的示例代码: ```python from pyspark.ml import Pipeline from pyspark.ml.classification import RandomForestClassifier from pyspark.ml.evaluation import MulticlassClassificationEvaluator from pyspark.ml.feature import VectorAssembler from pyspark.sql import SparkSession # 创建SparkSession spark = SparkSession.builder.appName("RandomForestExample").getOrCreate() # 加载数据集 data = spark.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt") # 将特征向量组合成一个向量列 assembler = VectorAssembler(inputCols=data.columns[1:-1], outputCol="features") # 创建随机森林分类器 rf = RandomForestClassifier(labelCol="label", featuresCol="features", numTrees=10) # 创建Pipeline pipeline = Pipeline(stages=[assembler, rf]) # 将数据集分为训练集和测试集 (trainingData, testData) = data.randomSplit([0.7, 0.3]) # 训练模型 model = pipeline.fit(trainingData) # 进行预测 predictions = model.transform(testData) # 评估模型 evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy") accuracy = evaluator.evaluate(predictions) print("Test Error = %g" % (1.0 - accuracy)) # 关闭SparkSession spark.stop() ``` 在这个示例中,我们首先使用`VectorAssembler`将特征向量组合成一个向量列,然后创建一个`RandomForestClassifier`分类器,并将其与`VectorAssembler`一起包含在一个`Pipeline`中。接下来,我们将数据集分为训练集和测试集,并使用`Pipeline`训练模型。最后,我们使用`MulticlassClassificationEvaluator`评估模型的准确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

北京下雨天

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值