text_plain_output_2.png text_plain_output_1.png
from sklearn.compose import ColumnTransformer

from sklearn.feature_selection import RFE

from sklearn.linear_model import LogisticRegression

from sklearn.preprocessing import OneHotEncoder

from sklearn.preprocessing import StandardScaler

from sklearn.tree import DecisionTreeClassifier

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

data = pd.read_csv('../input/heart-failure-prediction/heart.csv')

data.shape

data.nunique()

data.dropna(inplace=True)
data.isnull().sum()

data.shape

X = data.iloc[:, :-1]
y = data.iloc[:, -1]

ct1 = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [1, 2, 6, 8, 10])], remainder='passthrough')

X = ct1.fit_transform(X)

X = pd.DataFrame(X)

model = LogisticRegression()
rfe = RFE(model, n_features_to_select=5)
fit = rfe.fit(X, y)

fit.n_features_

features = fit.transform(X)

d = pd.DataFrame(features)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler(with_mean=False)

X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion='entropy', random_state=0)
classifier.fit(X_train, y_train)

y_pred = classifier.predict(X_test)

y_test = np.array(y_test)
y_pred = np.array(y_pred)

import matplotlib.pyplot as plt
importance = classifier.feature_importances_
for i, v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i, v))
plt.bar([x for x in range(len(importance))], importance)
plt.show()