我有一个一维数组,每个元素都有大字符串.我试图使用a CountVectorizer
将文本数据转换为数字向量.但是,我收到一个错误说:
AttributeError: 'numpy.ndarray' object has no attribute 'lower'
Run Code Online (Sandbox Code Playgroud)
mealarray
每个元素中包含大字符串.有5000个这样的样本.我正在尝试对此进行矢量化,如下所示:
vectorizer = CountVectorizer(
stop_words='english',
ngram_range=(1, 1), #ngram_range=(1, 1) is the default
dtype='double',
)
data = vectorizer.fit_transform(mealarray)
Run Code Online (Sandbox Code Playgroud)
完整的堆栈跟踪:
File "/Library/Python/2.7/site-packages/sklearn/feature_extraction/text.py", line 817, in fit_transform
self.fixed_vocabulary_)
File "/Library/Python/2.7/site-packages/sklearn/feature_extraction/text.py", line 748, in _count_vocab
for feature in analyze(doc):
File "/Library/Python/2.7/site-packages/sklearn/feature_extraction/text.py", line 234, in <lambda>
tokenize(preprocess(self.decode(doc))), stop_words)
File "/Library/Python/2.7/site-packages/sklearn/feature_extraction/text.py", line 200, in <lambda>
return lambda x: strip_accents(x.lower())
AttributeError: 'numpy.ndarray' object has no attribute 'lower'
Run Code Online (Sandbox Code Playgroud) 如果我有这样的 DataFrame
Date value
04 May 2015 1
06 May 2015 1
07 May 2015 1
11 May 2015 1
11 May 2015 1
Run Code Online (Sandbox Code Playgroud)
如何获得日期索引的差异?即下面的第三列:
Date value Diff
04 May 2015 1 NA
06 May 2015 1 2
07 May 2015 1 1
11 May 2015 1 4
11 May 2015 1 0
Run Code Online (Sandbox Code Playgroud) 我有以下两个功能:
def loop(x):
a = np.zeros(10)
for i1 in range(10):
for i2 in range(10):
a[i1] += np.sin(x[i2] - x[i1])
return a
Run Code Online (Sandbox Code Playgroud)
和
def vectorized(x):
b = np.zeros(10)
for i1 in range(10):
b += np.sin(np.roll(x, i1) - x)
return b
Run Code Online (Sandbox Code Playgroud)
但是,当我同时运行时,我发现它们的结果略有不同:
x = np.arange(10)
a, b = loop(x), vectorized(x)
print b - a
Run Code Online (Sandbox Code Playgroud)
我明白了:
[ 2.22044605e-16 0.00000000e+00 0.00000000e+00 6.66133815e-16
-2.22044605e-16 2.22044605e-16 0.00000000e+00 2.22044605e-16
2.22044605e-16 2.22044605e-16]
Run Code Online (Sandbox Code Playgroud)
这是非常小的,但在我的情况下,影响模拟.如果我从函数中删除np.sin,差异就会消失.或者,如果对x使用np.float32,差异也会消失,但这是使用float64的解算器解决的ode的一部分.有没有办法解决这个差异?
我有一个如下数据框:框架的形状是(1510,1399).列表示产品,行表示用户为给定产品分配的值(0或1).我怎样才能计算jaccard_similarity_score?
我创建了一个列出产品与产品的占位符数据框
data_ibs = pd.DataFrame(index=data_g.columns,columns=data_g.columns)
Run Code Online (Sandbox Code Playgroud)
我不知道如何迭代data_ibs来计算相似之处.
for i in range(0,len(data_ibs.columns)) :
# Loop through the columns for each column
for j in range(0,len(data_ibs.columns)) :
.........
Run Code Online (Sandbox Code Playgroud) 假设我有这个NumPy数组:
a = np.array([0, 3, 5, 5, 0, 10, 14, 15, 56, 0, 12, 23, 45, 23, 12, 45,
0, 1, 0, 2, 3, 4, 0, 0 ,0])
Run Code Online (Sandbox Code Playgroud)
我想在0之间打印所有数字并自动将它们添加到新的数字中np.array
(见下文):
a1=[3, 5, 5]
a2=[10, 14, 15, 56]
a3=[12, 23, 45, 23, 12, 45]
a4=[1]
a5=[2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
是否有内置函数来执行此操作?
今天,我使用Python的pandas_datareader抓取股票数据.有趣的是它在几个小时前就已经奏效,但现在我无法从雅虎财务中获取股票数据,但我可以用谷歌.然后我在命令终端中升级了pandas datareader pip install pandas-datareader --upgrade
.然后我像往常一样导入升级后的包from pandas_datareader import data, wb
.
它仍然无法运作,但它适用于获取股票期权.它应该根据本文档为pandas datareader https://pypi.python.org/pypi/pandas-datareader/0.4.0
from pandas_datareader import Options
aapl = Options("AAPL" "yahoo")
aapl = aapl.get_all_data()
Run Code Online (Sandbox Code Playgroud)
使用谷歌,抓取股票数据有效.
import datetime
import pandas as pd
from pandas_datareader import data, wb
start = datetime.datetime(2016, 1, 1)
end = datetime.datetime(2017, 1, 1)
aapl = data.DataReader("AAPL", "google", start, end)
Run Code Online (Sandbox Code Playgroud)
雅虎财经不起作用.
aapl = data.DataReader("AAPL", "yahoo", start, end)
Run Code Online (Sandbox Code Playgroud)
这真烦人!任何人都可以帮助从雅虎获取股票数据?
这是追溯:
aapl = data.DataReader("AAPL", "yahoo", start, end) Traceback (most recent call last): File "", line 1, in …
我有一个如下模型:
class Foo(models.Model):
fruit = models.CharField(max_length=10)
stuff = models.CharField(max_length=10)
color = models.CharField(max_length=10)
owner = models.CharField(max_length=20)
exists = models.BooleanField()
class Meta:
unique_together = (('fruit', 'stuff', 'color'), )
Run Code Online (Sandbox Code Playgroud)
它填充了一些数据:
fruit stuff color owner exists
Apple Table Blue abc True
Pear Book Red xyz False
Pear Phone Green xyz False
Apple Phone Blue abc True
Pear Table Green abc True
Run Code Online (Sandbox Code Playgroud)
我需要将它与一个集合(不是查询集)合并/连接:
[('Apple', 'Table', 'Blue'), ('Pear', 'Phone', 'Green')]
Run Code Online (Sandbox Code Playgroud)
因此,当我使用此元组列表搜索此模型时,基本上应返回行0和2.
目前我的解决方法是读Foo.objects.all()
入一个DataFrame并与元组列表合并并获取要传递给的ID Foo.objects.filter()
.我也尝试迭代列表并调用Foo.object.get()
每个元组,但它非常慢.名单很大.
当我按照当前答案的建议尝试链接Q时,它抛出了一个OperationalError(太多的SQL变量).
我的主要目标如下:
从模型中可以看出,这三个字段共同构成了我的主键.该表包含大约15k个条目.当我从另一个源获取数据时,我需要检查数据是否已经存在于我的表中并相应地创建/更新/删除(新数据可能包含多达15k个条目).有没有一种干净有效的方法来检查这些记录是否已经在我的表中?
注意:元组列表不必是那种形状.我可以修改它,将其转换为另一个数据结构或转置它.
我有一个带有列Gold
和的数据框架Gold.1
.我想找到这两列的差异最大的行.
对于以下DataFrame,这应该返回第6行.
df
Out:
Gold Gold.1
0 2 1
1 1 4
2 6 9
3 4 4
4 4 8
5 5 5
6 5 2 ---> The difference is maximum (3)
7 5 9
8 5 3
9 5 6
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下内容:
df.where(max(df['Gold']-df['Gold.1']))
Run Code Online (Sandbox Code Playgroud)
但是这引发了一个ValueError:
df.where(max(df['Gold']-df['Gold.1'])) Traceback (most recent call last): File "", line 1, in df.where(max(df['Gold']-df['Gold.1'])) File "../python3.5/site-packages/pandas/core/generic.py", line 5195, in where raise_on_error) File "../python3.5/site-packages/pandas/core/generic.py", line 4936, in _where raise ValueError('Array conditional must be same …
我尝试使用holt-winters model
如下所示的预测,但我不断得到与我期望的预测不一致的预测.我还展示了情节的可视化
Train = Airline[:130]
Test = Airline[129:]
from statsmodels.tsa.holtwinters import Holt
y_hat_avg = Test.copy()
fit1 = Holt(np.asarray(Train['Passengers'])).fit()
y_hat_avg['Holt_Winter'] = fit1.predict(start=1,end=15)
plt.figure(figsize=(16,8))
plt.plot(Train.index, Train['Passengers'], label='Train')
plt.plot(Test.index,Test['Passengers'], label='Test')
plt.plot(y_hat_avg.index,y_hat_avg['Holt_Winter'], label='Holt_Winter')
plt.legend(loc='best')
plt.savefig('Holt_Winters.jpg')
Run Code Online (Sandbox Code Playgroud)
我不确定我在这里缺少什么.
预测似乎适用于训练数据的早期部分
我们刚刚从 切换nose
到pytest
并且似乎没有抑制第三方日志记录的选项。在nose
配置中,我们有以下行:
logging-filter=-matplotlib,-chardet.charsetprober,-PIL,-fiona.env,-fiona._env
Run Code Online (Sandbox Code Playgroud)
其中一些日志非常啰嗦,尤其是matplotlib
我们不想看到输出,只想从我们的日志中输出。
但是我找不到等效的设置pytest
。是否可以?我错过了什么吗?谢谢。
python ×10
pandas ×4
numpy ×3
anaconda ×1
django ×1
django-orm ×1
forecasting ×1
holtwinters ×1
logging ×1
matrix ×1
nose ×1
pytest ×1
python-2.7 ×1
scikit-learn ×1
similarity ×1
statsmodels ×1
time-series ×1