- Ensemble Machine Learning Cookbook
- Dipayan Sarkar Vijayalakshmi Natarajan
- 237字
- 2021-07-02 13:21:56
How to do it...
We have a dataset that is based on the properties of wines. Using this dataset, we'll build multiple regression models with the quality as our response variable. With multiple learners, we extract multiple predictions. The averaging technique would take the average of all of the predicted values for each training sample:
- Import the required libraries:
# Import required libraries
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
- Create the response and feature sets:
# Create feature and response variable set
from sklearn.cross_validation import train_test_split
# create feature & response variables
feature_columns = ['fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar','chlorides', 'free sulfur dioxide', 'total sulfur dioxide','density', 'pH', 'sulphates', 'alcohol']
X = df_winedata[feature_columns]
Y = df_winedata['quality']
- Split the data into training and testing sets:
# Create train & test sets
X_train, X_test, Y_train, Y_test = \
train_test_split(X, Y, test_size=0.20, random_state=1)
- Build the base regression learners using linear regression, SVR, and a decision tree:
# Build base learners
linreg_model = LinearRegression()
svr_model = SVR()
regressiontree_model = DecisionTreeRegressor()
# Fitting the model
linreg_model.fit(X_train, Y_train)
svr_model.fit(X_train, Y_train)
regressiontree_model.fit(X_train, Y_train)
- Use the base learners to make a prediction based on the test data:
linreg_predictions = linreg_model.predict(X_test)
svr_predictions = svr_model.predict(X_test)
regtree_predictions = regressiontree_model.predict(X_test)
- Add the predictions and divide by the number of base learners:
# We divide the summation of the predictions by 3 i.e. number of base learners
average_predictions=(linreg_predictions + svr_predictions + regtree_predictions)/3
推薦閱讀
- 課課通計算機原理
- VMware Performance and Capacity Management(Second Edition)
- Hands-On Cybersecurity with Blockchain
- 可編程控制器技術應用(西門子S7系列)
- 悟透JavaScript
- Word 2007,Excel 2007辦公應用融會貫通
- R Data Analysis Projects
- Cloud Security Automation
- Hands-On Dashboard Development with QlikView
- Linux Shell Scripting Cookbook(Third Edition)
- 深度學習原理與 TensorFlow實踐
- 與人共融機器人的關節力矩測量技術
- 暗戰強人:黑客及反黑客工具快速精通
- Mastering Microsoft Dynamics 365 Customer Engagement
- Learn SOLIDWORKS 2020