Skip to content

Machine Learning scikit-learn API

scikit-learn provides a unified fit/predict API covering preprocessing, classification, regression and model evaluation.

1 class · 8 methods

sklearn API

8 methods

数据划分、特征标准化、模型训练预测与交叉验证的核心 API。

train_test_split(X, y, test_size)

将数据集随机划分为训练集与测试集。

Parameters

NameTypeDescription
Xarray_like特征矩阵
yarray_like标签
test_sizefloat | int测试集比例或样本数

Returns

tuple — (X_train, X_test, y_train, y_test)

Example

machine-learning
from sklearn.model_selection import train_test_split
import numpy as np

X = np.random.rand(100, 4)
y = np.random.randint(0, 2, 100)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=42)
print(X_tr.shape, X_te.shape)
StandardScaler().fit_transform(X)

对特征进行标准化(均值为 0,方差为 1)。

Parameters

NameTypeDescription
Xarray_like待标准化的特征矩阵

Returns

ndarray — 标准化后的特征

Example

machine-learning
from sklearn.preprocessing import StandardScaler
import numpy as np

X = np.array([[1, 10], [2, 20], [3, 30]])
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
print(X_scaled.mean(axis=0), X_scaled.std(axis=0))
LogisticRegression()

创建逻辑回归分类器实例。

Returns

LogisticRegression — 分类器

Example

machine-learning
from sklearn.linear_model import LogisticRegression
import numpy as np

X = np.array([[1], [2], [3], [4]])
y = np.array([0, 0, 1, 1])
clf = LogisticRegression()
clf.fit(X, y)
print(clf.predict([[1.5]]))
RandomForestClassifier(n_estimators)

创建随机森林分类器,通过多棵决策树集成提升效果。

Parameters

NameTypeDescription
n_estimatorsint树的数量

Returns

RandomForestClassifier — 分类器

Example

machine-learning
from sklearn.ensemble import RandomForestClassifier
import numpy as np

X = np.random.rand(100, 5)
y = np.random.randint(0, 2, 100)
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X, y)
print(clf.predict(X[:3]))
model.fit(X, y)

用训练数据拟合模型参数。

Parameters

NameTypeDescription
Xarray_like训练特征
yarray_like训练标签

Returns

模型实例 — 拟合后的自身

Example

machine-learning
from sklearn.linear_model import LogisticRegression
import numpy as np

X = np.array([[1], [2], [3], [4]])
y = np.array([0, 0, 1, 1])
model = LogisticRegression()
model.fit(X, y)
print(model.coef_, model.intercept_)
model.predict(X)

对输入特征进行预测,返回预测标签。

Parameters

NameTypeDescription
Xarray_like待预测特征

Returns

ndarray — 预测结果

Example

machine-learning
from sklearn.linear_model import LogisticRegression
import numpy as np

X = np.array([[1], [2], [3], [4]])
y = np.array([0, 0, 1, 1])
model = LogisticRegression().fit(X, y)
print(model.predict([[2.5], [3.5]]))
model.score(X, y)

返回模型在给定数据上的平均准确率。

Parameters

NameTypeDescription
Xarray_like测试特征
yarray_like真实标签

Returns

float — 准确率(0~1)

Example

machine-learning
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import numpy as np

X, y = np.random.rand(200, 4), np.random.randint(0, 2, 200)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2)
model = LogisticRegression().fit(X_tr, y_tr)
print(model.score(X_te, y_te))
cross_val_score(model, X, y, cv)

用 k 折交叉验证评估模型,返回每折得分。

Parameters

NameTypeDescription
modelestimator评估器实例
Xarray_like特征矩阵
yarray_like标签
cvint交叉验证折数

Returns

ndarray — 每折的得分

Example

machine-learning
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import numpy as np

X = np.random.rand(100, 5)
y = np.random.randint(0, 2, 100)
scores = cross_val_score(RandomForestClassifier(), X, y, cv=5)
print(scores, scores.mean())