ValueError:无法强制转换为 Series,长度必须为 1:给定 n
Posted
技术标签:
【中文标题】ValueError:无法强制转换为 Series,长度必须为 1:给定 n【英文标题】:ValueError: Unable to coerce to Series, length must be 1: given n 【发布时间】:2020-03-08 15:26:55 【问题描述】:我曾尝试使用 scikit-learn 的 RF 回归,但我的标准(来自文档和教程)模型存在问题。所以有代码:
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
db = pd.read_excel('/home/artyom/myprojects//valuevo/field2019/report/segs_inventar_dataframe/excel_var/invcents.xlsx')
age = df[['AGE_1', 'AGE_2', 'AGE_3', 'AGE_4', 'AGE_5']]
hight = df [['HIGHT_','HIGHT_1', 'HIGHT_2', 'HIGHT_3', 'HIGHT_4', 'HIGHT_5']]
diam = df[['DIAM_', 'DIAM_1', 'DIAM_2', 'DIAM_3', 'DIAM_4', 'DIAM_5']]
za = df[['ZAPSYR_', 'ZAPSYR_1', 'ZAPSYR_2', 'ZAPSYR_3', 'ZAPSYR_4', 'ZAPSYR_5']]
tova = df[['TOVARN_', 'TOVARN_1', 'TOVARN_2', 'TOVARN_3', 'TOVARN_4', 'TOVARN_5']]
#df['average'] = df.mean(numeric_only=True, axis=1)
df['meanage'] = age.mean(numeric_only=True, axis=1)
df['meanhight'] = hight.mean(numeric_only=True, axis=1)
df['mediandiam'] = diam.mean(numeric_only=True, axis=1)
df['medianza'] = za.mean(numeric_only=True, axis=1)
df['mediantova'] = tova.mean(numeric_only=True, axis=1)
unite = df[['gapA_segA','gapP_segP', 'A_median', 'p_median', 'circ_media','fdi_median', 'pfd_median', 'p_a_median', 'gsci_media','meanhight']].dropna()
from sklearn.model_selection import train_test_split as ttsplit
df_copy = unite.copy()
trainXset = df_copy[['gapA_segA','gapP_segP', 'A_median', 'p_median', 'circ_media','fdi_median', 'pfd_median', 'p_a_median', 'gsci_media']]
trainYset = df_copy [['meanhight']]
trainXset_train, trainXset_test, trainYset_train, trainYset_test = ttsplit(trainXset, trainYset, test_size=0.3) # 70% training and 30% test
rf = RandomForestRegressor(n_estimators = 100, random_state = 40)
rf.fit(trainXset_train, trainYset_train)
predictions = rf.predict(trainXset_test)
errors = abs(predictions - trainYset_test)
mape = 100 * (errors / trainYset_test)
accuracy = 100 - np.mean(mape)
print('Accuracy:', round(accuracy, 2), '%.')
但输出看起来不太好:
---> 24 errors = abs(predictions - trainYset_test)
25 # Calculate mean absolute percentage error (MAPE)
26 mape = 100 * (errors / trainYset_test)
..... somemore track
ValueError: Unable to coerce to Series, length must be 1: given 780
我怎么解决不了? 780 它是 trainYset_test 的 .shape。我不要求简单的解决方案(为我编写代码),而是要求提出错误的建议。我在教程中都喜欢。
【问题讨论】:
【参考方案1】:通过错误地看到,数组必须具有 1 的形状,
所以使用 reshape 使其形状正确,
predictions=predictions.reshape(780,1)
【讨论】:
以上是关于ValueError:无法强制转换为 Series,长度必须为 1:给定 n的主要内容,如果未能解决你的问题,请参考以下文章
ValueError:无法将字符串转换为浮点数:'GIAC'
ValueError:无法将字符串转换为浮点数:'2100 - 2850'
Python:ValueError:无法将字符串转换为浮点数:'0'