我正在使用pymongo来查询区域中的所有项目(实际上是查询地图上某个区域中的所有场所).我db.command(SON())
之前用过搜索球形区域,它可以返回一个字典,在字典中有一个叫做results
包含场地的键.现在我需要搜索一个方形区域,我建议使用db.places.find
,但是,这会给我一个pymongo.cursor.Cursor
课程,我不知道如何从中提取场地结果.
有谁知道我是否应该将光标转换为字典并提取结果,或使用其他方法查询方形区域中的项目?BTW,db是pymongo.database.Database类
代码是:
>>> import pymongo
>>> db = pymongo.MongoClient(host).PSRC
>>> resp = db.places.find({"loc": {"$within": {"$box": [[ll_lng,ll_lat], [ur_lng,ur_lat]]}}})
>>> for doc in resp:
>>> print(doc)
Run Code Online (Sandbox Code Playgroud)
我有ll_lng,ll_lat,ur_lng和ur_lat的值,使用这些值,但它不打印此代码
我是tensorflow和word2vec的新手.我刚研究了word2vec_basic.py,它使用Skip-Gram
算法训练模型.现在我想训练使用CBOW
算法.如果我简单地逆转train_inputs
和,这是否可以实现train_labels
?
我正在编写一个python代码来更新集合中的每个文档.我的代码是这样的:
for r, d_50 in enumerate(grid50.find().batch_size(500)):
self_grid = grid50.find({'_id':d_50['_id']})
.....
.....
(processing process)
grid50.update({'_id':d_50['_id']},{'$set':{u'big_cell8':{"POI":venue_count, "cell_ids":cell_ids}}})
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,我遇到了问题:
raise AutoReconnect(str(e))
pymongo.errors.AutoReconnect: connection closed
Run Code Online (Sandbox Code Playgroud)
有谁知道如何处理这个问题?我应该在代码中添加一些东西来处理这个问题吗?
我对 WSL 很陌生。我想在我的 win10 PC 上的 ubuntu shell 上运行 python 代码。此代码需要访问网络摄像头,但似乎网络摄像头未正确打开..我在网上查过,我发现1-2年前的几个帖子说WSL无法访问集成网络摄像头..是否有任何更新或可以在 WSL 上使用网络摄像头的技巧?
非常感谢!
我使用GridSearchCV进行分类,我的代码是:
parameter_grid_SVM = {'dual':[True,False],
'loss':["squared_hinge","hinge"],
'penalty':["l1","l2"]
}
clf = GridSearchCV(LinearSVC(),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)
Run Code Online (Sandbox Code Playgroud)
然后,我遇到了错误
ValueError:不支持的参数集:仅当dual ='false'时支持penalty ='l1'.,参数:penalty ='l1',loss ='hinge',dual = False
稍后我将我的代码更改为:
clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),verbose=2)
Run Code Online (Sandbox Code Playgroud)
我遇到了错误
TypeError:init()至少需要3个参数(给定3个)
我也尝试过:
parameter_grid_SVM = {
'loss':["squared_hinge"]
}
clf = GridSearchCV(LinearSVC(penalty='l1',dual=False),param_grid=parameter_grid_SVM,verbose=2)
clf.fit(trian_data, labels)
Run Code Online (Sandbox Code Playgroud)
但是,我仍然有错误
ValueError:不支持的参数集:仅当dual ='false'时,支持penalty ='l1'.,参数:penalty ='l1',loss ='squared_hinge',dual = False
任何人都知道我该怎么做才能解决这个问题?
我正在使用Python。现在我有一个mongoDB数据库集合,其中所有文档都有这样的格式:
{"_id":ObjectId("53590a43dc17421e9db46a31"),
"latlng": {"type" : "Polygon", "coordinates":[[[....],[....],[....],[....],[.....]]]}
"self":{"school":2,"home":3,"hospital":6}
}
Run Code Online (Sandbox Code Playgroud)
其中,“self”字段表示Polygon中的场地类型以及对应的场地类型的数量。不同的文档有不同的 self 字段,例如 {"KFC":1,"building":2,"home":6}, {"shopping mall":1, "gas station":2}
现在我需要计算两个文档的两个“自身”字段之间的余弦相似度。之前,我的所有文档都以字典形式保存在pickle文件中,我使用以下代码来计算相似度:
vec = DictVectorizer()
total_arrays = vec.fit_transform(data + citymap).A
vector_matrix = total_arrays[:len(data)]
citymap_base_matrix = total_arrays[len(data):]
def cos_cdist(matrix, vector):
v = vector.reshape(1, -1)
return scipy.spatial.distance.cdist(matrix, v, 'cosine').reshape(-1)
for vector in vector_matrix:
distance_result = cos_cdist(citymap_base_matrix,vector)
Run Code Online (Sandbox Code Playgroud)
这里,数据和城市地图就像 [{"KFC":1,"building":2,"home":6},{"school":2,"home":3,"hospital":6}, {“购物中心”:1,“加油站”:2}]
但现在我正在使用 mongoDB,我想知道是否有 mongoDB 方法以更直接的方式计算相似度,有什么想法吗?
我想计算两个列表的余弦相似度,如下所示:
A = [u'home (private)', u'bank', u'bank', u'building(condo/apartment)','factory']
B = [u'home (private)', u'school', u'bank', u'shopping mall']
Run Code Online (Sandbox Code Playgroud)
我知道 A 和 B 的余弦相似度应该是
3/(sqrt(7)*sqrt(4)).
Run Code Online (Sandbox Code Playgroud)
我尝试将列表改成“home bank bank building factory”这样的形式,看起来像一个句子,但是,有些元素(例如home(私人))本身有空格,有些元素有括号,所以我觉得很难计算单词出现次数。
您知道如何计算这个复杂列表中的单词出现次数,以便对于列表 B,单词出现次数可以表示为
{'home (private):1, 'school':1, 'bank': 1, 'shopping mall':1}?
Run Code Online (Sandbox Code Playgroud)
或者你知道如何计算这两个列表的余弦相似度吗?
非常感谢
我有一个从 0 到 1 的 7130 个值的列表,这些值代表城市地图上相应 GPS 坐标的“热值”,我可以从数据文件中提取坐标。现在我想使用 python 生成热图,如下图所示。有谁知道这是怎么做到的吗?非常感谢
我有一个代码来计算两个矩阵之间的余弦相似度:
def cos_cdist_1(matrix, vector):
v = vector.reshape(1, -1)
return sp.distance.cdist(matrix, v, 'cosine').reshape(-1)
def cos_cdist_2(matrix1, matrix2):
return sp.distance.cdist(matrix1, matrix2, 'cosine').reshape(-1)
list1 = [[1,1,1],[1,2,1]]
list2 = [[1,1,1],[1,2,1]]
matrix1 = np.asarray(list1)
matrix2 = np.asarray(list2)
results = []
for vector in matrix2:
distance = cos_cdist_1(matrix1,vector)
distance = np.asarray(distance)
similarity = (1-distance).tolist()
results.append(similarity)
dist_all = cos_cdist_2(matrix1, matrix2)
results2 = []
for item in dist_all:
distance_result = np.asarray(item)
similarity_result = (1-distance_result).tolist()
results2.append(similarity_result)
Run Code Online (Sandbox Code Playgroud)
results
是
[[1.0000000000000002, 0.9428090415820635],
[0.9428090415820635, 1.0000000000000002]]
Run Code Online (Sandbox Code Playgroud)
但是,results2
是[1.0000000000000002, 0.9428090415820635, 0.9428090415820635, 1.0000000000000002]
我理想的结果是 …
我使用Python的sklearn
随机林(ensemble.RandomForestClassifier
)进行分类,并feature_importances_
用于查找分类器的重要功能.现在我的代码是:
for trip in database:
venue_feature_start.append(Counter(trip['POI']))
# Counter(trip['POI']) is like Counter({'school':1, 'hospital':1, 'bus station':2}),actually key is the feature
feat_loc_vectorizer = DictVectorizer()
feat_loc_vectorizer.fit(venue_feature_start)
feat_loc_orig_mat = feat_loc_vectorizer.transform(venue_feature_start)
orig_tfidf = TfidfTransformer()
orig_ven_feat = orig_tfidf.fit_transform(feat_loc_orig_mat.tocsr())
# so DictVectorizer() and TfidfTransformer() help me to phrase the features and for each instance, the feature dimension is 580, which means that there are 580 venue types
data = orig_ven_feat.tocsr()
le = LabelEncoder()
labels = le.fit_transform(labels_raw)
if "Unlabelled" in labels_raw:
unlabelled_int = …
Run Code Online (Sandbox Code Playgroud) python ×9
mongodb ×2
pymongo ×2
dictionary ×1
gps ×1
heatmap ×1
list ×1
matrix ×1
scikit-learn ×1
string ×1
svm ×1
tensorflow ×1
webcam ×1
windows-subsystem-for-linux ×1
word2vec ×1