Mean of Absolute differences in Python - python

I am trying to write a programme in Python 3which calculates the mean of the absolute differences between successive values.

EDIT: As the code was removed from the question, updating answer, moving the issues with code to bottom.
As given in comments you can use enumerate() to get the index as well as element from the array and then use that to calculate the mean. Example -
>>> def absolute_difference(v):
... sum_diff = 0
... for i,x in enumerate(v):
... if i+1 < len(v):
... sum_diff += abs(v[i+1] - x)
... r = sum_diff/len(v)
... return r
...
>>> absolute_difference([4.0, 4.2, 4.1, 4.4, 4.3])
0.1400000000000004
Lots of issues in the code (that you seem to have removed) -
Why are you converting your absolute difference to float ? float mathematics is not accurate , as you can see from the sum of difference in your code - 0.20000000000000018 . In your case you do not need to convert them to float.
The main issue of 0.0 for r occurs because you are using // to divide, // truncates the division to the nearest integer, so diving 7.0 by something grater than that using // would always result in 0.0 . Example -
>>> 7.0 // 8.0
0.0
>>> 7.0/8.0
0.875
For your case, you should divide using / .
You are taking the mean in each iteration of the loop, though that is not an issue , it may not be completely needed. If you do not want to take the mean in each iteration of the loop, you should indent it outside the loop.

You are using // which means integer division in python 3
That is
i.e)
2/4 =0.5
2//4=0
Just replace the // with / when calculating r

Here is another approach:
def absolute_difference(values):
last = values[0]
total = 0
for value in values[1:]:
total += abs(value - last)
last = value
return total/(len(values)-1)
print('{:.5f}'.format(absolute_difference([4.0, 4.2, 4.1, 4.4, 4.3])))
Giving the answer: 0.17500

Also, to prevent None from appearing at the end, you must have return at the end of your definition. This happens if you make another variable "equal to" (=) your definition. This was shown in the other posts, but I'm stating this just to highlight things out.

Related

Why am I getting float division by zero?

I want to find the potential value and optimize it using the minimize function but when I run the codes, I get an error saying float division by zero. Does anyone know what's the problem?
Below are my codes.
def LennardJones(r):
"""
return Potential at input x for a polynomial with input r
"""
Potential = 0
for i in range(len(r)):
Potential += 4 * 0.41 * ((2.4 / i)**12 - (2.4 / i)**6)
return Potential
Answer = minimize(LennardJones, 1)
for i in range(len(r)):
for i in range(...) usually starts with 0.
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default)
At the start, i is 0, and division by zero is not possible.
however, it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6)
I think the 'i' value begins with 0 when you don't specify the range explicitly. You can use
range(1, len(r)), and check if the problem still persists!
Check the documentation for more details.
Maybe you should check why not add a parameter in method LennardJones.

python math on codeacademy acting weird when dividing by int vs float. dynamically typed/cast issues

Ok so the problem asks to find the median(middle number) in a list of numbers. If the list has an even amount of numbers return the average of the two middle numbers.
I came across some code doesn't work on the site but does in PyCharm. I imagine it is because some of the code on code academy's learning python is old (for example their print function and raw_input() are now deprecated)
The below does not work on CodeAcademy
def median(ls):
ls = sorted(ls)
length = len(ls)
median = 0.0
temp = 0
if length % 2 == 0:
temp = int(length / 2)
median = float (ls[temp-1] + ls[temp]) / 2 #why is this a problem?
return median
else:
temp=int((length-1) / 2)
median = ls[temp]
return median
Note: the above returns 4.0 instead of 4.5 when passed [4,5,5,4]
however when I change the /2 to /2.0 like below it works.
def median(ls):
ls = sorted(ls)
length = len(ls)
median = 0.0
temp = 0
if length % 2 == 0:
temp = int(length / 2)
median = float (ls[temp-1] + ls[temp]) / 2.0 #here
return median
else:
temp=int((length-1) / 2)
median = ls[temp]
return median
Note: the above correctly returns 4.5 when passed [4,5,5,4]
So even though I've figured out how to solve the problem, I want to know why it happened in the event that even though both code examples work in newer versions of python, is one is more correct or 'cleaner' and why?
Ok so what I believe happened in the first code example that was returning weird results is that Python casted the first two numbers to ints in order to divide by int (yielding 4 when passed [4,4,5,5] (after sorting)) and then casted that answer
(4) to a float giving 4.0. but when I divided by 2.0 it casted those numbers to floats first giving the correct 4.5. This also allowed me to remove the explicit cast to float and when tested worked on code academy

Convert a scipy interp1d array to a float value in Python

in my current project I have currently a conversion issue:
In a first step I am interpolating two lists with scipy interp1d.
f_speed = scipy.interpolate.interp1d(t, v, 'cubic')
After that I want to get one specific point in this function. This value is now an array from scipy with only one value in it.
currentSpeed = f_speed(tpoint)
# result: array(15.1944)
As a last step I want to calculate other values with the value from the interpolated function, but I only get 0 as a value. There is no Error at all.
real_distance = currentSpeed * (1/15) # some calculations
# result: 0, all results with further calculations are zero
I need a conversion from scipy array to a regular float value to proceed my calculations. Is there any function to do this?
I tried several things like V = currentSpeed[0], or .tolist from numpy (not possible in scipy)...
Thanks for your help in advance!!
You did not specify which Python version you're using, but if you're using Python 2.7, then operator / stands for integer division. It means that 1/15 will produce 0. Multiplying something with this results will end up being 0, regardless of how you access array values.
To solve the problem, make sure at least one operand is a float number.
result1 = 1/15 # Produces 0
result2 = 1.0/15 # Produces 0.06666666666666667
If you apply this to your code, you should use
real_distance = currentSpeed * (1.0/15)
Are you using Python 2? If so the problem is the division.
Integer division will result in an integer so 1/15 will result in 0.
Try 1.0/15 instead. By using 1.0 you make it explicitly a float an then the result will be as expected.

Divide values in Python

I made a xlrd to json parsing script, which doesn't divide variables. It always returns zero... the code which I am using to divide the variables is:
if not row_values[2]:
key['nr_one'] = 0
else:
key['nr_one'] = int(row_values[2])
if not row_values[4]:
key['nr_two'] = 0
else:
key['nr_two'] = int(row_values[4])
try:
key['perc'] = float(key['nr_two']/key['nr_one']*100)
except ZeroDivisionError:
key['perc'] = 0
I have printed the following code at the end of the script:
print('one')
print(key['nr_one'])
print('two')
print(key['nr_two'])
print('perc')
print(key['perc'])
This returns:
one
103386547
two
135680054
perc
0.0
So. I don't understand why the division fails and returns 0? Could someone please help me format a good way to calculate the percentage
it should be float(key['nr_two']) /key['nr_one']*100
Integer division is like this :
1/3 #=> 0
The easiest way to understand this result is not as a fraction, but as the answer to the question "How many times do 3 fit in 1?".
So 0 multiplied by 100 is still :
1/3 * 100 #=> 0
You need
100.0*1/3 #=> 33.333333
For your code :
key['perc'] = 100.0*key['nr_two']/key['nr_one'] (without using float(...))
It returns :
76.1987808465937
NOTE: You really need to type 100.0 (a float), not just 100 (an int). You'd get 76 otherwise.
key['nr_two'] is integer and so is key['nr_one']. So when they are divided, the result is forced to be an integer in Python 2x versions (in Python 3x this limitation does not exist and your code would run just fine). What you therefore get, is 0, which is later multiplied by 100, so still 0 but float, 0.0.
To achieve float division in python 2.x you can just add:
from __future__ import division
This way you will not need to convert your numbers each time you divide them.

Can't figure out how to type this sum in python

This sum keeps giving me he wrong answer.
We were told to give variables values and workout sums and one of them is A×B÷(C×D)
The right answer is 0.0625 and it keeps coming out as 16.
I am just wondering why this is happening and what I am doing wrong.
This is what I wrote in python maybe someone can tell me what I am missing.
print(int(A*B/C*D))
A = 2
B = 4
C = 8
D = 16
I am also confused with this one A^((B + C) × D)
this is how I wrote it in python
(A**(B+C)*D))
Thanks in advance :)
Because * and / have the same precedence. Your formula is interpreted as (((A*B)/C)*D)
Also you should convert your operands to float otherwise you will only get 0 as result.
Firstly, your parentheses are wrong, but that's already been cleared up.
Secondly, I assume you are using Python 2.x, in which integer division returns integers: 1/2 == 0. To fix this, make the numbers floats: A = 2.0; B = 4.0; …
Or use Python 3.x.
As some people said above, your formula is wrong, which should be (AB) / (CD) or AB / (CD) withouth int()
Since you have int() to convert the final result, you will never have float number, if the outcome of operation is from 0 to 1(exclusive), int(outcome) is always 0.
In python2.x, at least one of operands should be float to output float result.
In python3.x, the python interpreter will automatically convert your result to float if needed.

Categories

Resources