用于动物分类的Sklearn模型

时间:2018-08-10 04:36:58

标签: python csv machine-learning scikit-learn

修改: 因此,我设法用所有建议修复了错误。但是现在在model.predict部分正在给我这个问题。

Expected 2D array, got 1D array instead:
array=[   12 15432    40    20    33 40000 12800    20 19841     0     0].
Reshape your data either using array.reshape(-1, 1) if your data has a 
single feature or array.reshape(1, -1) if it contains a single sample.

这是我正在使用的新代码

'''
This method is to handel the training and testing of the models
'''
def testTrainModel(model, xTrain, yTrain, xTest, yTest):
    print("Start Method")
    print("Traing Model")
    model.fit(xTrain, yTrain)
    print("Model Trained")
    print("testing models")
    results = model.predict(xTest)

    print(model.__class__," Prediction Report")
    print(classification_report(results,yTest))
    print("Confusion Matrix")
    print(confusion_matrix(results,yTest))
    print("Accuracy is ", accuracy_score(results, yTest)*100)
    lables =["Hunter", "Scavenger"]
    plotConfusionMatrix(confusion_matrix(results,yTest),
                    lables,
                     title='Confusion matrix')

#Data set Preprocess data
dataframe = pd.read_csv("animalData.csv", dtype = 'category')
print(dataframe.head())
dataframe = dataframe.drop(["Name"], axis = 1)
cleanup = {"Class": {"Primary Hunter" : 0, "Primary Scavenger": 1 }}
dataframe.replace(cleanup, inplace = True)
print(dataframe.head())

#array = dataframe.values
#Data splt
# Seperating the data into dependent and independent variables
X = dataframe.iloc[:, :-1].values
y = dataframe.iloc[:,-1].values
#Get training and testoing data

#Set up the models Put model nicknake and model
models = []
models.append(('LogReg', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('DecTree', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))

#Create all the models
logReg = LogisticRegression()
lda = LinearDiscriminantAnalysis()
knn = KNeighborsClassifier()
decsTree = DecisionTreeClassifier()
nb = GaussianNB()
svm = SVC()

#Test value
trex = [12,15432,40,20,33,40000,12800,20,19841,0,0,0]
testTrainModel(logReg,X, y, trex[:-1], trex[-1:])
testTrainModel(lda,X, y, trex[:-1], trex[-1:])
testTrainModel(knn,X, y, trex[:-1], trex[-1:])
testTrainModel(decsTree,X, y, trex[:-1], trex[-1:])
testTrainModel(nb,X, y, trex[:-1], trex[-1:])
testTrainModel(svm,X, y, trex[:-1], trex[-1:])

OLD: 我在这里想要做的是使用一系列动物特征(例如牙齿和大小),然后将几个内置模型(例如SVN KNN ect)与我制作的这个cvs数据集一起使用。但是它一直说它不能将字符串转换为浮点数,当我取出cvs中的所有字符串时,它确实起作用了,但是我不知道它是否是我想要的,因为我想将每个动物作为猎人或清道夫。我真的不知道我在做什么错,因为我是python新手。也许有人可以帮上忙,看看我的代码,然后告诉我我在做什么错。同样,任何对此进行改进的建议也会很容易被接受。

所以我的代码如下:

import pandas as pd
import numpy as np
import itertools
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report


def plotConfusionMatrix(cm, classes,
                      normalize=False,
                      title='Confusion matrix',
                      cmap=plt.cm.Blues):

plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)

fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
    plt.text(j, i, format(cm[i, j], fmt),
             horizontalalignment="center",
             color="white" if cm[i, j] > thresh else "black")

plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

'''
This method is to handel the training and testing of the models
'''
def testTrainModel(model, xTrain, yTrain, xTest, yTest):
    print("Start Method")
    print("Traing Model")
    model.fit(xTrain, yTrain)
    print("Model Trained")
    print("testing models")
    results = model.predict(xTest)

    print(model.__class__," Prediction Report")
    print(classification_report(results,yTest))
    print("Confusion Matrix")
    print(confusion_matrix(results,yTest))
    print("Accuracy is ", accuracy_score(results, yTest)*100)
    lables =["Hunter", "Scavenger"]
    plotConfusionMatrix(confusion_matrix(results,yTest),
                        lables,
                         title='Confusion matrix')




#T-Rex, 12, 15432,  40, 20, 33, 40000,  12800,  20, 19841,  0,  0,


#Data set
dataframe = pd.read_csv("animalData.csv")
print(dataframe.head())
#array = dataframe.values
#Data splt
# Seperating the data into dependent and independent variables
X = dataframe.iloc[:, :-1].values
y = dataframe.iloc[:,-1].values
#Get training and testoing data

seed = 7 #prepare configuration for cross validation test harness

#Set up the models Put model nicknake and model
models = []
models.append(('LogReg', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('DecTree', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))


#store the results
results = []
names =[]
scoring = 'accuracy'
#print the results

for name, model in models:
    kfold = model_selection.KFold(n_splits=9, random_state=seed)
    cv_results = model_selection.cross_val_score(model, X, y, cv=kfold,             scoring=scoring)
    results.append(cv_results)
    names.append(name)
    msg = "Model:%s:\n Cross Validation Score Mean:%f - StdDiv:(%f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)

#plot the data
figure1 = plt.figure()
figure1.suptitle("Algorithm Comparision")
ax = figure1.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()

#Create all the models
logReg = LogisticRegression()
lda = LinearDiscriminantAnalysis()
knn = KNeighborsClassifier()
decsTree = DecisionTreeClassifier()
nb = GaussianNB()
svm = SVC()

#Test value
trex = ["T-Rex",12,15432,40,20,33,40000,12800,20,19841,0,0,"Primary Hunter"]
testTrainModel(logReg,X, y, trex[:-1], trex[-1:])
testTrainModel(lda,X, y, trex[:-1], trex[-1:])
testTrainModel(knn,X, y, trex[:-1], trex[-1:])
testTrainModel(decsTree,X, y, trex[:-1], trex[-1:])
testTrainModel(nb,X, y, trex[:-1], trex[-1:])
testTrainModel(svm,X, y, trex[:-1], trex[-1:])

现在这做了很多事情,我想我一切都很好,但可能我的数据也有误。

这是测试的csv文件

  

名称,牙齿长度,重量,长度,高度,速度,卡路里摄入量,咬力,猎物速度,猎物大小,EyeSight,气味,类别   鳄鱼,4,2400,23,1.6,8,2500,3700,30,881,0,0,初级猎人   狮子,2.7,416,9.8,3.9,50,7236,650,35,1300,0,0,初级猎人   熊,3.6,600,7,3.35,40,20000,975,0,0,0,0,主要清道夫   老虎,3,260,12,3,40,7236,1050,37,160,0,0,初级猎人   鬣狗,0.27,160,5,2,37,5000,1100,20,40,0,0,主要清道夫   美洲虎,2,220,5.5,2.5,40,5000,1350,15,300,0,0,主要猎人   猎豹1.5,154,4.9,2.9,70,2200,475,56,185,0,0,初级猎人   KomodoDragon,0.4,150,8.5,1,13,1994,240,24,110,0,0,主要清道夫

在此方面的任何帮助将不胜感激。

堆栈跟踪

  File "<ipython-input-10-691557e6b9ae>", line 1, in <module>
runfile('E:/TestPythonCode/Classifier.py', wdir='E:/TestPythonCode')

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 678, in runfile
execfile(filename, namespace)

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 106, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

  File "E:/TestPythonCode/Classifier.py", line 110, in <module>
cv_results = model_selection.cross_val_score(model, X, y, cv=kfold, scoring=scoring)

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\model_selection\_validation.py", line 342, in cross_val_score
pre_dispatch=pre_dispatch)

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\model_selection\_validation.py", line 206, in cross_validate
for train, test in cv.split(X, y, groups))

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 779, in __call__
while self.dispatch_one_batch(iterator):

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 625, in dispatch_one_batch
self._dispatch(tasks)

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 588, in _dispatch
job = self._backend.apply_async(batch, callback=cb)

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 111, in apply_async
result = ImmediateResult(func)

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 332, in __init__
self.results = batch()

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in <listcomp>
return [func(*args, **kwargs) for func, args, kwargs in self.items]

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\model_selection\_validation.py", line 458, in _fit_and_score
estimator.fit(X_train, y_train, **fit_params)

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\linear_model\logistic.py", line 1216, in fit
    order="C")

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\lib\site-packages\sklearn\utils\validation.py", line 573, in check_X_y
    ensure_min_features, warn_on_dtype, estimator)

  File "C:\Users\matth\Anaconda3\envs\TensorfGPU2\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: 'KomodoDragon'

1 个答案:

答案 0 :(得分:1)

如果您使用的是numpy.ndarry,则同时使用string元素和float元素是无效的,例如: 原生python列表:

mylist = [1, 3, 'KomodoDragon']

可以,但是当您尝试将列表mylist转换为ndarry对象时,例如:

mylist = np.array(mylist, dtype=float)

会发生错误

  

无法将字符串转换为浮点数:“ KomodoDragon”

。 您可以使用一键编码来解决此问题。