我想创建一个词云。当我的字符串是英文时,一切正常:
from wordcloud import WordCloud
from matplotlib import pyplot as plt
text="""Softrock 40 - close to the 6 MHz that the P6D requires (6.062 according) - https://groups.yahoo.com/neo/groups/softrock40/conversations/messages
I want the USB model that has a controllable (not fixed) central frequency."""
wordcloud = WordCloud().generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是当我在希伯来语中做同样的事情时,它没有检测到字体,我只得到空的矩形:
text="""?????? ?? ???? ????? ?????, ????? ???? ?????? ??????? ?? ??????? ???? ???? ????? ???? ????? ???????? ????? ????? ???? ????"""
wordcloud = WordCloud().generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有一个数据框,其中left
列是对象最左侧的位置,而right
列是最右侧的位置。如果对象重叠,或者它们重叠(递归地)重叠的对象,我需要对它们进行分组。因此,例如,如果这是我的数据框:
left right
0 0 4
1 5 8
2 10 13
3 3 7
4 12 19
5 18 23
6 31 35
Run Code Online (Sandbox Code Playgroud)
所以线0
和3
重叠 - 因此它们应该在同一组中,并且线1
也是重叠线3
- 因此它加入了该组。
所以,对于这个例子,输出应该是这样的:
left right group
0 0 4 0
1 5 8 0
2 10 13 1
3 3 7 0
4 12 19 1
5 18 23 1
6 31 35 2
Run Code Online (Sandbox Code Playgroud)
我想到了各种方向,但没有弄清楚(没有丑陋的for
)。任何帮助将不胜感激!
我正在尝试使用cvxpy
优化包解决数独。我对优化和 cvxpy 都很陌生。
约束条件是:
所以,这是我的代码:
from cvxpy import *
import numpy as np
x = Variable((9, 9), integer=True)
obj = Minimize(sum(x)) #whatever, if the constrains are fulfilled it will be fine
const = [x >= 1, #all values should be >= 1
x <= 9, #all values should be <= 9
sum(x, axis=0) == 45, # sum of all rows should …
Run Code Online (Sandbox Code Playgroud) 我是Python和Pycharm的新手,
我试图超越列表理解的界限,
但是pycharm并没有将我移至下一行,而是在1次迭代中增加了循环。
有什么想法如何在不按F8 3000次的情况下移至下一行?
谢谢!
我有一个numpy vector
,一个numpy array
.
我需要从矩阵中的每一行获取小于(或等于)向量中相应行的第一个N(比如3)值.
所以如果这是我的载体:
7,
9,
22,
38,
6,
15
Run Code Online (Sandbox Code Playgroud)
这是我的矩阵:
[[ 20., 9., 7., 5., None, None],
[ 33., 21., 18., 9., 8., 7.],
[ 31., 21., 13., 12., 4., 0.],
[ 36., 18., 11., 7., 7., 2.],
[ 20., 14., 10., 6., 6., 3.],
[ 14., 14., 13., 11., 5., 5.]]
Run Code Online (Sandbox Code Playgroud)
输出应该是:
[[7,5,None],
[9,8,7],
[21,13,12],
[36,18,11],
[6,6,3],
14,14,13]]
Run Code Online (Sandbox Code Playgroud)
是否有任何有效的方法来做掩码或其他东西,没有丑陋的for
循环?
任何帮助将不胜感激!
我正在寻找一种方法来获取英语维基百科上的所有电影,以及它们的创建日期。对我来说,电影是附加了 IMDB ID 的页面。所以,这是我到目前为止的查询:
SELECT DISTINCT ?item_label ?imdb_id (year(?dateCreation) as ?AnneeCreation) WHERE {
?item wdt:P345 $imdb_id.
filter STRSTARTS(?imdb_id,"tt")
OPTIONAL{
?item wdt:P571 ?dateCreation.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".}
?article schema:about ?item ; schema:isPartOf <https://en.wikipedia.org/> ; schema:name ?item_label
}
Run Code Online (Sandbox Code Playgroud)
问题是大多数页面没有 P571 属性,所以我想知道是否有更好的方法来获取创建日期?也许通过修订历史或其他什么,我找不到这样的选项。
任何帮助将不胜感激!
假设这是我的 df:
Name1 Name2 date
1 John Jay 2015-01-01 06:01:00
2 Sara Debra 2015-01-01 06:05:00
3 Ben Beth 2015-01-01 06:09:00
Run Code Online (Sandbox Code Playgroud)
我想填写 df ,其中每行应重复并增加 1 分钟,直到到达下一行,因此输出应为:
Name1 Name2 date
1 John Jay 2015-01-01 06:01:00
1 John Jay 2015-01-01 06:02:00
1 John Jay 2015-01-01 06:03:00
1 John Jay 2015-01-01 06:04:00
2 Sara Debra 2015-01-01 06:05:00
2 Sara Debra 2015-01-01 06:06:00
3 Ben Beth 2015-01-01 06:07:00
Run Code Online (Sandbox Code Playgroud)
我研究了 date_range,但没有找到一种没有丑陋且低效的 for 循环的正确方法。
任何帮助,将不胜感激!
我对 CVXPY 真的很陌生。我正在尝试解决8 个皇后问题,即将 8 个国际象棋皇后放在 8 x 8 棋盘上,这样两个皇后就不会互相威胁。据我了解,约束应该是:
此外,目标函数应该是:最大化矩阵的 2-范数(这样我们只能得到 1 和 0,因为我们也可以得到 s 的和为 1 float
,但是 1 与 0 的范数大于浮点数的范数) 0 到 1 之间且总和为 1(例如:0.8^2+0.2^2 < 1^2+0^2)。
CVXPY 中可以解决此类问题吗?我对如何在 CVXPY 中形成约束和目标函数一无所知,但这是我最初的初步尝试(我立即收到“DCP 错误”,所以我没有理由继续,但仍然如此):
from cvxpy import *
x=Variable(shape=(9,9), name='board')
obj = Maximize(norm(x))
const = [sum(x, axis=1)==1]
prob=Problem(objective=obj,constraints=const)
prob.solve()
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!!!
python optimization mathematical-optimization n-queens cvxpy
我有一个如下所示的数据框:
index value
0 1
1 1
2 2
3 3
4 2
5 1
6 1
Run Code Online (Sandbox Code Playgroud)
我想要的是每个值返回前一个较小值的索引,此外,还有前一个"1"值的索引.如果值为1,我不需要它们(两个值都可以是-1
或者某个值).
所以我要追求的是:
index value previous_smaller_index previous_1_index
0 1 -1 -1
1 1 -1 -1
2 2 1 1
3 3 2 1
4 2 1 1
5 1 -1 -1
6 1 -1 -1
Run Code Online (Sandbox Code Playgroud)
我尝试使用滚动,累积功能等但我无法弄明白.任何帮助,将不胜感激!
编辑:SpghttCd已经为"之前的1"问题提供了一个很好的解决方案.我正在为"前一个小问题"找一个漂亮的熊猫一个班轮.(尽管如此,对于这两个问题,欢迎使用更好,更有效的解决方案)
很多时候,当我试图回答Stackoverflow上的问题时,问题包含一个表,我必须将其转换为pandas数据帧才能处理.例如,在这个问题中:
http://stackoverflow.com/questions/43172116/pandas-count-some-value-in-all-columns
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否有更快的方法将其转换为数据帧而不是写入:
df=pd.DataFrame({'graph':[1,2,3,4,5,6],
0:['blue','blue','red','red','blue','blue'],
1:['blue','blue','red','blue','red','blue'],
2:['blue','blue','blue','red','blue','blue'],
3:['blue','blue','blue','red','red','blue'],
4:['blue','blue','red','blue','red','blue']})
Run Code Online (Sandbox Code Playgroud)
鉴于我可以复制文字:
graph 0 1 2 3 4
1 blue blue blue blue blue
2 blue blue blue blue blue
3 blue red blue blue red
4 red blue red red blue
5 red red blue red red
6 blue blue blue blue blue
Run Code Online (Sandbox Code Playgroud) 我想知道是否有相当于有熊猫cumsum()
或者cummax()
等为中位数:如cummedian()
。
因此,如果我有,例如这个数据框:
a
1 5
2 7
3 6
4 4
Run Code Online (Sandbox Code Playgroud)
我想要的是这样的:
df['a'].cummedian()
Run Code Online (Sandbox Code Playgroud)
应该输出:
5
6
6
5.5
Run Code Online (Sandbox Code Playgroud) 我有一个 DataFrame,其中包含价格数据和股票分割数据。我想将所有价格放在同一页面上,例如,如果我们的股票分割为 0.11111 (1/9),那么从现在开始所有股票价格都将乘以 9。
例如,如果这是我的初始数据框:
df= Price Stock_Splits
0 100 0
1 99 0
2 10 0.1111111
3 8 0
4 8.5 0
5 4 0.5
Run Code Online (Sandbox Code Playgroud)
“价格”栏将变为:
df= Price Stock_Splits
0 100 0
1 99 0
2 90 0.1111111
3 72 0
4 76.5 0
5 72 0.5
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!