May I know how to modify my Python programming thus it will be get the same result as refer to the image file?

import pandas
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

def load_pkl(gender_36):
    with open('gender_36.pkl', 'rb') as file:
    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("%2d) %-*s %f" % (f + 1, 30, feat_labels[indices[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]])

Please see the image file -

enter image description here 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

runfile('C:/Users/HSIPL/Desktop/New f/a i/Homework 9 draft.py', wdir='C:/Users/HSIPL/Desktop/New f/a i')
Traceback (most recent call last):

  File "C:\Users\HSIPL\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-1-e53e7077ed93>", line 1, in <module>
    runfile('C:/Users/HSIPL/Desktop/New f/a i/Homework 9 draft.py', wdir='C:/Users/HSIPL/Desktop/New f/a i')

  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/New f/a i/Homework 9 draft.py", line 12
    return df
IndentationError: expected an indented block

Please help me on this case


You just need to add a “return df” as the last line in your “load_pkl” function. That way when you call the load_pkl() it returns the data frame. It should work after that.

  • $\begingroup$ The new error message is - IndentationError: expected an indented block $\endgroup$ – vokoyo May 2 '19 at 20:19
  • $\begingroup$ @vokoyo you changed how you wrote the function. But if you use that method you have to first add the implementation for the with block then make sure the return fg is on the same alignment as the with line $\endgroup$ – gbdata May 2 '19 at 20:26
  • $\begingroup$ I cannot understand your reply - could you please write the sample code as a reference because of it is good for understanding $\endgroup$ – vokoyo May 2 '19 at 20:47
  • $\begingroup$ Indent error means that you have a code sequence for which something like „tab“ is expected. Eg. the „tab“ after opening a loop. Check your code. This is about bad code, not about data science. $\endgroup$ – Peter Jun 1 '19 at 22:17
  • $\begingroup$ docs.python.org/2.0/ref/indentation.html $\endgroup$ – Peter Jun 1 '19 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.