强文本我在安装包时不断遇到问题tidyverse,这使我无法执行许多文本处理任务。这个问题与 2017 年以来许多以前的线程中提到的问题相同,当我输入library(tidyverse)或尝试打开其他相关包时,他们总是说它需要 0.2.1 版本的vctrs. 我尝试过使用remote下载或下载其他临时版本但无济于事。有人可以帮我解决这个问题吗?
我正在使用最新版本的 R(版本 3.6.1),在 Rstudio 1.2.5033 上运行。我还更新了我的vctrs包。
install.packages("tidyverse")\ndevtools::install_github("tidyverse/tidyverse")\nlibrary(tidyverse)\nError: package or namespace load failed for \xe2\x80\x98tidyverse\xe2\x80\x99 in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):\n namespace \xe2\x80\x98vctrs\xe2\x80\x99 0.2.0 is already loaded, but >= 0.2.1 is required\nIn addition: Warning message:\npackage \xe2\x80\x98tidyverse\xe2\x80\x99 was built under R version 3.6.2 \n\n\nlibrary(devtools)\ndevtools::install_github("tidyverse/tidyverse")\nlibrary(tidyverse)\nError: package or namespace load failed for \xe2\x80\x98tidyverse\xe2\x80\x99 in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):\n namespace \xe2\x80\x98vctrs\xe2\x80\x99 0.2.0 is …Run Code Online (Sandbox Code Playgroud) 我在部分文本数据上使用了来自sklearn的TfidfVectorizer(),以了解每个功能(词)的术语频率感。我当前的代码如下
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(analyzer='word', stop_words = 'english')
# fit_transform on training data
X_traintfidf = tfidf.fit_transform(X_train)
Run Code Online (Sandbox Code Playgroud)
如果我想将“ X_traintfidf”中每个术语的tf-idf值从最低到最高排序(反之亦然),例如top10,并将这些排序的tf-idf值排名分为两个Series对象,我应该如何进行从我的代码的最后一行开始?
谢谢。
我在读类似的主题,但不知道该怎么做。也许有人可以将该主题中显示的提示与此处的问题联系起来。
我正在尝试将我手动创建的列表对象保存为list我可以快速引用的格式。\n保存的对象应该能够作为listnotdf或其他任何内容加载回来。
下面是我的列表对象的一部分:
\n\ncitations <- list(\nas.integer(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 111)),\nas.integer(c(2, 3, 4, 5, 6, 7, 8, 9, 10, 111)),\nas.integer(c(1, 3, 4, 5, 6, 7, 8, 9, 10, 111)),\nas.integer(111),\nas.integer(111),\nas.integer(111),\nas.integer(111),\nas.integer(111),\nas.integer(111),\nas.integer(111),\nas.integer(111),\nas.integer(6),\ninteger(0),\nas.integer(c(14, 15, 16, 17, 18, 19, 20, 21, 22, 23))\n)\nRun Code Online (Sandbox Code Playgroud)\n\n我已经引用了这个线程和nabble上的一个更早的线程,他们建议简单地使用saveand load,但我只是无法弄清楚它是如何工作的,每当我在对象save()上应用该函数list并为其指定一个 R 对象时,我总是收到错误消息。
save(citations, "/~citations.Rdata")\nError in save(citations, "~/citations.Rdata") : object \xe2\x80\x98~/citations.Rdata\xe2\x80\x99 not found\nRun Code Online (Sandbox Code Playgroud)\n\n只是不知道是否需要其他函数调用。
\n我正在使用 kaggle 的 pokemon 数据通过 练习 KNN 插补preProcess(),但是当我这样做时,我在这predict()一步之后遇到了以下消息。我想知道我是否使用了不正确的数据格式,或者某些列是否具有不适当的“类”。下面是我的代码。
library(dplyr)
library(ggplot2)
library(tidyr)
library(reshape2)
library(caret)
library(skimr)
library(psych)
library(e1071)
library(data.table)
pokemon <- read.csv("https://www.dropbox.com/s/znbta9u9tub2ox9/pokemon.csv?dl=1")
pokemon = tbl_df(pokemon)
# select relevant features
df <- select(pokemon, hp, weight_kg, height_m, sp_attack, sp_defense, capture_rate)
pre_process_missing_data <- preProcess(df, method="knnImpute")
classify_legendary <- predict(pre_process_missing_data, newdata = df)Run Code Online (Sandbox Code Playgroud)
我收到了这个错误信息
Error: Must subset rows with a valid subscript vector.
x Subscript `nn$nn.idx` must be a simple vector, not a matrix.
Run `rlang::last_error()` to see where the error occurred.
Run Code Online (Sandbox Code Playgroud) 我想将四个时间序列的均值绘制到具有置信区间的Matplotlib条形图中。我也想给它们涂上不同的颜色,以生成这样的条形图
所以我写了下面的代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(-10, 200, 100),
np.random.normal(42, 150, 100),
np.random.normal(0, 120, 100),
np.random.normal(-5, 57, 100)],
index=[2012, 2013, 2014, 2015])
years = ('2012', '2013', '2014', '2015')
y_pos = np.arange(len(years))
df1_mean = df.iloc[0].mean()
df1_std = df.iloc[0].std()
df2_mean = df.iloc[1].mean()
df2_std = df.iloc[1].std()
df3_mean = df.iloc[2].mean()
df3_std = df.iloc[2].std()
df4_mean = df.iloc[3].mean()
df4_std = df.iloc[3].std()
value = (df1_mean, df2_mean, df3_mean, df4_mean)
Std = (df1_std, df2_std, df3_std, df4_std)
plt.bar(y_pos, value, …Run Code Online (Sandbox Code Playgroud)我正在研究一个属于两种类型的小型示例节点集{'human', 'machine'},我想以字典形式在networkx图的每个节点之外标记节点属性,例如下图中的节点c,e,j中所示的那些。(我使用MS Word在图形上添加了字典类型的属性。):
基本图是使用以下代码生成的:
import networkx as nx
G = nx.Graph()
G.add_nodes_from(['a', 'b', 'c', 'd', 'e', 'f', 'g'], type = 'machine')
G.add_nodes_from(['h', 'i', 'j'], type = 'human')
G.add_edges_from([('a', 'c'), ('a', 'b'), ('a', 'd'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'g'), ('c', 'f'), ('c', 'd'), ('d', 'f'), ('d', 'e'), ('d', 'g'), ('e', 'g'), ('f', 'g'), ('f', 'h'), ('g', 'h'), ('h', 'i'), ('i', 'j')])
def plot_graph(G, weight_name=None):
import matplotlib.pyplot as plt
plt.figure()
pos = nx.spring_layout(G)
edges = …Run Code Online (Sandbox Code Playgroud) 我有pandas.DataFrame各种日期时间格式的日期列,并存储为列表对象,如下所示:
date
1 [May 23rd, 2011]
2 [January 1st, 2010]
...
99 [Apr. 15, 2008]
100 [07-11-2013]
...
256 [9/01/1995]
257 [04/15/2000]
258 [11/22/68]
...
360 [12/1997]
361 [08/2002]
...
463 [2014]
464 [2016]
Run Code Online (Sandbox Code Playgroud)
为了方便起见,我想将它们全部转换为MM/DD/YYYY格式。似乎无法使用regex replace()函数来执行此操作,因为无法对列表对象执行此操作。另外,对每个单元使用strptime()会很费时。
将它们全部转换为所需MM/DD/YYYY格式的更简单方法是什么?我发现很难对数据框内的列表对象执行此操作。
注意:对于格式为[YYYY](例如[2014]和[2016])的单元格值,我将假定它们是该年的第一天(即1968年1月1日),对于诸如[08/2002](或[8/2002])的单元格值,我将假定它们为第一天该年当月(即2002年8月1日)的日期。
在提出这个问题之前,我不得不说,我已经在此板上彻底阅读了15个以上的相似主题,每个主题都有不同的建议,但是所有这些都无法使我正确。
好的,所以我使用CountVectorizer及其“ fit_transform”函数将语料库的文本数据(最初为csv格式)分为训练集和测试集,以适应语料库的词汇量并从文本中提取字数统计功能。然后,我应用MultinomialNB()从训练集中学习并预测测试集。这是我的代码(简体):
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.cross_validation import train_test_split
from sklearn.naive_bayes import MultinomialNB
# loading data
# data contains two columns ('text', 'target')
spam = pd.read_csv('spam.csv')
spam['target'] = np.where(spam_data['target']=='spam',1,0)
# split data
X_train, X_test, y_train, y_test = train_test_split(spam_data['text'], spam_data['target'], random_state=0)
# fit vocabulary and extract word count features
cv = CountVectorizer()
X_traincv = cv.fit_transform(X_train)
X_testcv = cv.fit_transform(X_test)
# learn and predict using MultinomialNB
clfNB = MultinomialNB(alpha=0.1)
clfNB.fit(X_traincv, y_train)
# so far so good, but when I predict on …Run Code Online (Sandbox Code Playgroud) 我正在从电子邮件文本列表(以 csv 格式存储)中对垃圾邮件进行分类,但在此之前,我想从输出中获取一些简单的计数统计信息。我使用 sklearn 的 CountVectorizer 作为第一步,并通过以下代码实现
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
#import data from csv
spam = pd.read_csv('spam.csv')
spam['Spam'] = np.where(spam['Spam']=='spam',1,0)
#split data
X_train, X_test, y_train, y_test = train_test_split(spam_data['text'], spam_data['target'], random_state=0)
#convert 'features' to numeric and then to matrix or list
cv = CountVectorizer()
x_traincv = cv.fit_transform(X_train)
a = x_traincv.toarray()
a_list = cv.inverse_transform(a)
Run Code Online (Sandbox Code Playgroud)
输出以矩阵(名为“a”)或数组列表(名为“a_list”)格式存储,如下所示
[array(['do', 'I', 'off', 'text', 'where', 'you'],
dtype='<U32'),
array(['ages', 'will', 'did', 'driving', 'have', 'hello', 'hi', …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码从包含nonetype的列表对象中获取最大值:
import numpy as np
LIST = [1,2,3,4,5,None]
np.nanmax(LIST)
Run Code Online (Sandbox Code Playgroud)
但我收到此错误消息
'>=' not supported between instances of 'int' and 'NoneType'
Run Code Online (Sandbox Code Playgroud)
显然np.nanmax()不适用None.从包含None值的列表对象中获取最大值的替代方法是什么?