python - 我可以做什么来改变逗号中的点?

标签 python machine-learning neural-network spyder gridsearchcv

早上好!我是Python新手,我使用Spyder 4.0来构建神经网络。 在下面的脚本中,我使用随机森林来确定特征重要性。因此,importances 值告诉我每个功能的重要性。不幸的是我无法上传数据集,但我可以告诉你,有 18 个特征和 1 个标签,两者都是物理量,这是一个回归问题。 我想在 Excel 文件中导出变量 importances,但是当我这样做时(简单地合并向量),数字带有点(例如 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 shell 上运行了这个,这就是为什么您还看到“>>>”。

>>> import numpy as np  # first I import numpy as "np"
# I generate 10 random values and I store them in "importance"
>>> importance=np.random.rand(10)
# here I just want to see the content of "importance"
>>> importance
array([0.77609076, 0.97746829, 0.56946118, 0.23986983, 0.93655692,
       0.22003531, 0.7711095 , 0.36083248, 0.58277805, 0.57865248])
# here there is your error that I reproduce for teaching purpose
>>>importance.replace(".", ",")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'replace'

您需要的是将“重要”元素转换为字符串列表

>>> imp_astr=[str(i) for i in importance]
>>> imp_astr
['0.7760907642658763', '0.9774682868805988', '0.569461184647781', '0.23986982589422634', '0.9365569207431337', '0.22003531170279356', '0.7711094966708247', '0.3608324767276052', '0.5827780487688116', '0.5786524781334242']
# at the end, for each string, you can use the "replace" function
>>> imp_astr=[i.replace(".", ",") for i in imp_astr]
>>> imp_astr
['0,7760907642658763', '0,9774682868805988', '0,569461184647781', '0,23986982589422634', '0,9365569207431337', '0,22003531170279356', '0,7711094966708247', '0,3608324767276052', '0,5827780487688116', '0,5786524781334242']

关于python - 我可以做什么来改变逗号中的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59786327/

相关文章:

machine-learning - 如何调整 GaussianNB?

Python + Scikit-learn :How to plot the curves of training score and validation score against the additive smoothing parameter alpha

python - 自举估计方差为何减少的数学解释

python - sklearn : how to make fit and transform a feature selector in a GridSearchCV

python - 如何使用 SqlAlchemy 按 id 查询数据库?

Python:如何在 Python 中处理预期的可读缓冲区对象

python - 同时使用图像和数字输入的神经网络

machine-learning - 无法使用神经网络近似正弦函数

python - 如何向 scikit-neuralnetwork 中的分类器添加偏差?

python - 使用 selenium python 单击 svg