ValueError:无法将字符串转换为浮点型:读取.csv数据集时

时间:2019-06-11 11:13:34

标签: python machine-learning data-science

为什么我继续出现此错误?

我怀疑是因为get_dummies无法用于分类数据?

我应该先对数据进行标签编码吗?

感谢您的帮助

import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

data = pd.read_csv(r'C:\Users\charl\Downloads\TelcoChurn.csv')
data.head()

categorical_features = ['customerID', 'gender', 'Partner', 'Dependents', 'PhoneService', 'MultipleLines', 'InternetService', 
                        'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport', 'SeniorCitizen',
                        'StreamingTV', 'StreamingMovies', 'Contract', 'PaperlessBilling', 'PaymentMethod', 'Churn']
continuous_features = ['MonthlyCharges', 'TotalCharges', 'tenure']

data[continuous_features].describe()

for col in categorical_features:
    dummies = pd.get_dummies(data[col], prefix=col)
    data = pd.concat([data, dummies], axis=1)
    data.drop(col, axis=1, inplace=True)

data.head()

mms = MinMaxScaler()
mms.fit(data)
data_transformed = mms.transform(data)

Sum_of_squared_distances = []
K = range(1, 15)
for k in K:
    km = KMeans(n_clusters = k)
    km = km.fit(data_transformed)
    Sum_of_squared_distances.append(km.inertia_)

plt.plot(K, Sum_of_squared_distances, 'bx-')
plt.xlabel('k')
plt.ylabel('Sum_of_squared_distances')
plt.title('Elbow Method for Optimal k')
plt.show()

希望这是请求的追溯。

感谢您的帮助

菲勒 填料 填料 填料 填充物

In [55]: runfile('C:/Users/charl/AppData/Roaming/SPB_Data/.spyder-py3/temp.py', wdir='C:/Users/charl/AppData/Roaming/SPB_Data/.spyder-py3')
Traceback (most recent call last):

  File "<ipython-input-55-acad0a080800>", line 1, in <module>
    runfile('C:/Users/charl/AppData/Roaming/SPB_Data/.spyder-py3/temp.py', wdir='C:/Users/charl/AppData/Roaming/SPB_Data/.spyder-py3')

  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/charl/AppData/Roaming/SPB_Data/.spyder-py3/temp.py", line 31, in <module>
    mms.fit(data)

  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\sklearn\preprocessing\data.py", line 308, in fit
    return self.partial_fit(X, y)

  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\sklearn\preprocessing\data.py", line 334, in partial_fit
    estimator=self, dtype=FLOAT_DTYPES)

  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)

ValueError: could not convert string to float:

0 个答案:

没有答案