KevinIvan 2022-03-03 11:31 采纳率: 50%
浏览 88
已结题

利用yellowbrick绘制决策树残差图报错:ValueError: x and y must be the same size

使用决策树网格搜索参数的找到最佳模型,但是想用yellowbrick画残差图的时候报错说X和y不是一个size,没办法绘制残差图,请问如何解决这个问题,部分代码如下:
import numpy as np
import matplotlib.pyplot as plt
import graphviz
from sqlalchemy import create_engine
from sklearn import tree,metrics
import pandas as pd
from yellowbrick.regressor import PredictionError,ResidualsPlot
from sklearn.model_selection import train_test_split,GridSearchCV
from yellowbrick.model_selection import LearningCurve,FeatureImportances
import matplotlib
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import Ridge
import seaborn as sns

datas = pd.read_sql_table(table_name,conn)

features = ['X1,'X2','X3',
        'X4,'X5']
target = ['Y']
X = np.array(datas[features])
y = np.array(datas[target])
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=0.7,shuffle=True,random_state=0)

model = tree.DecisionTreeRegressor(criterion='squared_error'
                                  ,random_state= 0
                                  )
parameters = {'max_depth':[8,12],
              'min_samples_leaf':[1,3],
              'splitter':["best","random"],}
clf = GridSearchCV(estimator=model,param_grid=parameters)

clf = clf.fit(X_train,y_train)
print(clf.best_score_  )
print(clf.best_params_)
bestmodel = clf.best_estimator_
print(bestmodel)

ResPlot = ResidualsPlot(bestmodel)
ResPlot.fit(X_train, y_train)
ResPlot.score(X_test, y_test)
ResPlot.show()

ValueError: x and y must be the same size
搜寻过相关问题,我也用len()查了长度,训练集X,y都是87,测试集是38个数据,总共125个数据,也有说是格式不是arrary,但是我统一了数据格式在代码开头,我是5特征的数据,所以训练集的shape是875和871的。不知道哪里出的问题,麻烦解答。
绘制残差图
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 3月11日
    • 创建了问题 3月3日

    悬赏问题

    • ¥15 from seleniumwire import webdriver 在抓取http://链接的时候会自动转https://这个怎么解决
    • ¥15 hive直连数据库模式插入mysql表数据失败(相关搜索:数据库)
    • ¥30 不会,学习,有偿解答
    • ¥15 SQL查询语句报错(检查)
    • ¥15 此表中公式应该怎么写
    • ¥15 求HI-TECH PICC 9.50 PL3安装包
    • ¥15 下载ctorch报错,求解
    • ¥15 如何入门学习c语言,单片机
    • ¥15 idea 编辑语言的选择
    • ¥15 Windows下部署Asmjit