How can I check the correlation between continuous and categorical variables in python?

I have a dataset that includes categorical variables (binary) and continuous variables. I am trying to apply a linear regression model to predict a continuous variable. Can someone please let me know how to check the correlation between categorical variables and continuous target variable.

Current code:

import pandas as pd
df_hosp = pd.read_csv('C:\Users\LAPPY-2\Desktop\LengthOfStay.csv')

data = df_hosp[['lengthofstay', 'male', 'female', 'dialysisrenalendstage', 'asthma', \
              'irondef', 'pneum', 'substancedependence', \
              'psychologicaldisordermajor', 'depress', 'psychother', \
              'fibrosisandother', 'malnutrition', 'hemo']]
print data.corr()

      

All variables, except duration, are categorical. Should this work?

+3


source to share


1 answer


Convert your categorical variable to dummy variables here and put your variable in numpy.array. For example:

data.csv

age,size,color_head
4,50,black
9,100,blonde
12,120,brown
17,160,black
18,180,brown

      

Retrieving data:

import numpy as np
import pandas as pd

df = pd.read_csv('data.csv')

      

DF:

df

Convert categorical variable color_head

to dummy variables:

df_dummies = pd.get_dummies(df['color_head'])
del df_dummies[df_dummies.columns[-1]]
df_new = pd.concat([df, df_dummies], axis=1)
del df_new['color_head']

      

df_new:



df_new

Put this in a numpy array:

x = df_new.values

      

Calculate the correlation:

correlation_matrix = np.corrcoef(x.T)
print(correlation_matrix)

      

Output:

array([[ 1.        ,  0.99574691, -0.23658011, -0.28975028],
       [ 0.99574691,  1.        , -0.30318496, -0.24026862],
       [-0.23658011, -0.30318496,  1.        , -0.40824829],
       [-0.28975028, -0.24026862, -0.40824829,  1.        ]])

      

See:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.corrcoef.html

+5


source







All Articles