Professional Documents
Culture Documents
1.1 Introduction
1.1.1 About This Experiment
The development in this experiment is based on ModelArts. For details about how to set up the
environment, see the HCIA-AI V3.0 Experiment Environment Setup Guide. The sample size of the
dataset used in this case is small, and the data comes from the open source Boston house price data
provided by scikit-learn. The Boston House Price Forecast project is a simple regression model,
through which you can learn some basic usage of the machine learning library sklearn and some basic
data processing methods.
1.1.2 Objectives
Upon completion of this task, you will be able to:
Use the Boston house price data set that is open to the Internet as the model input data.
Build, train, and evaluate machine learning models.
Understand the overall process of building a machine learning model.
Master the application of machine learning model training, grid search, and evaluation
indicators.
Master the application of related APIs.
#Introduce algorithms.
from sklearn.linear_model import RidgeCV, LassoCV, LinearRegression, ElasticNet
#Compared with SVC, it is the regression form of SVM.
from sklearn.svm import SVR
#Integrate algorithms.
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from xgboost import XGBRegressor
Output:
x = pd.DataFrame(boston.data, columns=boston.feature_names)
x.head()
Output:
Output:
Huawei AI Certification Training Lab Guide Page 9
Output:
'Lasso',
'Random Forrest',
'GBDT',
'Support Vector Regression',
'ElasticNet',
'XgBoost']
model_fitted = model.fit(x_train,y_train)
y_pred = model_fitted.predict(x_test)
score = r2_score(y_test, y_pred)
return score
Output:
'''
'kernel': kernel function
'C': SVR regularization factor
'gamma': 'rbf', 'poly' and 'sigmoid' kernel function coefficient, which affects the model performance
'''
Huawei AI Certification Training Lab Guide Page 11
parameters = {
'kernel': ['linear', 'rbf'],
'C': [0.1, 0.5,0.9,1,5],
'gamma': [0.001,0.01,0.1,1]
}
Output:
GridSearchCV(cv=3, error_score='raise',
estimator=SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False),
fit_params={}, iid=True, n_jobs=1,
param_grid={'kernel': ['linear', 'rbf'], 'C': [0.1, 0.5, 0.9, 1, 5], 'gamma': [0.001, 0.01, 0.1, 1]},
pre_dispatch='2*n_jobs', refit=True, return_train_score=True,
scoring=None, verbose=0)
Output:
##Perform visualization.
ln_x_test = range(len(x_test))
y_predict = model.predict(x_test)
#Display in a diagram.
plt.legend(loc ='upper left')
plt.grid(True)
plt.title(u"Boston Housing Price Forecast (SVM)")
Huawei AI Certification Training Lab Guide Page 12
plt.xlim(0, 101)
plt.show()
Output:
1.3 Summary
This chapter describes how to build a Boston house price regression model based on sklearn, including
importing, segmenting, and standardizing data, defining models, and setting hyperparameters, and
provides trainees with a basic concept of machine learning model building.