weixin_48503431
2021-06-26 00:53
采纳率: 100%
浏览 43
已采纳

想问一下决策树模型中如何导入做出的数据

#模型输出y
Churn_y=np.array(Churn).reshape(-1,1)

#模型输入x
SeniorCitizen_x=np.array(SeniorCitizen).reshape(-1,1)
Contract_x=Contract_dummies.values
InternetService_x=InternetService_dummies.values
PaymentMethod_x=np.array(PaymentMethod).reshape(-1,1)
OnlineSecurity_x=np.array(OnlineSecurity).reshape(-1,1)
OnlineBackup_x=np.array(OnlineBackup).reshape(-1,1)
DeviceProtection_x=np.array(DeviceProtection).reshape(-1,1)
TechSupport_x=np.array(TechSupport).reshape(-1,1)
tenure_x=tenure_dummies.values
MonthlyCharges_x=MonthlyCharges_dummies.values

X=np.concatenate([SeniorCitizen_x,Contract_x,InternetService_x,PaymentMethod_x,OnlineSecurity_x,\ OnlineBackup_x,DeviceProtection_x,TechSupport_x,tenure_x,MonthlyCharges_x],axis=1)
#数据导入
import pandas as pd
import numpy as np
data_frame=pd.read_excel("Save_X.xlsx")
X=np.array(data_frame.values[:,1:])
data_frame2=pd.read_excel("Save_y.xlsx")
y=np.array(data_frame2.values[:,1])
#模型训练
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
x_train, x_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
tree = DecisionTreeClassifier(max_depth=6,random_state=0)
tree.fit(x_train,y_train)
print("training set score:{:.3f}".format(tree.score(x_train, y_train)))
print("test set score:{:.3f}".format(tree.score(x_test, y_test)))
print("Feature importances : \n{}".format(tree.feature_importances_))
 

这里的data_frame=pd.read_excel("Save_X.xlsx")

data_frame2=pd.read_excel("Save_y.xlsx")是怎么导入进去的

使用X.to_excel的时候会出现问题:

AttributeError: 'numpy.ndarray' object has no attribute 'to_excel'

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • CSDN专家-HGJ 2021-06-26 01:25
    已采纳

    代码中数据导入是通过读取xslx表格数据为数据框,然后取数据框中第一列后面所有列的数组赋值给X,把第一列数组赋值给y。因X是一个numpy数组,不是dataframe,没有to_excel方法,所以报错。numpy数组的保存方法,参考一下这里https://blog.csdn.net/leilei7407/article/details/107511187

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题