我想快速访问np.array元素,例如从0-6加10到最后的索引。到目前为止,我已经尝试过:
Fold_5 = len(predictorX)/5
trainX = np.concatenate(predictorX[:3*int(Fold_5)],predictorX[4*int(Fold_5)])
或
[[0.1,0.4,0.6,0.2],[..]....]
两个都给了我错误,第二个给了我:“TypeError:只有整数标量数组可以转换为标量索引”
编辑:concatenate仍然给我带来问题,所以我将在这里发布我的完整代码:
set.seed(123)
names <- c("acquisitionsmergers", "analystratings", "assets", "balanceofpayments",
"bankruptcy", "civilunrest", "corporateresponsibility", "credit",
"creditratings", "crime", "dividends", "earnings", "equityactions",
"exploration", "government", "indexes", "industrialaccidents",
"insidertrading", "investorrelations", "laborissues", "legal",
"marketing", "orderimbalances", "partnerships", "pricetargets",
"productsservices", "publicopinion", "regulatory", "revenues",
"security", "stockprices", "taxes", "technicalanalysis", "transportation",
"warconflict")
mylabels <- c("acquisitionsmergers" = "Acquisitions/Mergers",
"analystratings" = "Analyst Ratings",
"assets" = "Assets",
"balanceofpayments" = "Balance of Payments",
"bankruptcy" = "Bankruptcy",
"civilunrest" = "Civil Unrest",
"corporateresponsibility" = "Corporate Responsibility",
"credit" = "Credit",
"creditratings" = "Credit Ratings",
"crime" = "Crime",
"dividends" = "Dividends",
"earnings" = "Earnings",
"equityactions" = "Equity Actions",
"exploration" = "Exploration",
"government" = "Government",
"indexes" = "Indexes",
"industrialaccidents" = "Industrial Accidents",
"insidertrading" = "Insider Trading",
"investorrelations" = "Investor Relations",
"laborissues" = "Labor Issues",
"legal" = "Legal",
"marketing" = "Marketing",
"orderimbalances" = "Order Imbalances",
"partnerships" = "Partnerships",
"pricetargets" = "Price Targets",
"productsservices" = "Product Services",
"publicopinion" = "Public Opinion",
"regulatory" = "Regulatory",
"revenues" = "Revenues",
"security" = "Security",
"stockprices" = "Stockprices",
"taxes" = "Taxes",
"technicalanalysis" = "Technical Analysis",
"transportation" = "Transportation",
"warconflict" = "War Conflict")
df <- data.frame(item = rep(names, each=5), value=rnorm(5*35,5,2), date = rep(seq(as.Date("2000/1/1"), by = "month", length.out = 5),35))
预测变量X是一个值为
的数组library(ggplot2)
ggplot(df, aes(x=date, y=value, color=item)) +
geom_line() +
facet_wrap( ~ item, ncol=4, scales="free_y", labeller = mylabels)
答案 0 :(得分:1)
以下是获得所需子阵列的两种简短方法:
np.delete(a, np.s_[6:10])
和
np.r_[a[:6], a[10:]]
答案 1 :(得分:1)
在:
a[0:6,10:]
0:6选择行,10:选择列。如果a
不是2d或足够大会导致错误。
在
np.concatenate(a[0:6],a[10:])
问题是参数的数量;它需要一个数组列表。第二个,如果给出被理解为axis
,它应该是一个整数(因此你的错误)。
np.concatenate([a[0:6],a[10:]])
应该有用。
另一个选择是使用列表
进行索引a[0,1,2,3,4,5,10,11,...]]
np.r_
是构建此类列表的便捷小工具:
In [73]: np.r_[0:6, 10:15]
Out[73]: array([ 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14])
它实际上是np.concatenate([np.arange(0,6),np.arange(10,15)])
。
无论是首先索引还是连接,或者先连接索引然后索引,都无关紧要。效率大致相同。 np.delete
选择多种方法,包括这些方法,具体取决于&#39;删除&#39;的大小和类型。区域。
在trainX
表达式中,将[]添加到连接调用应该有效。但是,predictorX[4*Fold_5]
可能是个问题。你错过了:
(例如10:例子)吗?如果您只想要一个值,那么您需要将其转换为1d,例如predictorX[[4*Fold_5]]
Fold_5 = len(predictorX)//5 # integer division in py3
trainX = np.concatenate([predictorX[:3*Fold_5], predictorX[4*Fold_5:]])
答案 2 :(得分:0)
np.concatenate
采用一系列数组。试试
np.concatenate([a[0:6],a[10:]])
或
np.concatenate((a[0:6],a[10:]))