Python Forum
Help with correlation coefficient
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with correlation coefficient
#1
Trying to calculate the correlation coefficient between the two lists, so far no luck. Any suggestions or help would be greatly appreciated?

from table_utils import *
from stats_utils import *

cars = [29.9, 30.2, 30.3, 30.1, 30.3, 30.4, 30.5, 30.9, 31.1, 31.5, 31.7]
papers = [337, 322, 311, 299, 264, 242, 213, 191, 190, 174, 164]

car_list = []
papers_list = []

for car_row in cars:
    area = car_row[0]
    for papers_row in papers:
        if area == papers_row[1]: 
             car_list.append(float(car_row[1]))
             papers.append(float(papers_row[2])) 

corr = round(corr_coef(car_list, papers_list), 2)
print('Correlation coefficient =', corr)
Reply
#2
What is the problem you are facing... Do you get an error? Is the result wrong?
Reply
#3
I assume you're getting an error on line 11. I don't understand what you are trying to do with car_list and papers_list. Why not just run corr_coef on cars and papers?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Maybe this can help you:
Finding-correlation-coefficient-from-2-lists

With this solution the Correlation Coefficient is -0.9068908297699143.
Reply
#5
Sorry this is my first time working with correlation in python. Was just working with the example I had. Errors I kept getting where;

Traceback (most recent call last):
File "C:\Users\User\Desktop\q6b.py", line 4, in <module>
from table_utils import *
ModuleNotFoundError: No module named 'table_utils'

Line 11 error would have shown up in syntax. But guess its good practice to proof read my code before posting. Was thinking I had to write my own functions as the imports failed?

I'll try using example in the link provided, thanks
Reply
#6
These imports in your codes are not being used. You can just remove them.

The link that I suggested implements the solution like this:

def mean(someList):
    total = 0
    for a in someList:
        total += float(a)
    mean = total / len(someList)
    return mean


def standDev(someList):
    listMean = mean(someList)
    dev = 0.0
    for i in range(len(someList)):
        dev += (someList[i] - listMean)**2
    dev = dev**(1 / 2.0)
    return dev


def correlCo(someList1, someList2):

    # First establish the means and standard deviations for both lists.
    xMean = mean(someList1)
    yMean = mean(someList2)
    xStandDev = standDev(someList1)
    yStandDev = standDev(someList2)
    # r numerator
    rNum = 0.0
    for i in range(len(someList1)):
        rNum += (someList1[i] - xMean) * (someList2[i] - yMean)

    # r denominator
    rDen = xStandDev * yStandDev

    r = rNum / rDen
    return r


cars = [29.9, 30.2, 30.3, 30.1, 30.3, 30.4, 30.5, 30.9, 31.1, 31.5, 31.7]
papers = [337, 322, 311, 299, 264, 242, 213, 191, 190, 174, 164]

print(correlCo(cars, papers))
PS: This is not my code, just added your 2 lists.
Reply
#7
Those are not standard modules, you would have to install them before you could import them. There should be a tutorial about how to do that in the tutorials section of this board. Or you could use the example gontajones showed.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
what about scipy: https://docs.scipy.org/doc/scipy-0.14.0/...s.pearsonr
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Different Correlation Coefficents with different Time Ranges giaco__mar 0 855 Sep-28-2022, 02:03 PM
Last Post: giaco__mar
  How to get coefficient of determination R2 after scipy.curve_fit? AlekseyPython 0 1,876 Feb-12-2021, 09:03 AM
Last Post: AlekseyPython
  how to calculate overlaping coefficient between two probablity functions Staph 3 3,746 Aug-11-2019, 08:10 AM
Last Post: Staph
  Cross-correlation between 2 planes in a 3D array Mark3232 2 4,605 May-16-2019, 09:46 AM
Last Post: Mark3232
  do you know a code that will print all correlation values using numpty and panda? crispybluewaffle88 1 2,452 Mar-06-2019, 12:45 PM
Last Post: scidam
  Pandas dataframe: sum of exponentially weighted correlation matrices per row vvvcvvcv 1 3,270 May-29-2018, 01:09 AM
Last Post: scidam
  Mistake by correlation (x, y) Jack_Sparrow 2 2,728 May-10-2018, 02:23 PM
Last Post: volcano63
  Newbie question how to find the coefficient for each variable zydjohn 10 12,181 Dec-14-2017, 05:01 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020