- 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
推薦閱讀
- Canvas LMS Course Design
- Getting Started with Oracle SOA B2B Integration:A Hands-On Tutorial
- Natural Language Processing Fundamentals
- 離散事件系統建模與仿真
- STM32G4入門與電機控制實戰:基于X-CUBE-MCSDK的無刷直流電機與永磁同步電機控制實現
- AI 3.0
- Android游戲開發案例與關鍵技術
- Learn CloudFormation
- Lightning Fast Animation in Element 3D
- LMMS:A Complete Guide to Dance Music Production Beginner's Guide
- Hands-On Data Warehousing with Azure Data Factory
- Mastering Geospatial Analysis with Python
- 會聲會影X4中文版從入門到精通
- 人工智能:智能人機交互
- Puppet 3 Beginner’s Guide