【问题标题】:Running a Python function on Spark Dataframe在 Spark Dataframe 上运行 Python 函数
【发布时间】:2017-01-27 17:00:53
【问题描述】:

我有一个 python 函数,它基本上从原始数据集中进行一些采样并将其转换为 training_test。

我已经编写了用于处理 pandas 数据框的代码。

我想知道是否有人知道如何在 pyspark 中的 Spark DAtaframe 上实现相同的功能?我应该使用 Spark Dataframe 而不是提到的 Pandas 数据框或 numpy 数组,仅此而已?

请告诉我

def train_test_split(recommender,pct_test=0.20,alpha=40):
    """ This function takes a ratings data and splits it into 
    train, validation and test datasets

    This function will take in the original user-item matrix and "mask" a percentage of the original ratings where a
    user-item interaction has taken place for use as a test set. The test set will contain all of the original ratings, 
    while the training set replaces the specified percentage of them with a zero in the original ratings matrix. 

    parameters: 

    ratings - the original ratings matrix from which you want to generate a train/test set. Test is just a complete
    copy of the original set. This is in the form of a sparse csr_matrix. 

    pct_test - The percentage of user-item interactions where an interaction took place that you want to mask in the 
    training set for later comparison to the test set, which contains all of the original ratings. 

    returns:

    training_set - The altered version of the original data with a certain percentage of the user-item pairs 
    that originally had interaction set back to zero.

    test_set - A copy of the original ratings matrix, unaltered, so it can be used to see how the rank order 
    compares with the actual interactions.

    user_inds - From the randomly selected user-item indices, which user rows were altered in the training data.
    This will be necessary later when evaluating the performance via AUC.

    """

    test_set = recommender.copy() # Make a copy of the original set to be the test set. 

    test_set=(test_set>0).astype(np.int8)
    training_set = recommender.copy() # Make a copy of the original data we can alter as our training set. 
    nonzero_inds = training_set.nonzero() # Find the indices in the ratings data where an interaction exists
    nonzero_pairs = list(zip(nonzero_inds[0], nonzero_inds[1])) # Zip these pairs together of user,item index into list
    random.seed(0) # Set the random seed to zero for reproducibility
    num_samples = int(np.ceil(pct_test*len(nonzero_pairs))) # Round the number of samples needed to the nearest integer
    samples = random.sample(nonzero_pairs, num_samples) # Sample a random number of user-item pairs without replacement
    user_inds = [index[0] for index in samples] # Get the user row indices
    item_inds = [index[1] for index in samples] # Get the item column indices
    training_set[user_inds, item_inds] = 0 # Assign all of the randomly chosen user-item pairs to zero

    conf_set=1+(alpha*training_set)
    return training_set, test_set, conf_set, list(set(user_inds)) 

【问题讨论】:

    标签: python pandas apache-spark


    【解决方案1】:

    您可以在 Spark 数据帧上使用 randomSplit 函数。

    (train, test) = dataframe.randomSplit([0.8, 0.2])
    

    【讨论】:

    • 我正在寻找更多以了解如何在 spark 数据框中实现该功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    相关资源
    最近更新 更多