我有这个值数组:
> df
[1] 2 0 0 2 2 0 0 1 0 1 2 1 0 1 3 0 0 1 1 0 0 0 2 1 2 1 3 1 0 0 0 1 1 2 0 1 3
[38] 1 0 2 1 1 2 2 1 2 2 2 1 1 1 2 1 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0
[75] …Run Code Online (Sandbox Code Playgroud) 我有一个矩阵10x4,我有一个有10个元素的向量.每个元素都是应该检索的矩阵的列索引.这是一个例子:
> M.mat
[,1] [,2] [,3] [,4]
[1,] -0.4236174 0.2228897 0.11676857 0.16906735
[2,] -0.4860078 0.9862164 -2.04735716 -0.33708521
[3,] -0.6931023 -0.2255126 -0.58214338 -0.08705187
[4,] 0.4048169 0.8713917 0.38543781 -1.38207954
[5,] 2.4005044 1.2483514 0.66759229 -1.33667156
[6,] -1.2083913 0.2389032 0.29554618 -0.05910570
[7,] 0.8055317 -0.7978780 -0.31873361 0.57248675
[8,] -0.1606493 0.4110878 0.90236993 -0.62311446
[9,] 0.3721249 0.5276403 -0.09323399 -0.41223947
[10,] 2.0704414 0.1747543 0.45456052 -1.09215597
> Idx
[1] 3 4 1 2 1 3 1 1 2 3
Run Code Online (Sandbox Code Playgroud)
这意味着我想从第2行第1,4行第3列第3列获取第3列,...
我试图创建一个包含两列的data.frame,一个是来自1; 10的row.indx,另一列是Idx,但它不起作用.任何建议如何访问指定的元素?
我正在使用格子包,我想为我的身材添加一个图例.auto.key和legend的文档非常混乱,无法找出添加图例的正确语法.这是我的代码:
xyplot(y ~ x, df, pch=19, col=rgb(0.2, 0.4, 0.8, 0.7), cex=2,
scales=list(cex=1.7),
xlab=list("x", cex=1.ales=list(cex=1.7),
xlab=list("x", cex=1.7), ylab=list("y", cex=1.7),
main=list("Linear Regression w. Polynomial Attributes", cex=1.6),
auto.key=T,
panel = function(x, y, ...) {
panel.xyplot(x, y, ...)
llines(x, predict(lm.xtend), col="purple", lwd=6, lty=3)
llines(x, predict(ridge.lin), col="darkgreen", lwd=6, lty=2)
})
Run Code Online (Sandbox Code Playgroud)
图表如下所示,所以我只想为这些线条添加一个图例.

我创建了一个表,如下所示:
mysql> create table testa (a int, b int, c real);
Query OK, 0 rows affected (0.14 sec)
Run Code Online (Sandbox Code Playgroud)
但是当我想实现这样的触发器时,我会遇到一些语法错误:
mysql> create trigger testa_trig
before insert ON testa
FOR EACH ROW
WHEN (NEW.c > 100)
BEGIN
Print "Warning: c > 100!"
END;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN (NEW.c > 100)
BEGIN
Print "Warning: c > 100!"
END' …Run Code Online (Sandbox Code Playgroud) 我有一个数组A如下:
import numpy as np
A = np.random.sample(100)
Run Code Online (Sandbox Code Playgroud)
我想从A创建2个随机子集,如果我将它们组合在一起,我将获得A.
inx = np.random.choice(np.arange(100), size=70, replace=False)
S1 = A[inx]
Run Code Online (Sandbox Code Playgroud)
因此,S1是其中一个子集,现在我如何构造S2以包含A中不在S1中的所有元素; 换句话说,S2 = A-S1.
所以,我有一个numpy字符串数组,我想用这个函数计算每对元素之间的成对编辑距离:来自http://docs.scipy.org/doc/scipy的 scipy.spatial.distance.pdist -0.13.0 /参考/生成/ scipy.spatial.distance.pdist.html
我的数组样本如下:
>>> d[0:10]
array(['TTTTT', 'ATTTT', 'CTTTT', 'GTTTT', 'TATTT', 'AATTT', 'CATTT',
'GATTT', 'TCTTT', 'ACTTT'],
dtype='|S5')
Run Code Online (Sandbox Code Playgroud)
但是,因为它没有'editdistance'选项,所以我想给出一个自定义的距离函数.我试过这个,我遇到了以下错误:
>>> import editdist
>>> import scipy
>>> import scipy.spatial
>>> scipy.spatial.distance.pdist(d[0:10], lambda u,v: editdist.distance(u,v))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/epd-7.3.2/lib/python2.7/site-packages/scipy/spatial/distance.py", line 1150, in pdist
[X] = _copy_arrays_if_base_present([_convert_to_double(X)])
File "/usr/local/epd-7.3.2/lib/python2.7/site-packages/scipy/spatial/distance.py", line 153, in _convert_to_double
X = np.double(X)
ValueError: could not convert string to float: TTTTT
Run Code Online (Sandbox Code Playgroud) 所以,我从FIFA worldcup网站解析HTML代码,并希望获得所有匹配:
wcup <- htmlTreeParse("http://www.fifa.com/worldcup/matches/", useInternalNodes=T)
Run Code Online (Sandbox Code Playgroud)
但是,一个国家的领域是't-nText kern',其他国家的领域是't-nText'.
<span class="t-nText kern">Bosnia and Herzegovina</span>
Run Code Online (Sandbox Code Playgroud)
因此,如果我使用此命令,我将错过'波斯尼亚和黑塞哥维那',就像这个命令:
xpathSApply(wcup, "//span[@class='t-nText ']", xmlValue)
Run Code Online (Sandbox Code Playgroud)
那么,有什么方法可以同时搜索属性't-nText'和't-nText kern'?或者你还有其他解决方案吗?我希望保持匹配的顺序.
xpath不支持逻辑OR:
xpathSApply(wcup, "//span[@class='t-nText ' || 't-nText kern']", xmlValue)
XPath error : Invalid expression
//span[@class='t-nText ' || 't-nText kern']
^
XPath error : Invalid expression
//span[@class='t-nText ' || 't-nText kern']
^
Error in xpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces = namespaces, :
error evaluating xpath expression //span[@class='t-nText ' || 't-nText kern']
Run Code Online (Sandbox Code Playgroud) 我已获取具有 3 个分区的 CelebA 数据集,如下所示
>>> celeba_bldr = tfds.builder('celeb_a')
>>> datasets = celeba_bldr.as_dataset()
>>> datasets.keys()
dict_keys(['test', 'train', 'validation'])
ds_train = datasets['train']
ds_test = datasets['test']
ds_valid = datasets['validation']
Run Code Online (Sandbox Code Playgroud)
现在,我想将它们全部合并到一个数据集中。例如,我需要将训练和验证结合在一起,或者可能将它们全部合并在一起,然后根据我自己的不同主题不相交标准将它们分开。有办法做到这一点吗?
我在文档中找不到任何选项来执行此操作https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/data/Dataset
import mpl_toolkits
import mpl_toolkits.basemap
#
# specify the map boundaries and projection type
#
mymap = mpl_toolkits.basemap.Basemap(llcrnrlon= -120, llcrnrlat=22,
urcrnrlon=-58, urcrnrlat=48,
projection="tmerc", lon_0 = -95, lat_0 = 35,
resolution = "l")
fig_map = plt.figure(6, figsize=(10, 8))
mymap.fillcontinents(color = "lightgray")
mymap.drawcoastlines(color = "gray", linewidth=1.2)
mymap.drawcountries(color = "gray", linewidth=2)
mymap.drawstates(color = "gray")
mymap.drawmapboundary()
plt.show()
Run Code Online (Sandbox Code Playgroud)
而matplotlib给了我这个美丽的情节:

现在,我想将这个导入到情节不合时宜的情节中
py.iplot_mpl(fig_map, filename='DataScience/data-visualization/geographic_map_plot_1')
/usr/local/lib/python2.7/dist-packages/plotly/matplotlylib/renderer.py:479: UserWarning:
I found a path object that I don't think is part of a bar chart. Ignoring.
Run Code Online (Sandbox Code Playgroud)
我收到几个奇怪的错误,如下所示:
---------------------------------------------------------------------------
KeyError Traceback (most recent …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用预制的估计器tf.estimator.DNNClassifier在 MNIST 数据集上使用。我从tensorflow_dataset.
我遵循以下四个步骤:首先构建数据集管道并定义输入函数:
## Step 1
mnist, info = tfds.load('mnist', with_info=True)
ds_train_orig, ds_test = mnist['train'], mnist['test']
def train_input_fn(dataset, batch_size):
dataset = dataset.map(lambda x:({'image-pixels':tf.reshape(x['image'], (-1,))},
x['label']))
return dataset.shuffle(1000).repeat().batch(batch_size)
Run Code Online (Sandbox Code Playgroud)
然后,在步骤 2 中,我使用单个键和形状 784 定义特征列:
## Step 2:
image_feature_column = tf.feature_column.numeric_column(key='image-pixels',
shape=(28*28))
image_feature_column
NumericColumn(key='image-pixels', shape=(784,), default_value=None, dtype=tf.float32, normalizer_fn=None)
Run Code Online (Sandbox Code Playgroud)
第 3 步,我将估算器实例化如下:
## Step 3:
dnn_classifier = tf.estimator.DNNClassifier(
feature_columns=image_feature_column,
hidden_units=[16, 16],
n_classes=10)
Run Code Online (Sandbox Code Playgroud)
最后,通过调用.train()方法使用估计器的步骤 4 :
## Step 4:
dnn_classifier.train(
input_fn=lambda:train_input_fn(ds_train_orig, batch_size=32),
#lambda:iris_data.train_input_fn(train_x, train_y, args.batch_size),
steps=20)
Run Code Online (Sandbox Code Playgroud)
但这会导致以下错误。看起来问题出在数据集上。
--------------------------------------------------------------------------- …Run Code Online (Sandbox Code Playgroud)