
本文档旨在指导读者如何使用 sklearn 库构建一个多标签分类模型,用于预测基于坐标数据的人员位置和姿态。我们将探讨常见错误,并提供正确的代码示例,帮助您成功训练模型。本文重点解决 ValueError: Found input variables with inconsistent numbers of samples 错误,并提供调试和改进模型的建议。
数据准备
首先,我们需要准备数据。假设我们有一个包含坐标数据以及对应的类别(class)和姿态(stand)的 CSV 文件。
import pandas as pd
from sklearn.model_selection import train_test_split
# 读取 CSV 文件
df = pd.read_csv('deadlift.csv')
# 显示前几行数据
print(df.head())接下来,我们将数据分割成特征(X)和目标变量(y)。目标变量包含 class 和 stand 两列,表示多标签分类问题。
# 分割特征和目标变量
X = df.drop(['class', 'stand'], axis=1)
y = df[['class', 'stand']]
# 分割训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1234)
# 打印训练集形状
print("X_train shape:", X_train.shape)
print("y_train shape:", y_train.shape)确保 X_train 和 y_train 的样本数量一致。ValueError: Found input variables with inconsistent numbers of samples 错误通常是因为训练集和目标变量的样本数量不匹配造成的。
模型构建与训练
现在,我们可以构建和训练模型。这里使用 Pipeline 结合 CountVectorizer 和 MultiOutputClassifier,其中 MultiOutputClassifier 使用 LogisticRegression 作为基础分类器。
系统简介逍遥内容管理系统(CarefreeCMS)是一款功能强大、易于使用的内容管理平台,采用前后端分离架构,支持静态页面生成,适用于个人博客、企业网站、新闻媒体等各类内容发布场景。核心特性1、模板套装系统 - 支持多套模板自由切换,快速定制网站风格2、静态页面生成 - 一键生成纯静态HTML页面,访问速度快,SEO友好3、文章管理 - 支持富文本编辑、草稿保存、文章属性标记、自动提取SEO4、全
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.multioutput import MultiOutputClassifier
from sklearn.linear_model import LogisticRegression
# 构建模型
model = Pipeline(steps=[
('cv', CountVectorizer(lowercase=False)),
('lr_multi', MultiOutputClassifier(LogisticRegression()))
])
# 训练模型
model.fit(X_train.astype(str), y_train)注意:
- CountVectorizer 通常用于文本数据。如果你的特征不是文本数据,可能需要使用其他特征提取方法,例如 StandardScaler 或 MinMaxScaler。
- LogisticRegression 是一个常用的分类器,但也可以尝试其他分类器,例如 RandomForestClassifier 或 SVC。
- 需要将X_train的数据类型转换为字符串类型,避免ValueError。
模型评估
训练完成后,我们需要评估模型的性能。
from sklearn.metrics import accuracy_score
# 预测
y_pred = model.predict(X_test.astype(str))
# 评估模型
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)注意事项:
- accuracy_score 是一种常用的评估指标,但对于多标签分类问题,可能需要使用其他指标,例如 precision_score、recall_score 或 f1_score。
- 可以使用 classification_report 生成更详细的评估报告。
完整代码示例
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.multioutput import MultiOutputClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 读取 CSV 文件
df = pd.read_csv('deadlift.csv')
# 分割特征和目标变量
X = df.drop(['class', 'stand'], axis=1)
y = df[['class', 'stand']]
# 分割训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1234)
# 构建模型
model = Pipeline(steps=[
('cv', CountVectorizer(lowercase=False)),
('lr_multi', MultiOutputClassifier(LogisticRegression()))
])
# 训练模型
model.fit(X_train.astype(str), y_train)
# 预测
y_pred = model.predict(X_test.astype(str))
# 评估模型
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)总结
本文档介绍了如何使用 sklearn 库构建一个多标签分类模型,用于预测基于坐标数据的人员位置和姿态。我们解决了 ValueError: Found input variables with inconsistent numbers of samples 错误,并提供了完整的代码示例。记住,数据预处理、特征选择和模型选择是构建一个高性能模型的关键步骤。根据实际情况调整代码,并尝试不同的模型和评估指标,以获得最佳结果。同时,务必检查训练数据和目标变量的形状是否一致,这是避免 ValueError 的关键。









