【发布时间】:2023-04-10 07:08:02
【问题描述】:
我对机器学习很陌生,只是掌握了这些技术。因此,我正在尝试使用具有 4 个特征和目标特征/类(真值 1 或 0)的数据集在以下分类器上训练模型。
分类器
- SGD 分类器
- 随机森林分类器
- 线性支持向量分类器
- 高斯过程分类器
我正在以下数据集上训练模型[部分数据集如下所示]。
训练集:train_sop_truth.csv
Subject,Predicate,Object,Computed,Truth
concept:sportsteam:hawks,concept:teamplaysincity,concept:city:atlanta,0.4255912602,1
concept:stadiumoreventvenue:honda+AF8-center,concept:stadiumlocatedincity,concept:city:anaheim,0.4276425838,1
concept:sportsteam:ducks,concept:teamplaysincity,concept:city:anaheim,0.4762486517,1
concept:sportsteam:n1985+AF8-chicago+AF8-bears,concept:teamplaysincity,concept:city:chicago,0.4106097221,1
concept:stadiumoreventvenue:philips+AF8-arena,concept:stadiumlocatedincity,concept:city:atlanta,0.4190083146,1
concept:stadiumoreventvenue:united+AF8-center,concept:stadiumlocatedincity,concept:city:chicago,0.4211134315,1
测试数据集位于另一个 .csv 文件中,名称为 test_sop_truth.csv。
测试集:test_sop_truth.csv
Subject,Predicate,Object,Computed,Truth
Nigel_Cole,isMarriedTo,Kate_Isitt,0.9350595474,1
Véra_Clouzot,isMarriedTo,Henri-Georges_Clouzot,0.4773990512,1
Norodom_Sihanouk,produced,The_Last_Days_of_Colonel_Savath,0.3942225575,1
Farouk_of_Egypt,isMarriedTo,Farida_of_Egypt,0.4276426733,1
然后我想检查每个特征的形状,并希望看到相同数量的特征,因为我对两个数据集应用相同的转换。但它们不同。
Python 代码
import pandas as pd
import numpy as np
from termcolor import colored
features = pd.read_csv('../Data/train_sop_truth.csv')
testFeatures = pd.read_csv('../Data/test_sop_truth.csv')
print(features.head(5))
print(colored('\nThe shape of our features is:','green'), features.shape)
print(colored('\nThe shape of our Test features is:','green'), testFeatures.shape)
print()
print(colored('\n DESCRIPTIVE STATISTICS\n','yellow'))
print(colored(features.describe(),'cyan'))
print()
print(colored(testFeatures.describe(),'cyan'))
features = pd.get_dummies(features)
testFeatures = pd.get_dummies(testFeatures)
features.iloc[:,5:].head(5)
testFeatures.iloc[:,5].head(5)
labels = np.array(features['Truth'])
testlabels = np.array(testFeatures['Truth'])
features= features.drop('Truth', axis = 1)
testFeatures = testFeatures.drop('Truth', axis = 1)
feature_list = list(features.columns)
testFeature_list = list(testFeatures.columns)
features = np.array(features)
testFeatures = np.array(testFeatures)
train_samples = 100
testX_train, textX_test, testy_train, testy_test = model_selection.train_test_split(testFeatures, testlabels, test_size=0.25, random_state = 42)
X_train, X_test, y_train, y_test = model_selection.train_test_split(features, labels, test_size = 0.25, random_state = 42)
print(colored('\n TRAINING & TESTING SETS','yellow'))
print(colored('\nTraining Features Shape:','magenta'), X_train.shape)
print(colored('Training Labels Shape:','magenta'), X_test.shape)
print(colored('Testing Features Shape:','magenta'), y_train.shape)
print(colored('Testing Labels Shape:','magenta'), y_test.shape)
print()
print(colored('\n TRAINING & TESTING SETS','yellow'))
print(colored('\nTraining Features Shape:','magenta'), testX_train.shape)
print(colored('Training Labels Shape:','magenta'), textX_test.shape)
print(colored('Testing Features Shape:','magenta'), testy_train.shape)
print(colored('Testing Labels Shape:','magenta'), testy_test.shape)
输出
The shape of our features is: (1860, 5)
The shape of our Test features is: (1386, 5)
DESCRIPTIVE STATISTICS
Computed Truth
count 1860.000000 1860.000000
mean 0.443222 0.913441
std 0.110788 0.281264
min 0.000000 0.000000
25% 0.418164 1.000000
50% 0.427643 1.000000
75% 0.450023 1.000000
max 1.000000 1.000000
Computed Truth
count 1386.000000 1386.000000
mean 0.511809 0.992063
std 0.197954 0.088765
min 0.009042 0.000000
25% 0.418649 1.000000
50% 0.429140 1.000000
75% 0.515809 1.000000
max 1.702856 1.000000
TRAINING & TESTING SETS
Training Features Shape: (1395, 1045)
Training Labels Shape: (465, 1045)
Testing Features Shape: (1395,)
Testing Labels Shape: (465,)
TRAINING & TESTING SETS
Training Features Shape: (1039, 1790)
Training Labels Shape: (347, 1790)
Testing Features Shape: (1039,)
Testing Labels Shape: (347,)
我在这里不明白的是特征形状如何不同于特征(训练集)的 1045 和 testFeatures(测试集)的 1790,尽管经历了相同的转换并且具有相同的数字csv 文件中的特征和特征形式。
非常感谢您在这方面提出任何建议或澄清。
【问题讨论】:
-
我认为问题在于调用 pd.get_dummies,这会将所有类别添加到列中,并且当您的训练和测试功能具有不同的类别值时,这将导致列数不同.
-
如果我可能会问,为什么你的训练和测试数据如此不同?因为值的格式也不同。
-
@Sach,一个数据集由训练集形式的 NELL 事实驱动,另一个由 Yago 驱动。但两者的概念都坚持作为主、宾、谓的相同概念。
-
@Sach 当您说类别的不同值时,与
Computed功能相反,它是否仍被视为具有分类值的简单字符串,这是唯一的功能连续值的特征并且在 1 和 0 的范围内?
标签: python machine-learning classification training-data