zoukankan      html  css  js  c++  java
  • Python处理数据集-2

    原数据集的数据格式:

    每行为:(test_User, test_Item) negativeItem1 negativeItem2 negativeItem3 …… negativeItem99 

    即每一行对应一个user 与100个item,其中1个item为整理,其余99个为负例。

    将要处理成的目标数据的数据格式为:

    每一行对应一个User与一个Item,用“,”隔开,写入一个新的文件。

    【解决方案】

    ef load_test_user_item_file(filename):
        user_item_file = open('test_user_item_file.test','w',encoding='UTF-8')
        with open(filename, "r") as f:
            line = f.readline()
            while line != None and line != "":
                arr = line.split(" ")  # 针对 Musical_Instruments 数据集
                arr[0] = arr[0].lstrip("(")
                arr[0] = arr[0].rstrip(")")
                user = arr[0].split(",")[0]
                item = arr[0].split(",")[1]
                user_item_file.write( user + ',' + item + '
    ')
                for x in arr[1:]:
                    if x == arr[99]:
                        user_item_file.write(user + ',' + x )
                    else:
                        user_item_file.write(user + ',' + x + '
    ')
                line = f.readline()
    
        return user_item_file

    结果为:

  • 相关阅读:
    基本STRUTS标签-学习笔记-Logic标签
    Static的使用
    模板template
    iostream与iostream.h的区别
    数据库连接池
    canvas基础
    javascript面试题集
    ES6新特性学习
    原型和原型链
    css为tbody或者li奇数偶数行样式
  • 原文地址:https://www.cnblogs.com/shenxiaolin/p/11270647.html
Copyright © 2011-2022 走看看