Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error in method _scores_to_accuracy() of Matcher.py #23

Closed
caixiaocherry opened this issue Oct 3, 2019 · 26 comments
Closed

error in method _scores_to_accuracy() of Matcher.py #23

caixiaocherry opened this issue Oct 3, 2019 · 26 comments

Comments

@caixiaocherry
Copy link

Got error when i tried to run the example at:
m.fit_scores(balance=True, nmodels=10)
When the function calls the static method _scores_to_accuracy(), got error of mis matching size.
In this function, y is a DataFrame with shape as (n, 1), while preds is a list. I fixed the code by convert preds to a matrix

def _scores_to_accuracy(m, X, y):
    preds = [1.0 if i >= .5 else 0.0 for i in m.predict(X)]
    return (y == preds).sum() * 1.0 / len(y)

def _scores_to_accuracy(m, X, y):
    preds = [1.0 if i >= .5 else 0.0 for i in m.predict(X)]
    # return (y == preds).sum() * 1.0 / len(y)
    return (y.to_numpy().T == preds).sum() * 1.0 / len(y)

The code above works for me.

@xiaolinzhuo
Copy link

I found the same problem! I fixed it with
return (y.values == preds).sum() * 1.0 / len(y)

@swyoon
Copy link

swyoon commented Oct 6, 2019

The solution by @caixiaocherry worked for me, but @xiaolinzhuo 's didn't.

@tszumowski
Copy link

@caixiaocherry 's solution worked for me. This was with these versions:

pandas==0.25.1
-e git+git@github.com:benmiroglio/pymatch.git@982778f3fe438f6d6b2905472a3951722edca266#egg=pymatch

@caixiaocherry it may be worth making a PR given multiple people had this issue?

@tszumowski
Copy link

Actually on additional inspection, this seems similar to
#12

@diogoalvesderesende
Copy link

Hey everyone, I am quite new to python and am having this issue as well. Can anyone tell me how to make the changes that @caixiaocherry suggested? I tried to google it but to no avail. Thanks a lot!

@caixiaocherry
Copy link
Author

@diogoalvesderesende , you only need to comment out the following return statement:
return (y == preds).sum() * 1.0 / len(y)
to following return statement:
return (y.to_numpy().T == preds).sum() * 1.0 / len(y)

This shall solve the problem.

@w2998
Copy link

w2998 commented May 16, 2020

@caixiaocherry 's solution did not for me, it returned error Fitting Models on Balanced Samples: 1\100Error: 'DataFrame' object has no attribute 'to_numipy' While. @xiaolinzhuo 's solution works, but the average accuracy seems wrong, it returned value, which was greater than 1. I was using lending club's data as this article https://medium.com/@bmiroglio/introducing-the-pymatch-package-6a8c020e2009.

@caixiaocherry
Copy link
Author

caixiaocherry commented May 16, 2020 via email

@w2998
Copy link

w2998 commented May 16, 2020

Oh, yes. that was a typo. Now, it works, and results make sense.

@brendachang12
Copy link

I tried both
return (y.values == preds).sum() * 1.0 / len(y)
return (y.to_numpy().T == preds).sum() * 1.0 / len(y)
in the Matcher.py file but neither of them worked. Is there another way to get around this error?

This is the error I'm receiving:
Fitting Models on Balanced Samples: 1\100Error: Unable to coerce to Series, length must be 1: given 4484
Fitting Models on Balanced Samples: 1\100Error: Unable to coerce to Series, length must be 1: given 4484
Fitting Models on Balanced Samples: 1\100Error: Unable to coerce to Series, length must be 1: given 4484
Fitting Models on Balanced Samples: 1\100Error: Unable to coerce to Series, length must be 1: given 4484
Fitting Models on Balanced Samples: 1\100Error: Unable to coerce to Series, length must be 1: given 4484

Average Accuracy: nan%

@caixiaocherry
Copy link
Author

Unable to coerce to Series, length must be 1

Could you print out y and preds to check the shape? It seems the broadcast failed.

@PeterOrmosi
Copy link

@diogoalvesderesende , you only need to comment out the following return statement:
return (y == preds).sum() * 1.0 / len(y)
to following return statement:
return (y.to_numpy().T == preds).sum() * 1.0 / len(y)

This shall solve the problem.

this worked for me fine, thanks

@HongruZhai
Copy link

@caixiaocherry 's solution works like a charm. Thanks!

tklimonova added a commit to hyper-island-data-analyst/pymatch that referenced this issue Sep 21, 2020
Fixed the error from this issue benmiroglio#23.
@RishabhArora90
Copy link

I am facing the same issue. Can anyone please explain how to change in the source code?

@caixiaocherry
Copy link
Author

@RishabhArora90 , i think this bug had been patched, so you might only need to re install the package?

batra-akshita added a commit to batra-akshita/pymatch that referenced this issue Aug 3, 2021
Edited based on the issue: benmiroglio#23
@ziadzee
Copy link

ziadzee commented Aug 9, 2021

Hello,

I am currently having this issue although I have added @caixiaocherry solution to the source code:

    @staticmethod
    def _scores_to_accuracy(m, X, y):
        preds = [1.0 if i >= .5 else 0.0 for i in m.predict(X)]
        # return (y == preds).sum() * 1.0 / len(y)
        return (y.to_numpy().T == preds).sum() * 1.0 / len(y)

Am I missing something? Do I need to downgrade pandas?

Thanks

@adriennekline
Copy link

adriennekline commented Sep 3, 2021

I made the recommended change but now I am getting an accuracy of 219400.0%. The error was stated as 'Static column dropped: resultError: Perfect separation detected, results not available.

@CarlosDullius
Copy link

I found the same problem! I fixed it with
return (y.values == preds).sum() * 1.0 / len(y)

It worked for me. I have installed the package today

@medcharleslaidi
Copy link

I installed pymatch and I also getting this error. I understand that I need to change the pymatch code. I downloaded the package using pip install pymatch. Could someone point me a tutorial to help me change the code and use this function ? Thank you very much

@CarlosDullius
Copy link

CarlosDullius commented Feb 7, 2022

Go on
C:\Users{YOURUSER}\AppData\Local\Programs\Python\Python36\Lib\site-packages\pymatch
or if you use anaconda
C:\Users{YOURUSER}\Anaconda3\Lib\site-packages\pymatch
And edit into Matcher.py

return (y == preds).sum() * 1.0 / len(y)
to following statement:
return (y.to_numpy().T == preds).sum() * 1.0 / len(y)

PS: I think linux are the same .../Python36/Lib/site-packages/pymatch

@kelleyjbrady
Copy link

I am encountering this error in April 2022, but I can see that Matcher.py has already been fixed in lines: 523-526 to:

@staticmethod
  def _scores_to_accuracy(m, X, y):
      preds = [[1.0 if i >= .5 else 0.0 for i in m.predict(X)]]
      return (y.to_numpy().T == preds).sum() * 1.0 / len(y)

Is there another solution to this bug?

@philffm
Copy link

philffm commented Apr 6, 2022

@kelleyjbrady same error here - also in April 2022 🤷🏽‍♂️

@CarlosDullius
Copy link

@kelleyjbrady and @philffm follow my tutorial above, it will solve your problem I am sure.
They din't fixed it in

master/build/lib/pymatch/Matcher.py
@kelleyjbrady you probably are looking to

master/pymatch/Matcher.py

But when you install it with PIP, then you will get from the build folder.

@kelleyjbrady
Copy link

kelleyjbrady commented Apr 15, 2022

Thanks @CarlosDullius I will check it out. I ended up using a propensity matching package that is being presented in July at EMBC 2022, the author has written a blog post on Medium and uploaded to PyPi, but the author (@adriennekline) hasn’t updated the github page yet. I think anyone who has followed this thread all the way down here will be able to figure out how to use the package despite current lack of extensive documentation (the PyPi page has a 'quick start'). It was pretty easy to get it up and running on a simple age+sex match I was working on. @philffm may be interested.

@Kwakyejin
Copy link

@staticmethod
def _scores_to_accuracy(m, X, y):
    preds = [1.0 if i >= .5 else 0.0 for i in m.predict(X)]
    # return (y == preds).sum() * 1.0 / len(y)
    return (y.to_numpy().T == preds).sum() * 1.0 / len(y)

@Ayeshasaeedhaq
Copy link

Ayeshasaeedhaq commented Jan 13, 2023

Has anyone updated the code in the package? or has anyone created a clone with corrected code? I am not sure if I am able to correct the code at my end. Because now I am getting the following error

'bool' object has no attribute 'sum'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests