sklearn API
8 methods数据划分、特征标准化、模型训练预测与交叉验证的核心 API。
train_test_split(X, y, test_size)将数据集随机划分为训练集与测试集。
Parameters
| Name | Type | Description |
|---|---|---|
| X | array_like | 特征矩阵 |
| y | array_like | 标签 |
| test_size | float | 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
| Name | Type | Description |
|---|---|---|
| X | array_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
| Name | Type | Description |
|---|---|---|
| n_estimators | int | 树的数量 |
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
| Name | Type | Description |
|---|---|---|
| X | array_like | 训练特征 |
| y | array_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
| Name | Type | Description |
|---|---|---|
| X | array_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
| Name | Type | Description |
|---|---|---|
| X | array_like | 测试特征 |
| y | array_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
| Name | Type | Description |
|---|---|---|
| model | estimator | 评估器实例 |
| X | array_like | 特征矩阵 |
| y | array_like | 标签 |
| cv | int | 交叉验证折数 |
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())