我有一个时间序列,其指数如下所示:
In [671]: indices
Out[671]:
DatetimeIndex(['2000-12-29', '2001-02-20', '2001-03-26', '2001-04-12',
'2001-04-24', '2001-07-05', '2001-08-15', '2001-09-10',
'2001-09-18', '2001-10-02', '2001-10-11', '2001-10-30',
'2001-12-13', '2002-03-07', '2002-06-13', '2002-09-12',
'2002-12-12', '2003-03-13', '2003-06-12', '2013-02-19',
'2013-05-28', '2013-09-03', '2014-01-21', '2014-02-18',
'2014-05-27', '2014-07-07', '2014-09-02', '2015-01-20',
'2015-02-17', '2015-05-26', '2015-07-06', '2016-05-31',
'2016-07-05', '2016-09-06', '2016-10-04', '2017-01-17',
'2017-02-21', '2017-05-30', '2017-09-05'],
dtype='datetime64[ns]', name='date', freq=None)
Run Code Online (Sandbox Code Playgroud)
由于频率不规则,我无法分配频率。
我的目标是获得一组移动 2 行的新索引(不是 2 个日历日期之后,而是数据中的两个日期之后)。
我尝试:
indices2= indices.shift(2)
Run Code Online (Sandbox Code Playgroud)
但它说:
ValueError: Cannot shift with no freq
Run Code Online (Sandbox Code Playgroud)
我想要的输出看起来像:
In [671]: indices2
Out[671]:
DatetimeIndex(['2000-02-20', '2001-03-26', '2001-04-12', ...., '2017-09-05'],
Run Code Online (Sandbox Code Playgroud) 我知道之前已经问过与内存错误相关的问题,例如这里、这里、这里、这里或这里。并且建议的解决方案始终是切换到 Python 3 和/或 Window 64 位,或者在代码错误的情况下修复代码。但是,我已经在使用 Python 3 和 Win 64。我还可以从 Windows 任务管理器中看到,当 Python 抛出内存错误时,我的 64GB 内存中有几 GB 仍然可用。
我有大约 15 个日期索引的 Pandas 数据帧,每个数据帧有 14000 行,平均有 5000 列浮点数据,以及大约 40-50% 的 NaN 值,我从硬盘驱动器读入。我不能简单地删除 NaN,因为不同的列在不同的日期有 NaN。当我尝试将它们与pd.concat(). 所以这不是一些错误的代码或while循环的问题。如果我将某些数据框排除在串联之外,则串联时不会发生内存错误,但是当我尝试对串联数据进行 Scikit 学习决策树分析时,就会发生这种情况。
我的问题是如何让 Python 使用所有可用内存而不抛出内存错误?
考虑一下这个系列:
s = [1, -1, 1, 1, 1, -1]
Run Code Online (Sandbox Code Playgroud)
计算此类序列中值变化的次数最省时的方法是什么?在此示例中,答案为 3(从 1 到 -1,再回到 1,再到 -1)
我阅读了许多与此类似的问题,但仍然无法弄清楚。
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
X_to_predict = array([[ 1.37097033e+002, 0.00000000e+000, -1.82710826e+296,
1.22703799e+002, 1.37097033e+002, -2.56391552e+001,
1.11457878e+002, 1.37097033e+002, -2.56391552e+001,
9.81898928e+001, 1.22703799e+002, -2.45139066e+001,
9.24341823e+001, 1.11457878e+002, -1.90236954e+001]])
clf.predict_proba(X_to_predict)
ValueError: Input contains NaN, infinity or a value too large for dtype('float32').
Run Code Online (Sandbox Code Playgroud)
我的问题既不是也不 nan是inf价值观,因为:
np.isnan(X_to_predict).sum()
Out[147]: 0
np.isinf(X_to_predict).sum()
Out[148]: 0
Run Code Online (Sandbox Code Playgroud)
问题:如何转换X_to_predict为对于 float32 来说不太大的值,同时保留尽可能多的小数点后位数?
请考虑接受两个参数的此函数:series和categorical_values.它的目标是获得a series,使其成为分类,然后打印原始系列的每个元素以及分类的相应元素.但是,如果categorical_values 已经将该函数作为输入传递给函数,则跳过分类阶段,该函数只打印传递的对series和categorical_values.
def my_function(series, categorical_values = None):
if categorical_values: #meant to mean "if this argument is passed, just use it"
categorical_values = categorical_values
else: #meant to mean "if this argument is not passed, create it"
categorical_values= pd.qcut(series, q = 5)
for i,j in zip(series, categorical_values):
print(i, j)
Run Code Online (Sandbox Code Playgroud)
但是,传递categorical_values以下内容:
my_function(series, pd.qcut(series, q = 5))
Run Code Online (Sandbox Code Playgroud)
导致:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), …Run Code Online (Sandbox Code Playgroud) 我有一个系列:
series = [0,2, 1, -2, 0, 0, 2, 3 ,1, 7]
Run Code Online (Sandbox Code Playgroud)
查找最长连续正数字符串的长度的最省时方法是什么?在这个例子中,它必须是 4(长度为 [2, 3, 1, 7])