如何使用panda.read_csv从python中的csv文件导入数据?

时间:2019-10-19 14:35:00

标签: python pandas scikit-learn

enter image description here我正在尝试使用scikit_learn和pandas解决python中的决策树问题。数据集在CSV文件中可用。 当我尝试在python中加载数据时,出现错误消息“ ValueError:无法将字符串转换为float:'CustomerID'”。我不知道我在代码中做错了什么。

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
col_names=['CustomerID','Gender','Car Type', 'Shirt Size','Class']
pima=pd.read_csv("F:\Current semster courses\Machine 
Learning\ML_A1_Fall2019\Q2_dataset.csv",header=None, names=col_names)
pima.head()
feature_cols=['CustomerID','Gender','Car Type', 'Shirt Size']
X=pima[feature_cols]
y=pima.Class
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
clf = DecisionTreeClassifier()

# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)

#Predict the response for test dataset
y_pred = clf.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

有人可以告诉我我在做什么错吗?

数据集:

CustomerID  Gender  Car Type    Shirt Size  Class
1            M      Family       Small      C0
2            M      Sports       Medium     C0
3            M      Sports       Medium     C0
4            M      Sports       Large      C0
5            M      Sports     Extra Large  C0
6            M      Sports     Extra Large  C0
7            F      Sports       Small      C0
8            F      Sports       Small      C0
9            F      Sports       Medium     C0
10           F      Luxury       Large      C0
11           M      Family       Large      C1
12           M      Family     Extra Large  C1
13           M      Family       Medium     C1
14           M      Luxury    Extra Large   C1
15           F      Luxury       Small      C1
16           F      Luxury       Small      C1
17           F      Luxury       Medium     C1
18           F      Luxury       Medium     C1
19           F      Luxury       Medium     C1
20           F      Luxury       Large      C1

1 个答案:

答案 0 :(得分:1)

啊。好。问题是您的数据是分类数据,scikit无法直接使用。首先需要将其转换为数字数据。方法._get_dummies()通过采用具有多个分类值的单列并将其转换为多列(每列包含数字1或0指示哪个类别为“ True”)来实现。

此外,您应该从功能部件中删除“客户ID”列。它是一个随机值,与该行属于一个类别还是另一个类别无关。

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics

col_names=['CustomerID','Gender','Car Type', 'Shirt Size','Class']
data = [['1',  'M', 'Family', 'Small',      'C0'], 
        ['2',  'M', 'Sports', 'Medium',     'C0'], 
        ['3',  'M', 'Sports', 'Medium',     'C0'], 
        ['4',  'M', 'Sports', 'Large',      'C0'], 
        ['5',  'M', 'Sports', 'Extra Large','C0'], 
        ['6',  'M', 'Sports', 'Extra Large','C0'], 
        ['7',  'F', 'Sports', 'Small',      'C0'], 
        ['8',  'F', 'Sports', 'Small',      'C0'], 
        ['9',  'F', 'Sports', 'Medium',     'C0'], 
        ['10', 'F', 'Luxury', 'Large',      'C0'], 
        ['11', 'M', 'Family', 'Large',      'C1'], 
        ['12', 'M', 'Family', 'Extra Large','C1'], 
        ['13', 'M', 'Family', 'Medium',     'C1'], 
        ['14', 'M', 'Luxury', 'Extra Large','C1'], 
        ['15', 'F', 'Luxury', 'Small',      'C1']]

#pima=pd.read_csv("F:\Current semster courses\Machine ...
pima=pd.DataFrame(data, columns = col_names)
# Convert the categorical data to multiple columns of numerical data for the decision tree
pima = pd.get_dummies(pima, prefix=['CustomerID','Gender','Car Type', 'Shirt Size','Class'])
print(pima)

#feature_cols=['CustomerID','Gender','Car Type','Shirt Size']
feature_cols=['Gender_F', 'Gender_M',
       'Car Type_Family', 'Car Type_Luxury', 'Car Type_Sports',
       'Shirt Size_Extra Large', 'Shirt Size_Large', 'Shirt Size_Medium',
       'Shirt Size_Small', 'Class_C0', 'Class_C1']
X=pima[feature_cols]
y=pima[['Class_C0', 'Class_C1']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)

print("X_train =", X_train) 
print("X_test =", X_test) 
print("y_train =", y_train)
print("y_test =", y_test )
clf = DecisionTreeClassifier()

# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)

#Predict the response for test dataset
y_pred = clf.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))