gpt4 book ai didi

python - 值错误: X has 231 features per sample; expecting 1228

转载 作者:行者123 更新时间:2023-11-30 09:02:34 26 4
gpt4 key购买 nike

这是训练模型的脚本顶部(我使用逻辑回归):

data_raw = pd.read_sql(sql,cnxn)

pd.Series(data_raw.columns)
pd.Series(data_raw.dtypes)

data_raw.describe(include='all')

data_raw['collision_type'] = data_raw.loc[0:, 'collision_type'].replace('?', 'Unknown')

data_raw['property_damage'] = data_raw.loc[0:, 'property_damage'].replace('?', 'Unknown')

data_raw.isnull().sum()

dropping_columns = ['months_as_customer', 'policy_bind_date', 'age', 'policy_number', 'policy_annual_premium', 'insured_zip',
'capital_gains', 'capital_loss', 'total_claim_amount', 'injury_claim', 'property_claim', 'vehicle_claim',
'auto_year']

data_cleaned = data_raw.drop(dropping_columns, axis=1)

data_preprocessed = pd.get_dummies(data_cleaned, drop_first=True)


targets = data_preprocessed['fraud_reported_Y']
features = data_preprocessed.drop(['fraud_reported_Y'], axis=1)

x_train, x_test, y_train, y_test = train_test_split(features, targets, test_size=0.2, random_state=420)

from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
y_pred = logreg.predict(x_test)

现在我尝试对测试输入(从 SQL 表导入的测试数据集)进行预测:

test = df['TestTable']
test = test[0]
sql = 'SELECT * FROM '+ test
test_raw = pd.read_sql(sql,cnxn)

#sample_rows = test_raw.sample(n=5)

test_raw.describe(include='all')

test_raw['collision_type'] = data_raw.loc[0:, 'collision_type'].replace('?', 'Unknown')

test_raw['property_damage'] = data_raw.loc[0:, 'property_damage'].replace('?', 'Unknown')

test_raw.isnull().sum()

print(test_raw.shape)

test_dropped = test_raw.drop(dropping_columns, axis=1)
test_preprocessed = pd.get_dummies(test_dropped, drop_first=True)

logreg = LogisticRegression()
logreg.fit(x_train, y_train)
test_predicted = logreg.predict(test_preprocessed)

这是我得到的错误:

Traceback (most recent call last):

File "<ipython-input-149-e6d470e94433>", line 1, in <module>
runfile('C:/Users/BusinessUser/Downloads/insurance_claim_fraud_detection-master/insurance_claim_fraud_detection.py', wdir='C:/Users/BusinessUser/Downloads/insurance_claim_fraud_detection-master')

File "C:\Users\BusinessUser\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\Users\BusinessUser\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/BusinessUser/Downloads/insurance_claim_fraud_detection-master/insurance_claim_fraud_detection.py", line 402, in <module>
test_predicted = logreg.predict(test_preprocessed)

File "C:\Users\BusinessUser\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 289, in predict
scores = self.decision_function(X)

File "C:\Users\BusinessUser\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 270, in decision_function
% (X.shape[1], n_features))

ValueError: X has 231 features per sample; expecting 1228

我的训练数据集有 999 行,带有最终预测结果列,而测试数据集有 50 行,没有预测结果列。其他栏目基本相同。

我是个新手,我很确定关于这个模型训练有一些我不知道的基本事情。非常感谢你们对我的帮助。

最佳答案

检查用于预测的数据 (test_preprocessed) 的列数(特征)是否与用于训练/测试的数据 (x_train) 相同,x_test),例如使用函数 shapelen(test_preprocessed.columns)

关于python - 值错误: X has 231 features per sample; expecting 1228,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60072446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com