在我的功能工程期间,发生了以下错误。我的功能列表有 21 个子列表,每个 8537 个值是 0 或 1。当尝试通过 tensorflow 运行 One Hot Encoding 时,它显示错误Could not find valid device for node
有没有人有快速修复错误的方法?
for feature in featurelist[1:]:
df = tensorflow.convert_to_tensor(feature, dtype=tensorflow.float32)
print(df)
df_enc = tensorflow.one_hot(df, 2, on_value=None, off_value=None, axis=None, dtype=None, name=None)
print(df_enc)
Run Code Online (Sandbox Code Playgroud)
2020-05-29 15:08:41.969878: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fdefbc23d50 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-05-29 15:08:41.969919: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
tf.Tensor([0. 0. 0. ... 0. 0. 0.], shape=(8537,), …Run Code Online (Sandbox Code Playgroud) python keras tensorflow one-hot-encoding feature-engineering
我正在尝试直接从 tensorflow 数据集加载“iris”数据集,但我被卡住了。我习惯于使用 CSV。
import tensorflow as tf
import tensorflow_datasets as tfds
data = tfds.load("iris",split='train[:80%]', as_supervised=True)
data = data.batch(10)
features, labels = data
Run Code Online (Sandbox Code Playgroud)
我不知道我应该如何分离特征 X,y。标签与特征的张量不同,但我不知道如何访问它们以使用它们。我想对标签进行热编码并将它们输入模型,但我被困在这里。
tensorflow 文档很少提供有关如何执行此操作的信息。任何帮助深表感谢
python tensorflow tensorflow-datasets iris-dataset tensorflow2.0
大家好,我正在使用 Tensorflow 开发情绪分析,使用一些基于亚马逊电子产品的评论。在代码中,我遇到了一个错误。我使用 tensorflow 数据集来检索一些文本,但无法检索。这是代码的一部分,包含以下错误:
tokenizer = tfds.features.text.Tokenizer()
vocabulary_set = set()
for _, reviews in train_dataset.enumerate():
review_text = reviews['data']
reviews_tokens = tokenizer.tokenize(review_text.get('review_body').numpy())
vocabulary_set.update(reviews_tokens)
vocab_size = len(vocabulary_set)
vocab_size
Run Code Online (Sandbox Code Playgroud)
我从这里得到的错误是属性错误
AttributeError Traceback (most recent call last)
<ipython-input-17-1c32dce13853> in <module>()
----> 1 tokenizer = tfds.features.text.Tokenizer()
AttributeError: module 'tensorflow_datasets.core.features' has no attribute 'text'
Run Code Online (Sandbox Code Playgroud)
请问我该如何解决这个错误?谢谢
我有一个像这样的 DataFrame,我试图用 Sankey 图来描述它:
import pandas as pd
pd.DataFrame({
'animal': ['dog', 'cat', 'cat', 'dog', 'cat'],
'sex': ['male', 'female', 'female', 'male', 'male'],
'status': ['wild', 'domesticated', 'domesticated', 'wild', 'domesticated'],
'count': [8, 10, 11, 14, 6]
})
Run Code Online (Sandbox Code Playgroud)
animal sex status count
0 dog male wild 8
1 cat female domesticated 10
2 cat female domesticated 11
3 dog male wild 14
4 cat male domesticated 6
Run Code Online (Sandbox Code Playgroud)
我试图按照文档中的步骤操作,但无法使其工作 - 我无法理解哪些分支在哪里。这是示例代码:
import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15, …Run Code Online (Sandbox Code Playgroud) 在pytorch中,如果我定义一个单元素张量如下:
>>> import torch
>>> target1 = torch.tensor([5])
Run Code Online (Sandbox Code Playgroud)
我可以像这样提取一个元素的值:
>>> target1.item()
5
Run Code Online (Sandbox Code Playgroud)
我想知道的是,当我的张量定义为:
target2 = torch.tensor([[5], [5], [5], [5]])
Run Code Online (Sandbox Code Playgroud)
是否有某种方法(类似于或不类似于上面的.item())将其所有条目提取到一个列表中,例如:
>>> target2.(something)
[5, 5, 5, 5]
Run Code Online (Sandbox Code Playgroud)
我似乎无法在支持此类操作的文档中找到任何功能。
我很好奇如何sklearn处理连续变量sklearn.tree.DecisionTreeClassifier?我尝试使用一些连续变量而不使用 进行预处理DecisionTreeClassifier,但它获得了可接受的精度。
下面是一种将连续变量转换为分类变量的方法,但它不能获得相同的精度。
def preprocess(data, min_d, max_d, bin_size=3):
norm_data = np.clip((data - min_d) / (max_d - min_d), 0, 1)
categorical_data = np.floor(bin_size*norm_data).astype(int)
return categorical_data
X = preprocess(X, X.min(), X.max(), 3)
Run Code Online (Sandbox Code Playgroud) python machine-learning decision-tree python-3.x scikit-learn
此时间戳用于记录错误。
import os.path
import datetime
timestamp = datetime.datetime.now().strftime('%Y-%m-%d')
Run Code Online (Sandbox Code Playgroud)
试图搜索,但无法找到与此相关的任何内容。我想知道你什么时候使用双日期时间方法?
我将特征分离X,y然后在使用 k 折交叉验证将其拆分后预处理我的火车测试数据。之后,我将训练数据拟合到我的随机森林回归模型并计算置信度分数。拆分后为什么要预处理?因为人们告诉我这样做更正确,并且为了我的模型性能,我一直保持这个原则。
这是我第一次使用 KFold 交叉验证,因为我的模型分数过高,我想我可以通过交叉验证来修复它。我仍然对如何使用它感到困惑,我已经阅读了文档和一些文章,但我并没有真正理解我如何真正将它暗示给我的模型,但我还是尝试了,我的模型仍然过度拟合。使用训练测试拆分或交叉验证导致我的模型分数仍然是 0.999,我不知道我的错误是什么,因为我是使用这种方法的新手,但我想也许我做错了,所以它不能修复过度拟合。请告诉我我的代码有什么问题以及如何解决这个问题
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestRegressor
import scipy.stats as ss
avo_sales = pd.read_csv('avocados.csv')
avo_sales.rename(columns = {'4046':'small PLU sold',
'4225':'large PLU sold',
'4770':'xlarge PLU sold'},
inplace= True)
avo_sales.columns = avo_sales.columns.str.replace(' ','')
x = np.array(avo_sales.drop(['TotalBags','Unnamed:0','year','region','Date'],1))
y = np.array(avo_sales.TotalBags)
# X_train, X_test, …Run Code Online (Sandbox Code Playgroud) python machine-learning python-3.x scikit-learn cross-validation
我想为列表中的每个单词添加一个前缀。像这样:
mywords = ['word1', 'word2', 'word3']
['prefix'.join(word) for word in mywords]
Run Code Online (Sandbox Code Playgroud)
如果我运行这个,单词中的每个字母都有前缀。有没有办法确保我得到
['prefixword1', 'prefixword2' etc.]
Run Code Online (Sandbox Code Playgroud) 我制作了一个 TensorFlow CsvDataset,我正在尝试对数据进行标记:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from tensorflow import keras
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
import os
os.chdir('/home/nicolas/Documents/Datasets')
fname = 'rotten_tomatoes_reviews.csv'
def preprocess(target, inputs):
tok = Tokenizer(num_words=5_000, lower=True)
tok.fit_on_texts(inputs)
vectors = tok.texts_to_sequences(inputs)
return vectors, target
dataset = tf.data.experimental.CsvDataset(filenames=fname,
record_defaults=[tf.int32, tf.string],
header=True).map(preprocess)
Run Code Online (Sandbox Code Playgroud)
运行这个,给出以下错误:
ValueError: len 需要一个非标量张量,得到一个形状 Tensor("Shape:0", shape=(0,), dtype=int32)
我尝试过的:几乎任何可能性领域。请注意,如果我删除预处理步骤,一切都会运行。
数据是什么样的:
(<tf.Tensor: shape=(), dtype=int32, numpy=1>,
<tf.Tensor: shape=(), dtype=string, numpy=b" Some movie critic review...">)
Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×4
tensorflow ×4
keras ×3
scikit-learn ×2
datetime ×1
iris-dataset ×1
list ×1
nlp ×1
nltokenizer ×1
pandas ×1
plotly ×1
pytorch ×1
string ×1
tensor ×1