如何将字符串转换为浮点数?ValueError: 无法将字符串转换为浮点数: ‘0,25691372’

我在使用XGBoost来分析特征重要性,希望选择那些贡献了90%重要性的特征。首先,我构建了一个DataFrame,因为我需要它来导出到Excel中,然后我编写了一个循环来评估那些贡献了90%重要性的特征。在这之后,有一个神经网络(但不在下面的代码中)。我知道可能有更简单的方法来做这件事,但它给我抛出了一个错误:

ValueError: could not convert string to float: '0,25691372'

代码如下

  import pandas as pdimport numpy as npfrom sklearn.ensemble import RandomForestRegressorfrom sklearn.feature_selection import SelectFromModelfrom sklearn import preprocessingfrom sklearn.model_selection import train_test_splitfrom xgboost import XGBRegressorfrom matplotlib import pyplot as pltdataset = pd.read_csv('CompleteDataSet_original_Clean_CONC.csv', decimal=',', delimiter = ";")from sklearn.metrics import r2_scorelabel = 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_valueX_train1, X_test1, y_train1, y_test1 = train_test_split(features, label, test_size = 0.20, random_state = 1, 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 = XGBRegressor(colsample_bytree= 0.7, learning_rate = 0.005, max_depth = 5, min_child_weight = 3, n_estimators = 1000)sel.fit(X_train, y_train)importances = sel.feature_importances_importances = [str(i) for i in importances]importances = [i.replace(".", ",") for i in importances]df1 = pd.DataFrame(features.columns)df1.columns = ['Features']df2 = pd.DataFrame(importances)df2.columns = ['Importances [%]']result = pd.concat([df1,df2],axis = 1)result = result.sort_values(by='Importances [%]', ascending=False)result.to_excel("Feature_Results.xlsx") i = 0somma = 0feature = []while somma <=0.9:    a = result.iloc[i,-1]    somma = float(a) + somma    feature.append(result.iloc[i,-2])    i = i + 1

回答:

你可以使用locale.atof()来处理使用,作为小数点分隔符的情况。

import localelocale.setlocale(locale.LC_ALL, 'fr_FR')...    somma = locale.atof(a) + somma

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注