Anomaly Z score Error

The error I dont understand. Please help. @shoeb.ahmed @rahul.singh1

> import conf, json,time, requests, statistics, math
> from boltiot import Sms, Bolt
> 
> def data_get(hdata, frame, factor):
> 
>     if len(hdata)<frame :
>        return None
>     if len(hdata)>frame :
>        del hdata[0:len(hdata-frame)]
> 
>     Mean = statistics.mean(hdata)
> 
>     Variance= 0
> 
>     for data in hdata :
>        Varianace= Variance + math.pow((data-Mean),2)
> 
>     zn = factor * math.sqrt(Variance/frame)
> 
>     hbound = hdata[frame-1]+zn
>     lbound = hdata[frame-1]-zn
>     return[hbound,lbound]
> 
> mybolt = Bolt(conf.api, conf.id)
> sms = Sms(conf.ssid, conf.token, conf.ton, conf.fromn)
> hdata=[]
> 
> while True:
>     response = mybolt.analogRead('A0')
>     data = json.loads(response)
>     if data['success'] != 1:
>          print("There was an error while retriving the data.")
>          print("This is the error:" +data['value'])
>          time.sleep(10)
>          continue
>  print ("This is the value "+data['value'])
>     sensor=0
>     try:
>          sensor = int(data['value'])
>     except e:
>          print("Error. Try again",  e)
>          continue
> 
>     bound = data_get(hdata, conf.frame, conf.factor)
>     if not bound :
>          reqbound= conf.frame -len(hdata)
>          print ("No. of points needed is" , reqbound ,"more points")
>          hdata.append(int (data['value']))
>          time.sleep(10)
>          continue
> 
>     try :
>          if (sensor> bound[0]):
>              print("Light level increased. Sending sms")
>              response = sms.send_sms("Someon turned on the lights")
>              print("REsponse is ", response)
>          elif sensor < bound[1]:
>              print ("The light level decreased suddenly. Sending an SMS")
>              response = sms.send_sms("Someone turned off the lights")
>              print("This is the response ",response)
>          hdata.append(sensor);
>     except Exception as e:
>          print ("Error",e)
>     time.sleep(10)

Hi @ghoshg401,

It seems that you have an error in your code.

The error is for the line

del hdata[0:len(hdata-frame)]

The exception message says that "unsupported operand type(s) for -: ‘list’ and ‘int’

What this basically means that the operation ‘-’ ie subtraction cannot be done with a ‘list’ and an integer.

As can be seen in the code, hdata is a list, and frame is an integer.

After checking the course material, I found that the line is supposed to be

del history_data[0:len(history_data)-frame_size]

In your case, you have used the name hdata in the place of history_data and frame in the place of frame_size.

So your code should have the following line

del hdata[0:len(hdata)-frame]
1 Like

Thank you so much :slight_smile:

1 Like