'How to show best correlation result in dataframe?

I'd like to show the highest correlation results for my target variable. However, it creates such matrices for me, and I would like these results to be shown from the largest to the smallest in the table. It's best to add some plot. How to do it ? I would like the results to be For SalePrice only, not the table below

korelacja = train.corr()
sns.heatmap(korelacja, vmax=0.9, square=True,cmap='coolwarm')
a = korelacja[korelacja['SalePrice']>0.3]
a

enter image description here



Solution 1:[1]

Your program has the following problems:

  1. There is a problem with the definition of your decorator function. When it is used in @continue_exception(key="foo", value="bar"), there is no func parameter, it will only receive the positional parameters you pass in. Because at this time it is executed first, and then the decoration function is performed.

  2. continue_exception is an instance method, you can't use it as a decorator unless you can pass an object to it. So you can make it a static method, yes, you can, because self is not used inside the function.

The following code fixes the above issue and runs successfully.

import functools
class Rtest(object):

    def __init__(self, *args, **kwargs):
        self.key1 = "foo"
        self.value1 = "bar"


    @staticmethod
    def continue_exception(**kwargs):
        def wrapper(func):
            @functools.wraps(func)
            def inner_function(*args, **kwargs):
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    import traceback
                    print(f"Error with {kwargs.get('key')} :  {kwargs.get('value')}")
                    print(f"Exception {traceback.format_exc()}")
                    return
            return inner_function
        return wrapper

    def add1(self, n1, n2):
        return n1 + n2

    @continue_exception(key="foo", value="bar")
    def add2(self, n1, n2):
        return n1 + n2


obj = Rtest()

print(obj.add1(4,5))

print(obj.add2("fdfd", 6))

Output:

9
Error with None :  None
Exception Traceback (most recent call last):
  File "/home/wujun/Projects/code_test/test.py", line 15, in inner_function
    return func(*args, **kwargs)
  File "/home/wujun/Projects/code_test/test.py", line 29, in add2
    return n1 + n2
TypeError: can only concatenate str (not "int") to str

None

If you want to get the values of key1 and value1 in real time, you can make the following modifications.

import functools
class Rtest(object):

    def __init__(self, *args, **kwargs):
        self.key1 = "foo"
        self.value1 = "bar"


    @staticmethod
    def continue_exception(func):
        @functools.wraps(func)
        def inner_function(*args, **kwargs):
            self, *_ = args
            try:
                return func(*args, **kwargs)
            except Exception as e:
                import traceback
                print(f"Error with {self.key1} :  {self.value1}")
                print(f"Exception {traceback.format_exc()}")
                return
        return inner_function

    def add1(self, n1, n2):
        return n1 + n2

    @continue_exception
    def add2(self, n1, n2):
        return n1 + n2


obj = Rtest()

print(obj.add1(4,5))

print(obj.add2("fdfd", 6))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1