使用决策树网格搜索参数的找到最佳模型,但是想用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的。不知道哪里出的问题,麻烦解答。
绘制残差图