【发布时间】:2020-05-04 06:44:36
【问题描述】:
早上好!我是 python 新手,我使用 Spyder 4.0 构建神经网络。
在下面的脚本中,我使用随机森林来进行特征重要性。所以importances 的值告诉我每个特性的重要性。不幸的是我不能上传数据集,但我可以告诉你有 18 个特征和 1 个标签,都是物理量,这是一个回归问题。
我想将变量importances 导出到一个excel 文件中,但是当我这样做时(只是将向量合并),数字带有点(例如0.012、0.015、......等)。为了在 excel 文件中使用它,我更喜欢使用逗号而不是点。
我尝试使用.replace('.',','),但它不起作用,错误是:
AttributeError: 'numpy.ndarray' object has no attribute 'replace'
它认为这是因为向量 importances 是一个 float64 (18,) 的数组。
我能做什么?
谢谢。`
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.feature_selection import SelectFromModel
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
dataset = pd.read_csv('Dataset.csv', decimal=',', delimiter = ";")
label = dataset.iloc[:,-1]
features = dataset.drop(columns = ['Label'])
y_max_pre_normalize = max(label)
y_min_pre_normalize = min(label)
def denormalize(y):
final_value = y*(y_max_pre_normalize-y_min_pre_normalize)+y_min_pre_normalize
return final_value
X_train1, X_test1, y_train1, y_test1 = train_test_split(features, label, test_size = 0.20, shuffle = True)
y_test2 = y_test1.to_frame()
y_train2 = y_train1.to_frame()
scaler1 = preprocessing.MinMaxScaler()
scaler2 = preprocessing.MinMaxScaler()
X_train = scaler1.fit_transform(X_train1)
X_test = scaler2.fit_transform(X_test1)
scaler3 = preprocessing.MinMaxScaler()
scaler4 = preprocessing.MinMaxScaler()
y_train = scaler3.fit_transform(y_train2)
y_test = scaler4.fit_transform(y_test2)
sel = RandomForestRegressor(n_estimators = 200,max_depth = 9, max_features = 5, min_samples_leaf = 1, min_samples_split = 2,bootstrap = False)
sel.fit(X_train, y_train)
importances = sel.feature_importances_
# sel.fit(X_train, y_train)
# a = []
# for feature_list_index in sel.get_support(indices=True):
# a.append(feat_labels[feature_list_index])
# print(feat_labels[feature_list_index])
# X_important_train = sel.transform(X_train1)
# X_important_test = sel.transform(X_test1)
【问题讨论】:
标签: python machine-learning neural-network spyder gridsearchcv