14

I have to select those symptoms which is in the dictionary with the symptoms already posted.It works fine.But for some symptoms typeError is showing in command prompt and also all are getting printed in command prompt but not in html page. Here is my code

views.py

def predict(request):
sym=request.POST.getlist('symptoms[]')
sym=list(map(int,sym))
diseaseArray=[]
diseaseArray=np.array(diseaseArray,dtype=int)
dictArray=[]
for dicti in dictionary:
    if (set(sym)<= set(dicti['symptoms']) and len(sym)!= 0) or [x for x in sym if x in dicti['primary']]:
        diseaseArray=np.append(diseaseArray,dicti['primary'])
        diseaseArray=np.append(diseaseArray,dicti['symptoms'])
diseaseArray=list(set(diseaseArray))
print(diseaseArray)
for i in diseaseArray:
    if i not in sym:
        dict={'id':i}
        dictArray.append(dict)
        print(dictArray)
for j in dictArray:
    symptoms=Symptom.objects.get(syd=j['id'])
    j['name']=symptoms.symptoms
    print(j['name'])
print(len(dictArray))
return JsonResponse(dictArray,safe=False)

template

$('.js-example-basic-multiple').change(function(){
  $('#suggestion-list').html('');
  $('#suggestion').removeClass('invisible');

  $.ajax({
  url:"/predict",
  method:"post",
  data:{
    symptoms: $('.js-example-basic-multiple').val(),
  },
  success: function(data){

      data.forEach(function(disease){
      console.log(disease.name)
        $('#suggestion-list').append('<li>'+disease.name+'<li>')
        $('#suggestion-list').removeClass('invisible');
    });



  }

  });

3 Answers 3

12

The type of each element in diseaseArray is a np.int32 as defined by the line:

diseaseArray=np.array(diseaseArray,dtype=int)  # Elements are int32

int32 cannot be serialized to JSON by the JsonResponse being returned from the view.

To fix, convert the id value to a regular int:

def predict(request):
    ...
    for i in diseaseArray:
        if i not in sym:
            dict={'id': int(i)}  # Convert the id to a regular int
            dictArray.append(dict)
            print(dictArray)
    ...
1
  • I fixed my code.But I deleted that dtype and worked fine
    – najmath
    Commented Apr 12, 2018 at 13:43
7

Instead of manually casting the values to ints as the accepted answer suggests, you can usually let numpy do that for you.

Instead of calling

diseaseArray=list(set(diseaseArray))

You can call

diseaseArray=diseaseArray.unique().tolist()

This should automatically convert any numpy-specific datatypes in the array to normal Python datatypes. In this case it will cast int32 to int, but it also supports other conversions.

Additionally, using numpys .unique() might give some speedup for large datasets.

0

You seem to be trying to save non JSON serializable objects. If you want to save specific objects for later use I'd recommend you to use pickle. https://docs.python.org/3/library/pickle.html

2
  • can you please explain it?which one have to be saved using pickle?dictionary is saved using pickle.
    – najmath
    Commented Apr 12, 2018 at 12:18
  • 1
    Some of the datatypes that you are trying to save into JSON are not default python builtin datatypes. like int32 , If you want to save your objects in any format, so that you can read it the next time around. You can use pickle.dump instead of json.dump
    – Kenstars
    Commented Apr 12, 2018 at 12:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.