我想在Django中使用KMeans ...我的代码是:
from django.db import models
from django.utils import timezone
import sys
from sklearn.cluster import KMeans
from sklearn.metrics import pairwise_distances_argmin_min
# Create your models here.
class tabla (models.Model):
nombre = models.CharField(max_length=35)
comida = models.PositiveSmallIntegerField()
cantidad = models.PositiveSmallIntegerField()
edad = models.PositiveSmallIntegerField()
fecha_publicacion = models.DateTimeField(auto_now_add = True)
def datos(self):
cadena="hola, {0}, {1}, {2}, {3}, {4}"
cadena2="{0} {1} {2}"
cadena2.format(self.cantidad,self.comida,self.edad)
#return cadena.format(self.nombre, self.comida,self.cantidad,self.edad,self.fecha_publicacion)
kmeans = KMeans(n_clusters=3).fit(cadena2)
centroids = kmeans.cluster_centers_
return centroids
def __str__(self):
return self.datos()
和..我的结果是:哦,我的问题是...怎么了?
C:\TrabajoIA>python manage.py runserver
C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\externals\joblib\externals\cloudpickle\cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\externals\joblib\externals\cloudpickle\cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Performing system checks...
System check identified no issues (0 silenced).
December 17, 2018 - 04:46:40
Django version 2.1.3, using settings 'TrabajoIA.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\externals\joblib\externals\cloudpickle\cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Performing system checks...
System check identified no issues (0 silenced).
December 17, 2018 - 04:50:53
Django version 2.1.3, using settings 'TrabajoIA.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /admin/Gestion/tabla/
Traceback (most recent call last):
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\models\options.py", line 564, in get_field
return self.fields_map[field_name]
KeyError: '__str__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\utils.py", line 278, in lookup_field
f = _get_non_gfk_field(opts, name)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\utils.py", line 309, in _get_non_gfk_field
field = opts.get_field(name)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\models\options.py", line 566, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: tabla has no field named '__str__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\handlers\base.py", line 156, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\handlers\base.py", line 154, in _get_response
response = response.render()
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\response.py", line 106, in render
self.content = self.rendered_content
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 171, in render
return self._render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\base.py", line 33, in render
return super().render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\library.py", line 214, in render
_dict = self.func(*resolved_args, **resolved_kwargs)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\admin_list.py", line 341, in result_list
'results': list(results(cl))}
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\admin_list.py", line 318, in results
yield ResultList(None, items_for_result(cl, res, None))
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\admin_list.py", line 309, in __init__
super().__init__(*items)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\admin_list.py", line 228, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\utils.py", line 291, in lookup_field
value = attr()
File "C:\TrabajoIA\TrabajoIA\Apps\Gestion\models.py", line 28, in __str__
return self.datos()
File "C:\TrabajoIA\TrabajoIA\Apps\Gestion\models.py", line 23, in datos
kmeans = KMeans(n_clusters=3).fit(cadena2)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\cluster\k_means_.py", line 968, in fit
return_n_iter=True)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\cluster\k_means_.py", line 311, in k_means
order=order, copy=copy_x)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\utils\validation.py", line 522, in check_array
array = np.asarray(array, dtype=dtype, order=order)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: '{0} {1} {2}'
[17/Dec/2018 04:51:07] "GET /admin/Gestion/tabla/ HTTP/1.1" 500 316834
Internal Server Error: /admin/Gestion/tabla/
Traceback (most recent call last):
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\models\options.py", line 564, in get_field
return self.fields_map[field_name]
KeyError: '__str__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\utils.py", line 278, in lookup_field
f = _get_non_gfk_field(opts, name)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\utils.py", line 309, in _get_non_gfk_field
field = opts.get_field(name)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\db\models\options.py", line 566, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: tabla has no field named '__str__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\handlers\base.py", line 156, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\core\handlers\base.py", line 154, in _get_response
response = response.render()
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\response.py", line 106, in render
self.content = self.rendered_content
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 171, in render
return self._render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\base.py", line 33, in render
return super().render(context)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\template\library.py", line 214, in render
_dict = self.func(*resolved_args, **resolved_kwargs)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\admin_list.py", line 341, in result_list
'results': list(results(cl))}
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\admin_list.py", line 318, in results
yield ResultList(None, items_for_result(cl, res, None))
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\admin_list.py", line 309, in __init__
super().__init__(*items)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\templatetags\admin_list.py", line 228, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\django-2.1.3-py3.7.egg\django\contrib\admin\utils.py", line 291, in lookup_field
value = attr()
File "C:\TrabajoIA\TrabajoIA\Apps\Gestion\models.py", line 28, in __str__
return self.datos()
File "C:\TrabajoIA\TrabajoIA\Apps\Gestion\models.py", line 23, in datos
kmeans = KMeans(n_clusters=3).fit(cadena2)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\cluster\k_means_.py", line 968, in fit
return_n_iter=True)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\cluster\k_means_.py", line 311, in k_means
order=order, copy=copy_x)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\utils\validation.py", line 522, in check_array
array = np.asarray(array, dtype=dtype, order=order)
File "C:\Users\kleys\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: '{0} {1} {2}'
[17/Dec/2018 04:51:09] "GET /admin/Gestion/tabla/ HTTP/1.1" 500 316804
KMeans要求您传递类似数组的对象,而看起来您正在传递字符串。您可以尝试更换
cadena2="{0} {1} {2}"
cadena2.format(self.cantidad,self.comida,self.edad)
与
cadena2 = [self.cantidad,self.comida,self.edad].
假设列表(self.cantidad,self.comida,self.edad)中的项目是数字而不是字符串,则可以使用。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。