我已经开始学习张量流,并且很难理解占位符/变量问题。
我正在尝试编写一个矩阵乘法函数。它在使用 tf.constant 时有效,但我很难理解如何使用变量
这是我的代码
import tensorflow as tf
import numpy as np
mat_1 = np.array([[0,1,1,0], [1,0,1,0], [1,0,0,1], [0,1,1,0]]).astype('int32')
mat_2 = np.array([[0,1,1,0], [1,0,1,0], [1,0,0,1], [0,1,1,0]]).astype('int32')
def my_matmult1(mat_1, mat_2):
#define session
x_sess = tf.Session()
with x_sess:
xmat_1 = tf.constant(mat_1)
xmat_2 = tf.constant(mat_2)
r1 = tf.matmul(xmat_1, xmat_2)
qq1 = x_sess.run(r1)
return qq1
def my_matmult2(mat_1, mat_2):
#define session
x_sess1 = tf.Session()
with x_sess1:
#initialize placeholders
xmat_1_plh = tf.placeholder(dtype=mat_1.dtype, shape=mat_1.shape)
xmat_2_plh = tf.placeholder(dtype=mat_2.dtype, shape=mat_2.shape)
#create variables
x_mat_1 = tf.Variable(xmat_1_plh, trainable = False)
x_mat_2 = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用plyr,但在使用多个变量时遇到困难.这是一个例子.
df <- read.table(header=TRUE, text="
Firm Foreign SME Turnover
A1 N Y 200
A2 N N 1000
A3 Y Y 100
A1 N N 500
A2 Y Y 200
A3 Y Y 1000
A1 Y N 200
A2 N N 1000
A2 N Y 100
A2 N Y 200 ")
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个表,总结两个变量的营业额.基本上结合以下代码
t1 <- ddply(df, c('Firm', 'Foreign'), summarise,
BudgetForeign = sum(Turnover, na.rm = TRUE))
t2 <- ddply(df, c('Firm', 'SME'), summarise,
BudgetSME = sum(Turnover, na.rm = TRUE))
Run Code Online (Sandbox Code Playgroud)
结果如下
res <- read.table(header=TRUE, text=" …Run Code Online (Sandbox Code Playgroud) 我正在尝试导出带有一些可视化规范的图形文件.我无法弄清楚如何添加多级属性.
Import networkx as nx
#Create the Graph
g = nx.Graph()
g.add_edge('Andre', 'Beverly')
g.add_edge('Andre', 'Diane')
g.add_edge('Andre', 'Carol')
g.add_edge('Andre', 'Fernando')
g.add_edge('Beverly', 'Diane')
nx.draw(g)
Run Code Online (Sandbox Code Playgroud)
我想做的是添加位置但使用特定属性(名称和结构)
# compute position
pos = nx.spring_layout(g)
# add attribute
g.node["Andre"]["viz"]["position"]["x"]= pos["Andre"][0]
g.node["Andre"]["viz"]["position"]["y"]= pos["Andre"][1]
Run Code Online (Sandbox Code Playgroud)
我其实有两个问题:
我在使用 pandas 中带有列名称的数据透视表时遇到问题。
这是我的问题
from collections import OrderedDict
import pandas as pd
table = OrderedDict((
("Item", ['Item0', 'Item0', 'Item1', 'Item1']),
('CType',['Gold', 'Bronze', 'Gold', 'Silver']),
('USD', [1, 2, 3, 4]),
('EU', [1, 2, 3, 4])
))
d = pd.DataFrame(table)
print d
p = d.pivot_table(index='Item', columns='CType', values='USD')
print p
p.fillna(0, inplace=True)
print p
Run Code Online (Sandbox Code Playgroud)
下面的操作给了我一个奇怪形状的 NaN 。 我缺少什么?
p / p.sum(axis = 1)
Run Code Online (Sandbox Code Playgroud)
PS:数据示例取自此处,但我自己的数据显示相同的行为