我正在阅读fully_connected_feed.py
包含以下代码的tensorflow教程文件.我不明白那些意思.我们为什么需要那个?它似乎只是定义了一些全局变量.为什么不直接定义它们?任何帮助表示赞赏.谢谢
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('max_steps', 2000, 'Number of steps to run trainer.')
flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 100, 'Batch size. '
'Must divide evenly into the dataset sizes.')
flags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.')
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '
'for unit testing.')
Run Code Online (Sandbox Code Playgroud) 我有一个pandas.DataFrame
作为图像的列。该列的每一行都是作为 2d 的图像numpy.array
。我将它保存DataFrame
到一个csv
文件中pandas.DataFrame.to_csv()
。但是,当我打开csv
文件时,该列变为字符串而不是numpy.array
.
如何读取csv
文件并保留numpy.array
?
我正在尝试使用 ggplot:stat_smooth 创建具有二次多项式回归线的散点图。以下是代码:
df.car_spec_data <- read.csv(url("http://www.sharpsightlabs.com/
wp- content/uploads/2015/01/auto-snout_car-specifications_COMBINED.txt"))
df.car_spec_data$year <- as.character(df.car_spec_data$year)
df.car_spec_data %>% group_by(year) %>%
summarise(maxspeed=max(top_speed_mph,
na.rm=T)) %>% ggplot(aes(x=year, y=maxspeed,
group=1))+geom_point(color='red', alpha=0.3,
size=3)+stat_smooth(method='lm', y~poly(x,2))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
Error: Mapping must be created by `aes()` or `aes_()`
Run Code Online (Sandbox Code Playgroud)
多谢。
我想检查变量是否为''
“NULL”。我做了如下:
x =NULL #or ''
if(is.null(x) || x=='') {
print('nothing')
} else {
print(x)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是检查这种情况的最佳方法是什么?我觉得有一些更好的方法可以做到这一点......
我正在尝试使用purrr :: map制作lm对象的列表。以mtcars为例:
vars <- c('hp', 'wt', 'disp')
map(vars, ~lm(mpg~.x, data=mtcars))
Run Code Online (Sandbox Code Playgroud)
错误:model.frame.default(formula = mpg〜.x,data = mtcars,drop.unused.levels = TRUE)中的错误:可变长度不同(为'.x'找到)
我也尝试过:
map(vars, function(x) {x=sym(x); lm(mpg~!!x, data=mtcars)})
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
Error in !x : invalid argument type
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么?提前致谢。
我需要绘制每个特征如何影响LightGBM
二元分类器中每个样本的预测概率。所以我需要以概率的形式输出Shap值,而不是正常的Shap值。它似乎没有任何概率输出选项。
下面的示例代码是我用来生成 Shap 值的数据帧并force_plot
为第一个数据样本执行的代码。有谁知道我应该如何修改代码来改变输出?我是 Shap 值和 Shap 包的新手。预先非常感谢。
import pandas as pd
import numpy as np
import shap
import lightgbm as lgbm
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = lgbm.LGBMClassifier()
model.fit(X_train, y_train)
explainer = shap.TreeExplainer(model)
shap_values = explainer(X_train)
# force plot of first row for class 1
class_idx = 1
row_idx = 0
expected_value = …
Run Code Online (Sandbox Code Playgroud) 我在 facet_wrap 之后通过另一个变量将相关系数放在散点图上时遇到问题。下面是我使用 mtcars 数据集制作的示例,用于说明目的。当我绘制出来时,两个图都有相同的相关数。似乎没有为每个方面计算相关系数。我想不出实现这一目标的方法。如果有人可以帮助解决这个问题,真的很感激......
library(ggplot2)
library(dplyr)
corr_eqn <- function(x,y, method='pearson', digits = 2) {
corr_coef <- round(cor.test(x, y, method=method)$estimate, digits = digits)
corr_pval <- tryCatch(format(cor.test(x,y, method=method)$p.value,
scientific=TRUE),
error=function(e) NA)
paste(method, 'r = ', corr_coef, ',', 'pval =', corr_pval)
}
sca.plot <- function (cor.coef=TRUE) {
df<- mtcars %>% filter(vs==1)
p<- df %>%
ggplot(aes(x=hp, y=mpg))+
geom_point()+
geom_smooth()+
facet_wrap(~cyl, ncol=3)
if (cor.coef) {
p<- p+geom_text(x=0.9*max(df$hp, na.rm=TRUE),
y=0.9*max(df$mpg, na.rm=TRUE),
label = corr_eqn(df[['hp']],df[['mpg']],
method='pearson'))
}
return (p)
}
sca.plot(cor.coef=TRUE)
Run Code Online (Sandbox Code Playgroud) 我有一个关于数据预处理的问题需要澄清。据我了解,当我们通过交叉验证调整超参数并估计模型性能时,我们需要在交叉验证中进行,而不是预处理整个数据集。换句话说,在交叉验证中,我们对训练折叠进行预处理,然后使用相同的预处理参数来处理测试折叠并进行预测。
在下面的示例代码中,当我在 caret::train 中指定 preProcess 时,它会自动执行此操作吗?如果有人能澄清我这一点,我真的很感激。
从一些在线资源来看,有些人预处理整个数据集(训练集),然后使用预处理数据通过交叉验证来调整超参数,这似乎不对......
library(caret)
library(mlbench)
data(PimaIndiansDiabetes)
control <- trainControl(method="cv",
number=5,
preProcOptions = list(pcaComp=4))
grid=expand.grid(mtry=c(1,2,3))
model <- train(diabetes~., data=PimaIndiansDiabetes, method="rf",
preProcess=c("scale", "center", "pca"),
trControl=control,
tuneGrid=grid)
Run Code Online (Sandbox Code Playgroud) 我想在一个循环中制作多个ggplot,并在一个绘图上显示它们。
for ( i in 1:8) {
g <- ggplot(data=mtcars, aes(x=hp, y=wt))+
geom_point()
print(g)
}
Run Code Online (Sandbox Code Playgroud)
我想将上面的图安排在一页,四行和两列上。有谁知道这是怎么做到的吗?谢谢。
我需要在我的 dash 应用程序上渲染一个 wordcloud。根据此线程https://community.plot.ly/t/solved-is-it-possible-to-make-a-wordcloud-in-dash/4565,dash 中没有 wordcloud 内置组件。一种解决方法是使用WordCloud
模块将 wordcloud 生成为图像并用于dash_html_components.Img
在布局上显示。
我是 Dash 的新手。不知道如何渲染图像。每次生成 wordcloud 时是否需要将 wordcloud 保存为临时图像?
如果在 Dash 方面具有一定专业知识的任何人都可以提供帮助,我将不胜感激。
代码如下:
import dash
import dash_core_components as dcc
import dash_html_components as html
print(dcc.__version__) # 0.6.0 or above is required
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
dfm = pd.DataFrame({'word': ['apple', 'pear', 'orange'], 'freq': [1,3,9]})
app.layout = html.Div([
html.Img(id = 'image_wc')
])
# function to make wordcoud from word frequency dataframe
def plot_wordcloud (data):
d = {} …
Run Code Online (Sandbox Code Playgroud)