我正在使用 sklearn 包的 KNN 分类器处理数值数据集。
预测完成后,前 4 个重要变量应显示在条形图中。
这是我尝试过的解决方案,但它抛出一个错误,即 feature_importances 不是 KNNClassifier 的属性:
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X_train, y_train)
y_pred = neigh.predict(X_test)
(pd.Series(neigh.feature_importances_, index=X_test.columns)
.nlargest(4)
.plot(kind='barh'))
Run Code Online (Sandbox Code Playgroud)
现在显示决策树的变量重要性图:传递给 pd.series() 的参数是 classifier.feature_importances_
对于 SVM,线性判别分析,传递给 pd.series() 的参数是 classifier.coef_[0]。
但是,我无法为 KNN 分类器找到合适的参数。
我正在使用 nginx、gunicorn、postgresql 在生产模式(debug=false)下测试我的 django 应用程序。
虽然我能够渲染静态文件,但无法访问存储在“media”文件夹中的文件。
在我的 settings.py 中,以下是设置的变量:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# also tried another combination: MEDIA_ROOT = 'media'
Run Code Online (Sandbox Code Playgroud)
同样在 urls.py 中 MEDIA_ROOT 设置如下:
urlpatterns = [
path('admin/', admin.site.urls),
path('venter/', include('appname.urls')),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Run Code Online (Sandbox Code Playgroud)
在我的 /etc/nginx/sites-available/ 文件中,我有以下设置:
server {
listen 80;
server_name website.com www.website.com ;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /home/a/btawebsite;
}
location /media/ {
root /home/a/btawebsite;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/a/myproject.sock;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,在渲染存储在我的 …
在我的 Django 应用程序中,我使用 celery @task 过滤器定义了一个函数。
# tasks.py
# import statements
@app.task
def categorise():
# LOC
return results # results should be returning json data
Run Code Online (Sandbox Code Playgroud)
我正在使用 Celery 的 .delay() 方法来调用此函数:
def driver(self):
results = categorise.delay()
print("type of results: ", type(results))
return results
Run Code Online (Sandbox Code Playgroud)
results 变量中返回的类型为:
type of results: <class 'celery.result.AsyncResult'>
Run Code Online (Sandbox Code Playgroud)
我遇到以下错误:
Object of type 'AsyncResult' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
在我的 Django settings.py 文件中,我添加了以下配置,但没有帮助:
# REDIS related settings
REDIS_HOST = 'localhost'
REDIS_PORT = '6379'
BROKER_URL = 'redis://' + REDIS_HOST + ':' + …Run Code Online (Sandbox Code Playgroud) 我有一个 Django 博客应用程序,其中在发布博客时,也可以附加并提交文件。
这是views.py代码:
def user_own_blog(request):
if request.method == 'POST' and request.FILES['blog_document']:
title_b = request.POST.get('blog_title')
content_b = request.POST.get('blog_content')
file1 = request.FILES['blog_document']
fs = FileSystemStorage()
document_name = fs.save(file1.name, file1)
uploaded_document_url = fs.url(document_name)
b = Blog(title=title_b, content=content_b, blog_document=uploaded_document_url)
b.save()
return render(request, 'mysite/portfolio.html')
else:
return render(request, 'mysite/blog.html')
Run Code Online (Sandbox Code Playgroud)
这是 MEDIA_ROOT 和 MEDIA_URL 路径名:
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Run Code Online (Sandbox Code Playgroud)
以下是 mysite 应用程序中 urls.py 的代码:
urlpatterns=[ .....
......
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Run Code Online (Sandbox Code Playgroud)
这是项目结构,其中我需要将媒体文件夹直接存在于Assignment1项目下
当文件上传成功时,它在 /api 中显示如下。
但是正在创建两个媒体路径: /media/media ,如下所示: 我无法找到我可能创建的重复字段。
单击文件链接时:出现 …