Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

random forest algorithm

# Import RandomForestRegressor
from sklearn.ensemble import RandomForestRegressor

# Instantiate rf
rf = RandomForestRegressor(n_estimators=25,
                           random_state=2)
                           
# Fit rf to the training set            
rf.fit(X_train, y_train) 
# Import mean_squared_error as MSE
from sklearn.metrics import mean_squared_error as MSE

# Predict the test set labels
y_pred = rf.predict(X_test)

# Evaluate the test set RMSE
rmse_test = MSE(y_test, y_pred)**(1/2)

# Print rmse_test
print('Test set RMSE of rf: {:.2f}'.format(rmse_test))
# Create a pd.Series of features importances
importances = pd.Series(data=rf.feature_importances_,
                        index= X_train.columns)

# Sort importances
importances_sorted = importances.sort_values()

# Draw a horizontal barplot of importances_sorted
importances_sorted.plot(kind='barh', color='lightgreen')
plt.title('Features Importances')
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: python rounding numbers to n digits 
Python :: control flow in python 
Python :: django pytest how to load data 
Python :: python minimum 
Python :: run python file from cmd 
Python :: vector data 
Python :: python string variable 
Python :: sparse matrix multiplication in python 
Python :: variables in python 
Python :: python looping through a list 
Python :: data encapsulation in python 
Python :: KeyError 
Python :: python list to arguments 
Python :: subarrays in python 
Python :: is the multiply code in python 
Python :: Python RegEx SubString – re.sub() Syntax 
Python :: how to check if variable in python is of what kind 
Python :: generating random numbers numpy 
Python :: pandas drop rows 
Python :: k-means clustering 
Python :: django context data 
Python :: ImportError: No module named pandas 
Python :: Showing all column names and indexes dataframe python 
Python :: python randint with leading zero 
Python :: asyncioevents.py", line 504, in add_reader raise NotImplementedError 
Python :: python nasa api 
Python :: something useless. python 
Python :: Python Program to Find sum Factorial of Number Using Recursion 
Python :: python you bad 
Python :: #Combine two sets on python with for loop: reverse way in one line with space 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =