【问题标题】:Is there any train_test_split in pyspark or MLLib?pyspark 或 MLLib 中是否有 train_test_split?
【发布时间】:2023-04-01 12:27:01
【问题描述】:

下面这个经典的 sklearm 经典 train_test_split 代码是否有任何 pyspark / MLLib 版本?

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(featuresonly, 
                                                    target, 
                                                    test_size = 0.2, 
                                                    random_state = 123)

# Show the results of the split
print("Training set has {} samples.".format(X_train.shape[0]))
print("Testing set has {} samples.".format(X_test.shape[0]))
print("Training set has good {} samples.".format(len(y_train) -y_train.sum()))
print("Testing set has good {} samples.".format(len(y_test) -y_test.sum()))

【问题讨论】:

    标签: python dataframe pyspark apache-spark-mllib


    【解决方案1】:

    根据文档,它被称为RandomSplit
    有很多例子here看看怎么用

    【讨论】:

      【解决方案2】:

      RandomSplit - 如上所述 - 是要走的路

      train, test = final_data.randomSplit([0.7,0.3], seed=4000)
      

      然后,你可以计算你在训练集中的标签

      dataset_size=float(train.select("label").count())
      
      Positives=train.select("label").where('label == 1').count()
      
      percentage_ones=(float(Positives)/float(dataset_size))*100
      
      Negatives=float(dataset_size-Positives)
      
      print('The number of ones are {}'.format(Positives))
      
      print('Percentage of ones are {}'.format(percentage_ones))
      
      print(' The number of zeroes are {}'.format(Negatives))
      

      【讨论】:

      • 不幸的是,randomSplit() 缺少很多功能,例如 train_test_split() 中的分层功能。此功能不能成为其真正的替代品。有什么建议吗?
      • pyspark 中的分层抽样可以通过使用 sampleBy 来实现,假设您要对样本进行分层的列是“colname”sampled = df.sampleBy("colname", fractions={4: 0.2, 6: 0.4, 8: 0.2}, seed=0)
      • 太好了,我做了一点搜索,发现它没有替换,非常完美,我相信它非常解决了目的。 spark.apache.org/docs/latest/api/python/reference/api/…
      猜你喜欢
      • 2020-09-17
      • 2017-08-31
      • 2016-12-14
      • 2014-12-11
      • 2019-09-20
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      相关资源
      最近更新 更多