May I know how to modify my Python programming thus it will be get the same result as refer to the image file?
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas
def load_pkl():
df = pandas.read_pickle('gender_36.pkl', 'rb')
return df
feat_labels = load_pkl.columns[1:]
forest = RandomForestClassifier(n_estimators=1000, random_state=2, n_jobs=-1)
forest.fit(X_train, y_train)
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
for f in range(X_train.shape[1]):
print(f + 1, 30, feat_labels[f], importances[indices[f]])
plt.title('Feature Importances')
plt.bar(range(X_train.shape[1]), importances[indices], color='lightblue', align='center')
plt.xticks(range(X_train.shape[1]), feat_labels[indices], rotation=90)
plt.xlim([-1, X_train.shape[1]])
plt.tight_layout()
plt.show()Please see the image file -
![[Image: r0YeY.jpg]](https://i.stack.imgur.com/r0YeY.jpg)
The error message is -
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 6.5.0 -- An enhanced Interactive Python.
C:\Users\HSIPL\Anaconda3\lib\site-packages\ipykernel\parentpoller.py:116: UserWarning: Parent poll failed. If the frontend dies,
the kernel may be left running. Please let us know
about your system (bitness, Python, etc.) at
[email protected]
[email protected]""")
runfile('C:/Users/HSIPL/Desktop/Homework 9 draft.py', wdir='C:/Users/HSIPL/Desktop')
C:\Users\HSIPL\Anaconda3\lib\site-packages\sklearn\ensemble\weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
from numpy.core.umath_tests import inner1d
Traceback (most recent call last):
File "<ipython-input-1-53490d045156>", line 1, in <module>
runfile('C:/Users/HSIPL/Desktop/Homework 9 draft.py', wdir='C:/Users/HSIPL/Desktop')
File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
execfile(filename, namespace)
File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/HSIPL/Desktop/Homework 9 draft.py", line 12, in <module>
feat_labels = load_pkl.columns[1:]
AttributeError: 'function' object has no attribute 'columns'Please help me on this case
Attached Files
Thumbnail(s)
Assessing Feature with Random Forests.pdf (Size: 163.92 KB / Downloads: 509)
