[英]How can I remove newlines if they aren't at the beginning or at the end of a string?
[英]How can I add to the end of a numpy array and remove from the beginning?
我有一個形狀為: (1, 100)
的數組,它是:
[[1. 2. 3. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 55.]]
我想在最后添加一些東西(比如123
這樣的數字)並刪除第一個元素,這樣我就可以:
[[2. 3. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 55. 123.]]
保留原始形狀(1, 100)
我在嘗試:
x_pred = np.append(x_pred, next_index, axis=1)
( x_pred
是一個(1, 100)
數組, next_index
是一個標量)
但是我收到一個錯誤:
ValueError: all the input arrays must have same number of dimensions
我究竟做錯了什么?
你可以用roll
做到這一點。
a = np.zeros((1,10))
#roll and replace
a[0] = np.roll(a[0],-1)
a[0][-1] = new_value
import numpy as np
x_pred = np.zeros((1,100))
x_pred = np.insert(x_pred, x_pred.size, 123, 1)
x_pred = np.delete(x_pred, 0, axis=1)
x_pred打印:
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
123.]])
尺寸為100
下面不適用於二維數組,但對於一維數組是正確的:
使用numpy插入可以執行此操作
import numpy as np
tmp = np.array([0,1,2,3])
tmp = np.insert(tmp[1:], tmp.size-1, 123)
# [ 1, 2, 3, 123]
或者更像你的例子
import numpy as np
tmp = np.array([0,0,0,0])
tmp = np.insert(tmp[1:], tmp.size-1, 123)
# [ 0, 0, 0, 123]
在np.insert()
,第一個參數是要插入的數組,第二個是要插入的索引,第三個是要插入的值。
tmp[1:]
只是說除了第一個元素(即第0個元素)之外的所有內容直到結尾。
x_pred = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,])
x_pred = np.insert(x_pred[1:], x_pred.size-1, 123)
x_pred
打印:
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 123])
和x_pred.size
打印100
將標量作為列表附加到列表中,以使其具有相同的維數 :
>>> a = np.array([[0,1,2,3]])
>>> a
array([[0, 1, 2, 3]])
>>> q = 123
>>> np.append(a[:,1:],[[q]], axis=1)
array([[ 1, 2, 3, 123]])
>>>
要么
>>> b = np.array([[q]])
>>> np.concatenate([a[:,1:],b], 1)
array([[ 1, 2, 3, 123]])
>>> np.concatenate([a[:,1:],[[q]]], 1)
array([[ 1, 2, 3, 123]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.