scikit-learn processes simplified | 简化scikit-learn过程

Code light and concept heavy–that’s been the trend as programming languages stack on one another to wrap up more and more algorithms with fewer and fewer lines of code. A child can run a linear regression model. Yes, you hear it right. We have done it, and we summarize the building blocks of Python machine learning library scikit-learn in this table:

Process Code
Import libraries %matplotlib inline

import numpy as np

import pandas as pd

import matplotlib

import matplotlib.pyplot as plt

Format pd.options.display.float_format = ‘{:20,.3f}’.format
Import data from sklearn.datasets import load_boston

dataset = load_boston()

Prepare data y = dataset.target

X = dataset.data

Split data from sklearn.model_selection import train_test_split
Algorithm a from sklearn.linear_model import RidgeCV

my_regr = RidgeCV()

Train-predict my_regr.fit(X_train, y_train)

y_pred =my_regr.predict(X_test)

Plot – performance f, ax =plt.subplots(1, 1)

ax.scatter(y_test, y_pred)

plt.plot([0, 50], [0, 50], ‘–k’)

ax.set_ylabel(‘Target predicted’)

ax.set_xlabel(‘True Target’)

ax.set_title(‘Ridge regression on test data’)

ax.text(5, 40, r’$R^2$=%.2f, MAE=%.2f’ % (

   r2_score(y_test, y_pred), median_absolute_error(y_test, y_pred)))

ax.set_xlim([0, 50])

ax.set_ylim([0, 50])

Interpretation interpretation = pd.DataFrame({‘X’: dataset.feature_names, ‘coef’: my_regr.coef_})
Algorithm b from sklearn.model_selection import cross_val_predict

from sklearn import linear_model

Train-predict my_regr = linear_model.LinearRegression()

y_pred = cross_val_predict(my_regr, X, y, cv=10)

Plot – performance f, ax =plt.subplots(1, 1)

ax.scatter(y, y_pred, edgecolors=(0, 0, 0))

plt.plot([0, 50], [0, 50], ‘–k’)

ax.set_ylabel(‘Target predicted’)

ax.set_xlabel(‘True Target’)

ax.set_title(‘linear regression with cross validation’)

ax.text(5, 45, r’$R^2$=%.2f, MAE=%.2f’ % (

   r2_score(y, y_pred), median_absolute_error(y, y_pred)))

Leave a Reply

Your email address will not be published.