当前位置: 首页 > 知识库问答 >
问题:

陷入机器学习应用的数据预处理代码

卫劲
2023-03-14

我目前正在开发一个机器学习应用程序。请在此代码中帮助我 - 当我上传大数据集时,我遇到了一个错误。

代码如下:

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

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

/预处理/字典更新序列元素#0处的ValueError的长度为1;2是必需的请求方法:POST请求URL:http://127 . 0 . 0 . 1:8000/preprocessing/Django版本:2.2.4异常类型:ValueError异常值:< br >字典更新序列元素#0的长度为1;2是必需的异常位置:C:\ Users \ Admin \ pycharm project \ ml app \ views . py in post,第81行Python可执行文件:C:\ Users \ Admin \ AppData \ Local \ Programs \ Python \ Python 37 \ Python . exe Python版本:3.7.3 Python路径:< br >[' C:\ Users \ Admin \ pycharm project \ freed ',' C:\ Users \ Admin \ AppData \ Local \ Programs \ Python 37 \ Python 37 . zip ',' C:\ Users \ Admin \ AppData \ Local \ Programs \ Python \ Python \ Programs \ Python \ Python

共有2个答案

夏弘义
2023-03-14

你好像想编一本字典

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

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

戚宏浚
2023-03-14

错误位于此行:

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

并且表示其中一个元素(可能是第一个)的长度为1。这意味着没有“:”可以拆分。你期望每个元素都是“a: b”的形式,但显然其中一个不是这种情况。

在假定数据具有特定格式之前,应始终对其进行验证。在您的情况下,是请求中的元素之一。POST.getlist(“coltype”)没有您期望的格式。

 类似资料:
  • 机器学习无疑是当前数据分析领域的一个热点内容。很多人在平时的工作中都或多或少会用到机器学习的算法。

  • 机器学习原理

  • 从sklearn加载流行数字数据集。数据集模块,并将其分配给可变数字。 分割数字。将数据分为两组,分别命名为X_train和X_test。还有,分割数字。目标分为两组Y_训练和Y_测试。 提示:使用sklearn中的训练测试分割方法。模型选择;将随机_状态设置为30;并进行分层抽样。使用默认参数,从X_序列集和Y_序列标签构建SVM分类器。将模型命名为svm_clf。 在测试数据集上评估模型的准确

  • 本文向大家介绍Python----数据预处理代码实例,包括了Python----数据预处理代码实例的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Python数据预处理的具体代码,供大家参考,具体内容如下 1.导入标准库 2.导入数据集 3.缺失数据 4.分类数据 5.将数据集分为训练集和测试集 6.特征缩放 7.数据预处理模板 (1)导入标准库 (2)导入数据集 (3)缺失和分类很

  • Kubernetes 在大数据与机器学习中的实践案例。

  • Data Preparation You must pre-process your raw data before you model your problem. The specific preparation may depend on the data that you have available and the machine learning algorithms you want