我正在尝试计算从scikit-learn返回的Scipy稀疏矩阵上的最近邻居聚类DictVectorizer.但是,当我尝试使用scikit-learn计算距离矩阵时,我会使用' pairwise.euclidean_distances和'的'euclidean'距离得到一条错误信息pairwise.pairwise_distances.我的印象是scikit-learn可以计算这些距离矩阵.
我的矩阵非常稀疏,形状为:<364402x223209 sparse matrix of type <class 'numpy.float64'>
with 728804 stored elements in Compressed Sparse Row format>.
我也曾尝试的方法,如pdist与kdtree在SciPy的,但已经收到不能够处理结果的其他错误.
任何人都可以请我指出一个有效地允许我计算距离矩阵和/或最近邻结果的解决方案吗?
一些示例代码:
import numpy as np
from sklearn.feature_extraction import DictVectorizer
from sklearn.neighbors import NearestNeighbors
from sklearn.metrics import pairwise
import scipy.spatial
file = 'FileLocation'
data = []
FILE = open(file,'r')
for line in FILE:
templine = line.strip().split(',')
data.append({'user':str(int(templine[0])),str(int(templine[1])):int(templine[2])})
FILE.close()
vec = DictVectorizer()
X = vec.fit_transform(data)
result = scipy.spatial.KDTree(X)
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most …Run Code Online (Sandbox Code Playgroud) 我正在使用 mitmproxy 做一些工作,想知道如何获取通过 GET/POST 命令传递的标头和数据。例如,我看到 mitmproxy 输出以下内容:
IPADDRESS GET http://www.example.com/wp-content/uploads/2015/01/9418ASD23801.jpg
Run Code Online (Sandbox Code Playgroud)
但我想查看正在传递的所有数据,包括用户代理、标头等。帖子示例也是如此。
我正在寻找一种非常pythonic的方式(Python 3.x)做下面的事情,但尚未提出一个.如果我有以下字符串:
string = 'this is a test string'
Run Code Online (Sandbox Code Playgroud)
我可以用它来标题:
string.title()
Run Code Online (Sandbox Code Playgroud)
结果如下:
'This Is A Test String'
Run Code Online (Sandbox Code Playgroud)
但是,如果我有以下字符串,我想转换:
string = 'Born in the USA'
Run Code Online (Sandbox Code Playgroud)
应用标题案例会导致:
string = 'Born In The Usa'
Run Code Online (Sandbox Code Playgroud)
应该导致:
'Born In The USA'
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来做一个标题案例,但不是调整现有的大写文本.有没有办法做到这一点?
我最初有一个带有单个应用程序的django项目,所有模型都在该应用程序中定义.该项目在启动时仅使用默认数据库.它现在已成为一个笨拙的应用程序,我正在尝试分解为较小的应用程序.这样做,我想为不同的应用程序使用不同的数据库.我在settings.py文件中设置了新数据库和路由器.但是,我对如何将现有表迁移到新数据库感到困惑.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'db_user_name',
'PASSWORD': 'password',
'HOST': 'hostname',
'PORT': '3306',
},
'db2': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name2',
'USER': 'db_user_name2',
'PASSWORD': 'password2',
'HOST': 'hostname2',
'PORT': '3306',
}
}
Run Code Online (Sandbox Code Playgroud)
我希望应用程序(例如app1)从默认值移动到db2.路由器已经知道将app1指定给db2但是正在运行的迁移什么都不做.有任何想法吗?
我有一个Elasticsearch索引,我试图根据多个字段(标题和描述)搜索匹配项。如果标题中出现了特定的术语,我希望能够将分数提高2 *原始分数。如果在说明中,则应保留原始分数。我对elasticsearch文档有些困惑。谁能帮助我调整以下查询以反映此逻辑?
{
"query": {
"query_string": {
"query": "string",
"fields": ["title","description"]
}
}
}
Run Code Online (Sandbox Code Playgroud) 我注意到NLTK send_tokenizer在某些日期上会犯错误。有什么方法可以对其进行调整,以便它可以正确标记以下内容:
valid any day after january 1. not valid on federal holidays, including february 14,
or with other in-house events, specials, or happy hour.
Run Code Online (Sandbox Code Playgroud)
当前运行send_tokenize结果为:
['valid any day after january 1. not valid on federal holidays, including february 14,
or with other in-house events, specials, or happy hour.']
Run Code Online (Sandbox Code Playgroud)
但这应导致:
['valid any day after january 1.', 'not valid on federal holidays, including february 14,
or with other in-house events, specials, or happy hour.']
Run Code Online (Sandbox Code Playgroud)
因为“ 1月1日”之后的时间段是合法的句子终止字符。
如何__init__在基本抽象类和派生抽象类中定义函数并self.*在抽象方法中都可用?例如:
利用在抽象类的基类中导入的函数的正确方法是什么?例如:在base.py中我有以下内容:
import abc
class BasePizza(object):
__metaclass__ = abc.ABCMeta
def __init__(self):
self.firstname = "My Name"
@abc.abstractmethod
def get_ingredients(self):
"""Returns the ingredient list."""
Run Code Online (Sandbox Code Playgroud)
然后我在diet.py中定义方法:
import base
class DietPizza(base.BasePizza):
def __init__(self):
self.lastname = "Last Name"
@staticmethod
def get_ingredients():
if functions.istrue():
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
但是,当我运行时,diet.py我只能访问self.lastname.我想要DietPizza两个self.firstname和self.lastname.我怎样才能做到这一点?
如何在MySQL中选择特定字段值不唯一的所有行.例如,我有以下数据:
---------------------------------------
| ID | Name | URL |
---------------------------------------
| 1 | Store 1| http://www.store1.com |
| 2 | Store 2| http://www.store1.com |
| 3 | Store 3| http://www.store3.com |
| 4 | Store 4| http://www.store4.com |
| 5 | Store 5| http://www.store4.com |
---------------------------------------
Run Code Online (Sandbox Code Playgroud)
在这里我想要返回URL字段有重复的以下内容:
---------------------------------------
| ID | Name | URL |
---------------------------------------
| 1 | Store 1| http://www.store1.com |
| 2 | Store 2| http://www.store1.com |
| 4 | Store 4| http://www.store4.com |
| 5 | …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用bs4/Python 3中的BeautifulSoup来提取CData.但是,每当我使用以下内容搜索它时,它都会返回一个空结果.谁能指出我做错了什么?
from bs4 import BeautifulSoup,CData
txt = '''<foobar>We have
<![CDATA[some data here]]>
and more.
</foobar>'''
soup = BeautifulSoup(txt)
for cd in soup.findAll(text=True):
if isinstance(cd, CData):
print('CData contents: %r' % cd)
Run Code Online (Sandbox Code Playgroud) 我有一个要在Phoenix / Elixir中执行的模型。该模型基本上计算用户花费的金额的总和。模型(Receipts)如下所示:
---------------
| ID | Amount |
---------------
| 1 | 0 |
| ...| ... |
| 5 | 4 |
---------------
Run Code Online (Sandbox Code Playgroud)
上有一个唯一索引ID。我想将用户插入表中(我定义ID),然后在用户不存在的情况下设置金额,否则将新金额添加到现有金额中。例如,执行:
Repo.insert(Users.changeset(%Users{}, %{ID: 1, Amount: 10})
Run Code Online (Sandbox Code Playgroud)
将导致:
---------------
| ID | Amount |
---------------
| 1 | 11 |
| ...| ... |
| 5 | 4 |
---------------
Run Code Online (Sandbox Code Playgroud)
并执行Repo.insert(%Users {},Users.changeset(%{ID:6,Amount:5})将导致:
---------------
| ID | Amount |
---------------
| 1 | 11 |
| ...| ... |
| 5 | 4 …Run Code Online (Sandbox Code Playgroud)