9951 explained code solutions for 126 technologies


python-scikit-learnSklearn model training example


import numpy as np
from sklearn import datasets, linear_model, model_selection, metrics

X, y = datasets.load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = linear_model.LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

r2 = metrics.r2_score(y_test, y_pred)ctrl + c
from sklearn import

import module from scikit-learn

import numpy

import Numpy module

datasets.load_diabetes

loads sample diabetes database

X, y

loaded features data (X) and target variable (y) values

model_selection.train_test_split

splits given X and y datasets to test (25% of values by default) and train (75% of values by default) subsets

linear_model.LinearRegression

initialize linear regression model

.fit(

train model with a given features and target variable dataset

.predict(

predict target variable based on given features dataset

metrics.r2_score(

calculate R2 score to evaluate model quality (1 is best, 0 is worst)

y_test

target variable values from test dataset (correct values)

y_pred

target variable predicted values by our model (values to evaluate)


Usage example

import numpy as np
from sklearn import datasets, linear_model, model_selection, metrics

X, y = datasets.load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y)

model = linear_model.LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

r2 = metrics.r2_score(y_test, y_pred)

print(r2)
output
0.5225055395659151