django - 带有机器学习的 Django 给出了这个错误 Found array with 0 features(s) (shape=(1, 0)) 而至少需要 1
问题描述
视图.py
from django.shortcuts import render
from .models import questions
from .serializers import approvalSerializers
from django.http import JsonResponse
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets
from rest_framework.decorators import api_view
import os
from avasyu import settings
import pickle
import requests
import numpy as np
from django.core.cache import cache
# Create your views here.
data = []
def landing_views(request):
return render(request, "avasyuapp/landing.html")
def ques_views(request):
cache.clear()
return render(request, "avasyuapp/ques.html")
def store_db(request):
if request.method == "POST":
ans1 = request.POST["answer1"]
ans2 = request.POST["answer2"]
ans3 = request.POST["answer3"]
ans4 = request.POST["answer4"]
ans5 = request.POST["answer5"]
ans6 = request.POST["answer6"]
ans7 = request.POST["answer7"]
ans8 = request.POST["answer8"]
ans9 = request.POST["answer9"]
data.append(ans1)
data.append(ans2)
data.append(ans3)
data.append(ans4)
data.append(ans5)
data.append(ans6)
o1 = questions(
ques='Have you motivated yourself to become a good communicator?', ans=ans1)
o1.save()
o2 = questions(
ques='Can you speak in front of group without any nervousness?', ans=ans2)
o2.save()
o3 = questions(
ques='Can you justify you as a good communicator?', ans=ans3)
o3.save()
o4 = questions(
ques='Are you really happy to make communication as your future passion?', ans=ans4)
o4.save()
o5 = questions(
ques='Is your english vocabulary and comprehension strong?', ans=ans5)
o5.save()
o6 = questions(ques='Are you good at grammar?', ans=ans6)
o6.save()
o7 = questions(
ques='Have you achieved anything till date as a good communicator ?', ans=ans7)
o7.save()
o8 = questions(
ques='Are you a good listener,good reader and are you clear in your communication when communicating with others ?', ans=ans8)
o8.save()
o9 = questions(ques='Are you spending any time on reading ?', ans=ans9)
o9.save()
print("Stored in DB..")
# print(data)
cache.clear()
return render(request, 'avasyuapp/result.html')
else:
return redirect('ques/')
class ApprovalsView(viewsets.ModelViewSet):
queryset = questions.objects.all()
serializer_class = approvalSerializers
answer = []
def input_con(ans):
for i in range(len(data)):
if ans[i] == 'Yes':
answer.insert(i, 3)
elif ans[i] == 'Partially Yes':
answer.insert(i, 2)
elif ans[i] == 'Partially No':
answer.insert(i, 1)
else:
answer.insert(i, 0)
return answer
def output_con(classifier):
if classifier == 3:
result = 'Yes'
elif classifier == 2:
result = 'Partially Yes'
elif classifier == 1:
result = 'Partially No'
else:
result = 'No'
return result
@api_view(["POST"])
def approvereject(request):
files = os.path.join(settings.MODELS, 'classifierfinal.pkl')
with open(files, 'rb') as file:
classifier = pickle.load(file)
#classifier = joblib.load(file)
converted_data = input_con(data)
# print(data)
output = classifier.predict([converted_data])
print(output)
dis_out = output_con(output)
print(dis_out)
cache.clear()
return render(request, 'avasyuapp/output.html', {'output': dis_out})
最初,当我将代码推送到 repo 时,我得到了预期的输出,但是当我重新加载应用程序时,我得到 0 个特性或特性在每次运行后加倍。重新加载应用程序后,我收到此错误,我需要再次将其推送到 heroku 以获取输出。如何在每次提交后清除缓冲区并停止收到此 0 个功能错误。
ValueError at /output/
Found array with 0 feature(s) (shape=(1, 0)) while a minimum of 1 is required.
Request Method: POST
Request URL: https://avasyu.herokuapp.com/output/
Django Version: 3.0.4
Exception Type: ValueError
Exception Value:
Found array with 0 feature(s) (shape=(1, 0)) while a minimum of 1 is required.
Exception Location: /app/.heroku/python/lib/python3.6/site-packages/sklearn/utils/validation.py in check_array, line 594
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.6.10
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python36.zip',
'/app/.heroku/python/lib/python3.6',
'/app/.heroku/python/lib/python3.6/lib-dynload',
'/app/.heroku/python/lib/python3.6/site-packages']
Server time: Thu, 12 Mar 2020 19:45:41 +0000
解决方案
我知道的第一件事是 Django 不是机器学习框架,所以我不确定您在这里所说的 ML 是什么意思。
我无法真正遵循您的代码在做什么,但是
找到具有 0 个特征的数组 (shape=(1, 0)),而至少需要 1 个。
貌似来自 Scikit Learn,那一定是你使用的 ML 框架?这个问题解决了这个错误 ValueError: Found array with 0 feature(s) (shape=(546, 0)) while a minimum of 1 is required
推荐阅读
- ios - 以编程方式触发呼吸应用程序或 HRV 测量 [Apple Watch 4]
- mysql - 在 SQL 中查找和替换或删除部分字符串
- haproxy - haproxy 配置将数据和健康检查路由到不同的 IP 地址
- azure-devops - Azure Agent.Listener - 配置挂起
- sql-server - Visual Studio 2019 - SSDT
- java - java - 如何在java中的arrayList中添加JTree?
- swift - UI中的数据更新延迟8秒
- excel - 如何在行中复制字符串的一部分
- mysql - Mysql查询以获取恰好在1天前创建的记录
- spring-boot - 如何设置一个 Spring Boot 验证器,它可以使用 2x 传递的项目,但要求是任一值之一?