我使用DBSCAN使用Scikit-Learn(Python 2.7)聚集一些数据:
from sklearn.cluster import DBSCAN
dbscan = DBSCAN(random_state=0)
dbscan.fit(X)
Run Code Online (Sandbox Code Playgroud)
但是,我发现没有内置函数(除了"fit_predict"之外)可以将新数据点Y分配给原始数据中标识的簇X.K-means方法有一个"预测"功能,但我希望能够对DBSCAN做同样的事情.像这样的东西:
dbscan.predict(X, Y)
Run Code Online (Sandbox Code Playgroud)
因此密度可以从X推断,但返回值(集群分配/标签)仅适用于Y.从我所知道的,这个功能在R中可用,所以我假设它在某种程度上也可用于Python.我似乎无法找到任何相关的文档.
此外,我已经尝试搜索为什么DBSCAN不能用于标记新数据的原因,但我没有找到任何理由.
我正在寻找一种方法来关闭iPython笔记本中的自动保存功能.我已经看到通过Google/Stack Overflow搜索引用如何打开自动保存,但我想要相反(关闭自动保存).如果这是可以永久设置而不是在每个笔记本的顶部,那将是优惠的.
我有一个带有两个id变量的pandas数据帧:
df = pd.DataFrame({'id': [1,1,1,2,2,3],
'num': [10,10,12,13,14,15],
'q': ['a', 'b', 'd', 'a', 'b', 'z'],
'v': [2,4,6,8,10,12]})
id num q v
0 1 10 a 2
1 1 10 b 4
2 1 12 d 6
3 2 13 a 8
4 2 14 b 10
5 3 15 z 12
Run Code Online (Sandbox Code Playgroud)
我可以用以下方式转动表:
df.pivot('id','q','v')
Run Code Online (Sandbox Code Playgroud)
并最终结束了一些事情:
q a b d z
id
1 2 4 6 NaN
2 8 10 NaN NaN
3 NaN NaN NaN 12
Run Code Online (Sandbox Code Playgroud)
但是,我真正想要的是(原始的未融合形式):
id num a b d …Run Code Online (Sandbox Code Playgroud) 根据这个答案,您可以从Python脚本中导入pip并使用它来安装模块.有可能这样做conda install吗?
conda文档仅显示命令行中的示例,但我正在寻找可以在Python脚本中执行的代码.
是的,我可以从脚本中执行shell命令,但我试图避免这种情况,因为它基本上假设无法导入conda并调用其函数.
我有一个数据帧:
df = pd.DataFrame([[2, 4, 7, 8, 1, 3, 2013], [9, 2, 4, 5, 5, 6, 2014]], columns=['Amy', 'Bob', 'Carl', 'Chris', 'Ben', 'Other', 'Year'])
Run Code Online (Sandbox Code Playgroud)
Amy Bob Carl Chris Ben Other Year
0 2 4 7 8 1 3 2013
1 9 2 4 5 5 6 2014
Run Code Online (Sandbox Code Playgroud)
还有一本字典:
d = {'A': ['Amy'], 'B': ['Bob', 'Ben'], 'C': ['Carl', 'Chris']}
Run Code Online (Sandbox Code Playgroud)
我想重塑我的数据框看起来像这样:
Group Name Year Value
0 A Amy 2013 2
1 A Amy 2014 9
2 B Bob 2013 4
3 B …Run Code Online (Sandbox Code Playgroud) 我正在使用此处的输入数据(参见第3.1节).
我试图使用scikit-learn重现它们的协方差矩阵,特征值和特征向量.但是,我无法重现数据源中显示的结果.我也在其他地方看过这个输入数据,但是我无法辨别它是scikit-learn,我的步骤还是数据源的问题.
data = np.array([[2.5,2.4],
[0.5,0.7],
[2.2,2.9],
[1.9,2.2],
[3.1,3.0],
[2.3,2.7],
[2.0,1.6],
[1.0,1.1],
[1.5,1.6],
[1.1,0.9],
])
centered_data = data-data.mean(axis=0)
pca = PCA()
pca.fit(centered_data)
print(pca.get_covariance()) #Covariance Matrix
array([[ 0.5549, 0.5539],
[ 0.5539, 0.6449]])
print(pca.explained_variance_ratio_) #Eigenvalues (normalized)
[ 0.96318131 0.03681869]
print(pca.components_) #Eigenvectors
[[-0.6778734 -0.73517866]
[ 0.73517866 -0.6778734 ]]
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,投影与来自上述数据源的结果相匹配.
print(pca.transform(centered_data)) #Projections
array([[-0.82797019, 0.17511531],
[ 1.77758033, -0.14285723],
[-0.99219749, -0.38437499],
[-0.27421042, -0.13041721],
[-1.67580142, 0.20949846],
[-0.9129491 , -0.17528244],
[ 0.09910944, 0.3498247 ],
[ 1.14457216, -0.04641726],
[ 0.43804614, -0.01776463],
[ 1.22382056, 0.16267529]])
Run Code Online (Sandbox Code Playgroud)
这是我不明白的:
假设我有一个 NumPy 数组:
x = np.array([0, 1, 2, 0, 4, 5, 6, 7, 0, 0])
Run Code Online (Sandbox Code Playgroud)
在每个索引处,我想找到到最近的零值的距离。如果位置本身为零,则返回零作为距离。之后,我们只对与当前位置右侧的最近零的距离感兴趣。超级幼稚的方法是这样的:
out = np.full(x.shape[0], x.shape[0]-1)
for i in range(x.shape[0]):
j = 0
while i + j < x.shape[0]:
if x[i+j] == 0:
break
j += 1
out[i] = j
Run Code Online (Sandbox Code Playgroud)
输出将是:
array([0, 2, 1, 0, 4, 3, 2, 1, 0, 0])
Run Code Online (Sandbox Code Playgroud)
我注意到输出中零之间的倒计时/递减模式。所以,我也许可以使用零的位置(即,zero_indices = np.argwhere(x == 0).flatten())
在线性时间内获得所需输出的最快方法是什么?
是否有自动方法来维护返回的数据帧的列('C','B','A')的顺序?
g = df.groupby(['people'])
g['people'].agg({'C' : len,
'B' : len,
'A' : len,
})
Run Code Online (Sandbox Code Playgroud)
这将返回A,B,C而不是C,B,A的列.
我只能找到示例,但不能找到agg函数本身的文档.
这似乎是一种解决方法:
g = df.groupby(['people'])
g['people'].agg({'C' : len,
'B' : len,
'A' : len,
}).reindex_axis(['C','B','A'], axis=1)
Run Code Online (Sandbox Code Playgroud) 看来pandas read_csv函数只允许使用单字符分隔符/分隔符.有没有办法允许使用一串字符,比如"*|*"或"%%"?
我有一个二维数组(它实际上非常大并且是另一个数组的视图):
x = np.array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5]]
)
Run Code Online (Sandbox Code Playgroud)
我有一个处理数组每一行的函数:
def some_func(a):
"""
Some function that does something funky with a row of numbers
"""
return [a[2], a[0]] # This is not so funky
np.apply_along_axis(some_func, 1, x)
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是调用该np.apply_along_axis函数的某种方式,以便我可以访问行索引(对于正在处理的行),然后能够使用此函数处理每一行:
def some_func(a, idx):
"""
I plan to use the index for some logic on which columns to
return. This is only an example
"""
return [idx, a[2], a[0]] # This is not so …Run Code Online (Sandbox Code Playgroud) python ×8
pandas ×4
numpy ×2
scikit-learn ×2
anaconda ×1
autosave ×1
conda ×1
csv ×1
data-mining ×1
dbscan ×1
installation ×1
pca ×1
predict ×1
python-2.7 ×1