我正在使用clipboard.js从a复制一些文本textarea,而且工作正常,但我想显示一个工具提示"复制!" 如果它被成功复制,就像在他们的网站上给出的例子中那样.
这是一个没有显示工具提示的工作示例:https://jsfiddle.net/5j50jnhj/
例如,如果我有列表
a = [1,1,1,2,2]
b = [1,1,2,2,2]
c = [2,1,1,1,1]
Run Code Online (Sandbox Code Playgroud)
我想要获得列表中第一个元素的最长条纹,所以例如a将给出3,b将给出2并且c将给出1.我知道我可以创建一个while循环并计算条纹,但我想知道如果有一个更优雅的方式来做到这一点?
我有以下代码:
x1 = np.random.randn(100)
y1 = np.random.randn(100) + 3
x2 = np.random.randn(100) + 3
y2 = np.random.randn(100)
plt.plot(x1, y1, "+", x2, y2, "x")
plt.axis('equal')
plt.show()
Run Code Online (Sandbox Code Playgroud)
这导致以下图像
我已经实现了自己的逻辑回归,这会返回一个theta,我想用这个θ来绘制决策边界,但我不知道该怎么做.
X = np.matrix(np.vstack((np.hstack((x1,x2)), np.hstack((y1,y2)))).T)
X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)
Y = np.matrix(1.0 * np.hstack((np.zeros(100), np.ones(100)))).T
learning_rate = 0.0001
iterations = 3000
theta = np.matrix([[0.5], [0.5], [0.5]])
theta = logistic_regression(theta, X, Y, learning_rate, iterations)
Run Code Online (Sandbox Code Playgroud)
这给了theta=
[[ 0.40377942]
[ 0.53696461]
[ 0.1398419 ]]
Run Code Online (Sandbox Code Playgroud)
例如.我如何使用它来绘制决策边界?
我正在尝试实现自己的kNN分类器.我已经设法实现了一些东西,但速度非常慢......
def euclidean_distance(X_train, X_test):
"""
Create list of all euclidean distances between the given
feature vector and all other feature vectors in the training set
"""
return [np.linalg.norm(X - X_test) for X in X_train]
def k_nearest(X, Y, k):
"""
Get the indices of the nearest feature vectors and return a
list of their classes
"""
idx = np.argpartition(X, k)
return np.take(Y, idx[:k])
def predict(X_test):
"""
For each feature vector get its predicted class
"""
distance_list = [euclidean_distance(X_train, X) for X …Run Code Online (Sandbox Code Playgroud) 有没有一种快速的方法(所以没有 for 循环)来查找 numpy 数组中每行的多数元素并从中创建一个新数组?
例如,如果您有以下 numpy 数组:
X =
[[ 1. 1. 1.]
[ 1. 0. 1.]
[ 1. 0. 1.]
[ 1. 1. 1.]
[ 1. 0. 1.]
[ 1. 0. 1.]
[ 0. 0. 0.]
[ 1. 1. 1.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
Run Code Online (Sandbox Code Playgroud)
你可以这样做get_majority(X),这将输出
[ 1. 1. 1. 1. 1. 1. 0. 1. 0. 0. 0.]
Run Code Online (Sandbox Code Playgroud)
我已经尝试通过循环矩阵并使用Counterfromcollections来做到这一点,但这对于大型矩阵来说非常慢,所以我想找到一种矢量化的方法来做到这一点。
我有一个包含两个textareas和两个buttons 的页面,textarea当按下时它们分别将每个内容复制到用户剪贴板。当按下按钮并且复制成功时,工具提示显示Copied!。按照我之前的设置方式,当按下1个按钮时,将显示两个按钮的工具提示,而不仅仅是一个按钮的提示(请参见此处)。
$('button').tooltip({
trigger: 'click',
placement: 'bottom'
});
function setTooltip(message) {
$('button').tooltip('hide')
.attr('data-original-title', message)
.tooltip('show');
}
function hideTooltip() {
setTimeout(function() {
$('button').tooltip('hide');
}, 1000);
}
var clipboard = new Clipboard('button');
clipboard.on('success', function(e) {
e.clearSelection();
setTooltip('Copied!');
hideTooltip();
});
clipboard.on('error', function(e) {
e.clearSelection();
setTooltip('Failed!');
hideTooltip();
});
Run Code Online (Sandbox Code Playgroud)
在基本复制了上面的代码之后,我设法做到了,以便仅为所单击的按钮显示工具提示(请参阅此处),但这似乎是一种非常入门的方式(即我)。
我正在尝试在Python中实现k最近邻分类器,因此我想计算欧几里得距离。我有一个数据集,我已经将其转换为一个大的numpy数组
[[ 0. 0. 4. ..., 1. 0. 1.]
[ 0. 0. 5. ..., 0. 0. 1.]
[ 0. 0. 14. ..., 16. 9. 1.]
...,
[ 0. 0. 3. ..., 2. 0. 3.]
[ 0. 1. 7. ..., 0. 0. 3.]
[ 0. 2. 10. ..., 0. 0. 3.]]
Run Code Online (Sandbox Code Playgroud)
每行的最后一个元素指示类。因此,在计算欧几里得距离时,我显然不想包含最后一个元素。我以为我可以做到以下几点
for row in dataset:
distance = euclidean_distance(vector, row[:dataset.shape[1] - 1])
Run Code Online (Sandbox Code Playgroud)
但这仍然包括最后一个元素
print row
>>> [[ 0. 0. 4. ..., 1. 0. 1.]]
print row[:dataset.shape[1] - 1]
>>> [[ 0. …Run Code Online (Sandbox Code Playgroud) 我正在抓取页面,并从该页面上的表中获取所有<tr>元素,如下所示:
r = requests.get("http://lol.esportswikis.com/wiki/G2_Esports/Match_History")
s = BeautifulSoup(r.content, "lxml")
tr = s.find_all("table", class_="wikitable sortable")[0].find_all("tr")[3:]
print tr[0]
Run Code Online (Sandbox Code Playgroud)
输出:
<tr style="background-color:#C6EFCE"><td>...</td> ... <td>...</td></tr>
Run Code Online (Sandbox Code Playgroud)
现在,我试图获取<tr>标签的样式,但是我不知道如何。例如,如果我这样做:
for item in tr[0]:
print item
Run Code Online (Sandbox Code Playgroud)
它显然只是打印<td> ... </td>东西。我想我大概可以做这样的事情print tr[0].something,比如tr[0].tag,但一切到目前为止,我已经尝试并没有带来我想要的东西。