如何更改逗号中的点? [英] What can I do to change dot in comma?

查看:154
本文介绍了如何更改逗号中的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好!我是python的新手,我使用Spyder 4.0构建神经网络. 在下面的脚本中,我使用随机森林来实现功能的重要性.因此,值importances是告诉我每个功能的重要性的值.不幸的是,我无法上传数据集,但是我可以告诉您,有18个要素和1个标签,都是物理量,这是一个回归问题. 我想在excel文件中导出变量importances,但是当我执行该操作(简单地将向量关联)时,数字就是点(例如0.012、0.015,.... ect).为了在excel文件中使用它,我更希望使用逗号而不是点. 我尝试使用.replace('.',','),但是它不起作用,错误是:

Good morning! I'm new of python, I use Spyder 4.0 to build neural network. In the script below I use the random forest in order to do feature importances. So the values importances are the ones that tell me what is the importance of each features. Unfortunatly I can't upload the dataset, but I can tell you that there are 18 features and 1 label, both are phisical quantyties and it's a regression problem. I want to export in a excel file the variable importances, but when I do it (simply cooping the vector) the numbers are with the dot (eg 0.012, 0.015, .....ect). In order to use it in the excel file I prefere to have the comma instead of the dot. I try to use .replace('.',',') but it doesn't works, the error is:

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

它认为发生这种情况是因为向量importances是float64的数组(18,). 我该怎么办?

It think that it happens because the vector importances is an Array of float64 (18,). What can I do?

谢谢.`

    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上运行了它,这就是为什么您还会看到">>>".

I will try to show you an example of what you should do by using some random values. I ran this on the python shell that's why you see also the ">>>".

>>> 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'

您需要做的是将重要性"元素转换为字符串列表

What you need to to is to convert the elements of "importance" to a list of strings

>>> 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']

这篇关于如何更改逗号中的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆