我正在使用Python中的RandomForestRegressor,我想创建一个图表来说明功能重要性的排名。这是我使用的代码:
from sklearn.ensemble import RandomForestRegressor
MT= pd.read_csv("MT_reduced.csv")
df = MT.reset_index(drop = False)
columns2 = df.columns.tolist()
# Filter the columns to remove ones we don't want.
columns2 = [c for c in columns2 if c not in["Violent_crime_rate","Change_Property_crime_rate","State","Year"]]
# Store the variable we'll be predicting on.
target = "Property_crime_rate"
# Let’s randomly split our data with 80% as the train set and 20% as the test set:
# Generate the training set. Set random_state to be able to replicate results.
train2 = df.sample(frac=0.8, random_state=1)
#exclude all obs with matching index
test2 = df.loc[~df.index.isin(train2.inde