- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:所以我设法用所有建议修复了错误。但现在 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:])
旧版:我在这里想做的是使用一系列动物特征,例如 dentry 和大小,然后使用一些内置模型,例如 SVN KNN 等以及我制作的 cvs 数据集。但它一直说它不能将字符串转换为 float ,当我取出简历中的所有字符串时,它确实有效,但我不知道这是否是我想要的,因为我想将每个动物绘制为猎人或清道夫。我真的不知道我在这里做错了什么,因为我是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 文件
Name,teethLength,weight,length,hieght,speed,Calorie Intake,Bite Force,Prey Speed,PreySize,EyeSight,Smell,Class Crocodile,4,2400,23,1.6,8,2500,3700,30,881,0,0,Primary Hunter Lion,2.7,416,9.8,3.9,50,7236,650,35,1300,0,0,Primary Hunter Bear,3.6,600,7,3.35,40,20000,975,0,0,0,0,Primary Scavenger Tiger,3,260,12,3,40,7236,1050,37,160,0,0,Primary Hunter Hyena,0.27,160,5,2,37,5000,1100,20,40,0,0,Primary Scavenger Jaguar,2,220,5.5,2.5,40,5000,1350,15,300,0,0,Primary Hunter Cheetah,1.5,154,4.9,2.9,70,2200,475,56,185,0,0,Primary Hunter KomodoDragon,0.4,150,8.5,1,13,1994,240,24,110,0,0,Primary Scavenger
对此的任何帮助将不胜感激。
堆栈跟踪
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'
最佳答案
如果使用numpy.ndarry,则字符串元素和浮点元素一起使用是无效的。例如:原生 Python 列表:
mylist = [1, 3, 'KomodoDragon']
没关系,但是当您尝试将 list mylist 转换为 ndarry 对象时,例如:
mylist = np.array(mylist, dtype=float)
会发生错误
could not convert string to float: 'KomodoDragon'
。您可以使用one-hot编码来处理这个问题。
关于python - 用于动物分类的 Sklearn 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51779042/
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
给出这个例子: 动物 thisIsACat = new Cat(); 是否存在从 Cat 到 Animal 的隐式转换? 说明: 假设: class Animal { } class Cat: Ani
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
这个问题已经有答案了: What is PECS (Producer Extends Consumer Super)? (16 个回答) 已关闭 4 年前。 我对 Java 通用通配符有疑问 为什么这
我需要一个 java 库来确定哪个图像是“人体”;这是一张“人脸”;这是一个“动物”;这是一个“风景”等等。 有这样的东西吗? 谢谢 最佳答案 我不认为那里有什么方便的东西。特别是因为你在这里有非常广
我是一名优秀的程序员,十分优秀!