我一直在修补NLTK,目的是从一些新闻文章中提取实体,但我不断收到错误,ValueError:块结构必须包含标记的标记或树.
谁能帮我?
import lxml.html
import nltk, re, pprint
def ie_preprocess(document):
"""This function takes raw text and chops and then connects the process to break
it down into sentences, then words and then complete part-of-speech tagging"""
sentences = nltk.sent_tokenize(document)
sentences = [nltk.word_tokenize(sent) for sent in sentences]
sentences = [nltk.pos_tag(sent) for sent in sentences]
return sentences
#import story
base_url = "http://www.thisisstaffordshire.co.uk/Yobs-pelt-999-crews-bottles-fireworks-Shelton/story-17256383-detail/story.html"
page = lxml.html.parse(base_url)
story = page.xpath('//*[@id="story"]/div[2]/div[1]')
raw_text = story[0].text_content()
#tokenize
output = ie_preprocess(raw_text)
print output
#chunk
grammar = r'''
NP:
{<DT><NN.*><.*>*<NN.*>} …Run Code Online (Sandbox Code Playgroud) 我有一个带有犯罪时间序列数据的数据框,其中有一个攻击方面(看起来像下面的格式).我想对数据框进行分组绘图,以便随着时间的推移探索犯罪趋势.
Offence Rolling year total number of offences Month
0 Criminal damage and arson 1001 2003-03-31
1 Drug offences 66 2003-03-31
2 All other theft offences 617 2003-03-31
3 Bicycle theft 92 2003-03-31
4 Domestic burglary 282 2003-03-31
Run Code Online (Sandbox Code Playgroud)
我有一些代码可以完成这项工作,但它有点笨拙并且它丢失了Pandas在单个绘图上提供的时间序列格式.(我已经包含了一张图片来说明).任何人都可以建议我可以使用这种情节的成语吗?
我会转向Seaborn,但我无法弄清楚如何将xlabel格式化为时间序列.
[![subs = \[\]
for idx, (i, g) in enumerate(df.groupby("Offence")):
subs.append({"data": g.set_index("Month").resample("QS-APR", how="sum" ).ix\["2010":\],
"title":i})
ax = plt.figure(figsize=(25,15))
for i,g in enumerate(subs):
plt.subplot(5, 5, i)
plt.plot(g\['data'\])
plt.title(g\['title'\])
plt.xlabel("Time")
plt.ylabel("No. of crimes")
plt.tight_layout()][1]][1]
Run Code Online (Sandbox Code Playgroud) 这可能是一个微不足道的查询,但我无法解决.
从本质上讲,我希望能够从下面的数据帧中滤除嘈杂的推文
<class 'pandas.core.frame.DataFrame'>
Int64Index: 140381 entries, 0 to 140380
Data columns:
text 140381 non-null values
created_at 140381 non-null values
id 140381 non-null values
from_user 140381 non-null values
geo 5493 non-null values
dtypes: float64(1), object(4)
Run Code Online (Sandbox Code Playgroud)
我可以根据不需要的关键字创建数据框:
junk = df[df.text.str.contains("Swans")]
Run Code Online (Sandbox Code Playgroud)
但是用这个来看看剩下的最好的方法是什么?
我只是想在 dplyr 中裁剪数据框顶部和底部的行,但无法解决。
在 Pandas 中,我会使用 iloc 之类的东西来裁剪行,但无法弄清楚如何使用 dplyr:
这是我的建议,但它不会在 R Studio 中返回结果:
df %>%
filter(!is.na(column)) %>%
filter(column, between(row_number(), 1, (length(df) - 3))
Run Code Online (Sandbox Code Playgroud) 我正在尝试在GGPlot的水平条形图中针对美国各州绘制一些值。
代码是:
g <- df %>%
select(Sample.Measurement, State.Name) %>%
group_by(State.Name) %>%
count() %>%
arrange(n) %>%
ggplot(., mapping = aes(x=reorder(State.Name, n), y =n))+
geom_bar(stat = "identity", width=.8, position = position_dodge(width = .25))+
labs(
title = "",
y= "Frequency"
)+
coord_flip()
g+theme(axis.text=element_text(size=20),
axis.title=element_text(size=30,face="bold"))
Run Code Online (Sandbox Code Playgroud)
主题元素是为了更改文本大小。
我真正想做的是扩展图表的深度,以便各个州有更多的空间。我怎么做?
我正在尝试使用手动策划的图像数据集和相关标签来训练一个简单的神经网络。
我创建了一个 numpy 来创建名为 facey_label 的标签。
我已经使用 matplotlib 的 imread 函数将 811 个图像中的每一个都转换为一个形状为 (255, 255, 3) 的数组,然后计划使用 np.array 函数创建一个形状为 (811, 255, 255, 3) 的张量 img_array )
model = keras.Sequential([
keras.layers.Flatten(input_shape=(811, 255, 255, 3)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(img_array, facey_label, epochs=5)
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
ValueError: Error when checking input: expected flatten_1_input to have 5 dimensions, but got array with shape (811, 250, 250, 3)
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我使用 Python 中的 Altair 库创建了一些数据的可视化。当前悬停信息框显示中位数。如何更改聚合以显示平均值?
我正在尝试使用 NetBeans 学习 PHP,尽管我遇到了解释器的问题并且我不知道如何解决它。
它与符号<<<_END有关。从我正在学习的内容来看,它应该将所有内容包装到一个变量中,直到以 _END 结束
但是,如果我插入以下示例:
<?php
echo <<<_END
<html><head><title>PHP form upload</title></head><body><form method='post' action='upload.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='10' />
<input type='submit' value='Upload'/>
</form>
_END
if ($_FILES)
{
$name = $_FILES ['filename']['name'];
move_uploaded_file($_FILES ['filename'][tmp_none], $name);
echo "Uploaded image '$name' <br/> <img src='$name'/>";
}
echo "</body></html>";
?>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息
解析错误:语法错误,第 13 行 script.php 中的意外 T_SL,其中第 13 行是表示“echo <<<_END”的代码。
任何人都可以帮助我吗?
我有以下格式的数据框。我想替换的日期列中的年份有误
Date Year
0 2013-04-13 2019
1 2013-04-13 2019
Run Code Online (Sandbox Code Playgroud)
将Year映射到datetime列的最佳方法是什么?
我正在尝试遍历 Pandas 数据框中的一些数据并绘制成不同的子图,但不太确定我做错了什么。
这是代码:
df2 = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
fig, axes = plt.subplots(nrows=4, ncols=1, sharex=True)
for i, col in enumerate(df2.columns):
print(col)
axes[i] = df2[col].plot(kind="box")
Run Code Online (Sandbox Code Playgroud)
我怎样才能填满其他子图?
我正在尝试用Java编译一个简单的类,但无法解决错误的含义.我是Java的新手.
错误是:
Book.java:1: error: '{' expected
public class Book(String bookName)
^
Run Code Online (Sandbox Code Playgroud)
码
public class Book(String bookName)
{
private String name;
private String price;
public Book()
{
name = bookName;
Random r = new Random();
int lowPrice = 10;
int highPrice = 100;
price = r.nextInt(highPrice - lowPrice) + lowPrice;
}
}
Run Code Online (Sandbox Code Playgroud)