- 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
推薦閱讀
- Design for the Future
- 精通MATLAB神經(jīng)網(wǎng)絡(luò)
- Seven NoSQL Databases in a Week
- Windows程序設(shè)計與架構(gòu)
- 大學(xué)計算機(jī)應(yīng)用基礎(chǔ)
- 數(shù)據(jù)庫系統(tǒng)原理及應(yīng)用教程(第5版)
- 完全掌握AutoCAD 2008中文版:機(jī)械篇
- 傳感器與新聞
- 運(yùn)動控制系統(tǒng)
- Red Hat Linux 9實(shí)務(wù)自學(xué)手冊
- 氣動系統(tǒng)裝調(diào)與PLC控制
- 深度學(xué)習(xí)與目標(biāo)檢測
- 智能制造系統(tǒng)及關(guān)鍵使能技術(shù)
- 智能+:制造業(yè)的智能化轉(zhuǎn)型
- 網(wǎng)絡(luò)信息安全項目教程