【问题标题】:Getting stuck with the data pre-processing code for machine learning application陷入机器学习应用程序的数据预处理代码中
【发布时间】:2019-12-25 20:17:58
【问题描述】:

我目前正在开发一个机器学习应用程序。请在这段代码中帮助我 - 当我上传大数据集时出现错误。

代码如下:

prep_file = Prepross.objects.get_or_create(
                filename = CurrentFile.objects.order_by('-id')[0].filename,
                coltype = request.POST.getlist('coltype'),
                assvar = request.POST.getlist('assvar'),
                missingvalues = request.POST.getlist('missingvalues'),
                trainingset_size = request.POST['trainingset_size'],
                featscaling = request.POST.getlist('featscaling'))

然后:

        # Get dataframe and change data type
        context = {}
        file_name = CurrentFile.objects.order_by('-id')[0].filename
        coltype = request.POST.getlist('coltype')
        coltype = dict([i.split(':', 1) for i in coltype])
        df = pd.read_csv(os.path.join('media\downloaded', file_name), dtype= coltype)
        row_count = df.count()[0]

        # Keep only selected columns
        assvar = request.POST.getlist('assvar')
        xcols0 = [s for s in assvar if ":X" in s]
        xcols = [i.split(':', 1)[0] for i in xcols0]
        ycol0 = [s for s in assvar if ":y" in s]
        ycol = [i.split(':', 1)[0] for i in ycol0]
        cols = xcols + ycol
        df = df[cols]

        xcols = ', '.join(xcols)
        ycol = ', '.join(ycol)
        missing = request.POST.getlist('missingvalues')
        missing = ', '.join(missing)
        trainingset_s = request.POST.getlist('trainingset_size')
        trainingset_s = ', '.join(trainingset_s)
        testset_s = 100 - int(trainingset_s)
        feat =  request.POST['featscaling']

        # Taking care of missing data
        if missing == "no":
            if len(df) != len(df.dropna()):
                context['selecty'] = 'Your data seem to have Missing Values'
            else:
                df = df.dropna()

        # Return error if columns are not selected
        if len(ycol0) != 1:
            context['selecty'] = 'Please select one y variable'

        elif len(xcols0) < 1:
            context['selecty'] = 'Please select one or more X variables'

错误是:

 File "C:\Users\Admin\PycharmProject\mlapp\views.py", line 81, in post
    coltype = dict([i.split(':', 1) for i in coltype])
ValueError: dictionary update sequence element #0 has length 1; 2 is required
[20/Aug/2019 16:43:39] "POST /preprocessing/ HTTP/1.1" 500 81482

关于错误的附加行和信息:

/preprocessing/ 处的ValueError 字典更新序列元素#0的长度为1; 2 是必需的 请求方法:POST 请求网址:http://127.0.0.1:8000/preprocessing/ Django 版本:2.2.4 异常类型:ValueError 异常值:
字典更新序列元素#0的长度为1; 2 是必需的 异常位置:C:\Users\Admin\PycharmProject\mlapp\views.py 在帖子中,第 81 行 Python 可执行文件:C:\Users\Admin\AppData\Local\Programs\Python\Python37\python.exe Python版本:3.7.3 Python 路径:
['C:\Users\Admin\PycharmProject\freed', 'C:\Users\Admin\AppData\Local\Programs\Python\Python37\python37.zip', 'C:\Users\Admin\AppData\Local\Programs\Python\Python37\DLLs', 'C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib', 'C:\Users\Admin\AppData\Local\Programs\Python\Python37', 'C:\Users\Admin\AppData\Roaming\Python\Python37\site-packages', 'C:\Users\Admin\AppData\Local\Programs\Python\Python37\lib\site-packages'] 服务器时间:2019年8月20日星期二13:27:32 +0000

【问题讨论】:

    标签: python django machine-learning


    【解决方案1】:

    错误在这一行:

    coltype = dict([i.split(':', 1) for i in coltype])
    

    并且说其中一个元素(可能是第一个元素)的长度为一。这意味着没有“:”可以拆分。您希望每个元素都采用“a:b”形式,但显然其中一个元素并非如此。

    在假定数据具有某种格式之前,您应该始终对其进行验证。在您的情况下,request.POST.getlist('coltype') 中的元素之一没有您期望的格式。

    【讨论】:

      【解决方案2】:

      你好像想做一本字典

       coltype = dict([i.split(':', 1) for i in coltype])
      
      

      但是要制作字典,您需要两个参数 Key 和 Value。 你只是传递一个论点。可能,这就是为什么,它显示了这个错误。

      【讨论】:

        猜你喜欢
        • 2018-08-18
        • 1970-01-01
        • 2020-06-15
        • 2015-08-18
        • 2017-12-15
        • 2020-09-07
        • 1970-01-01
        • 2018-03-11
        • 2014-02-17
        相关资源
        最近更新 更多