我有以下 Pandas 数据框,其中 a 列代表一个虚拟变量:

我想要做的是在cmap='jet'column 的值之后给我的标记一个颜色b,除非column 中的值a等于 1 - 在这种情况下,我希望它是灰色。
知道我该怎么做吗?
我有一个带有全局变量的模块,里面有很多变量。对于特定的子例程,我想使用其中除 1 之外的所有变量。
我可以做到这一点的一种方法是
use Global_Variables, only : item1,...item50,item52,...item100
Run Code Online (Sandbox Code Playgroud)
但是写出 99 个要使用的项目是非常痛苦的,所以在这个例子中我可以跳过 item51。
我也可以把这个特定的项目放在一个单独的Global_Variables2模块中,但这很笨拙。
是否有一个except子句可以与该子句类似但相反地使用only?
有没有办法在 Fortran 中将数组(向量、矩阵,甚至标量)设置为零?2003/2008 似乎将 Fortran 提升到了一个非常现代的水平,我想知道它们是否包含一种无需执行即可将数组值设置为零的简单方法
do i = 1,X
do j = 1,Y
A(i,j) = 0
enddo
enddo
Run Code Online (Sandbox Code Playgroud)
其中 X 是行数,Y 是二维矩阵中的列数。这可以推广到任意多个维度,但原理是相同的。
在 Matlab 中,这很容易用 zeros 函数完成,即,
A = zeros(X,Y)
Run Code Online (Sandbox Code Playgroud)
现代 Fortran 似乎将很多人们喜欢的关于 Matlab 和其他语言的东西融入到它的曲目中,所以我很好奇他们是否对这个简单而基本的任务有内在的东西。
或者,也许在现代 Fortran 中,没有必要通过初始化数组来清除内存中以前存储的任何值?
我想更短的方法就是去
real, dimension(X,Y) :: A
A = A*0.0 ! Set all elements to zero by multiplying matrix by scalar value 0
Run Code Online (Sandbox Code Playgroud)
但是关于内在的问题仍然存在。
在下面的代码中,我有一个复合类型,在我的实际代码中,几个字段是矩阵.在这个例子中只有1.当我尝试构造复合类型时,我不断得到堆栈溢出.这是代码示例:
struct Tables
eij::Array{Float64,2}
end
Tables(eij::Array{Float64,2}) = Tables(eij) # works if this is commented out
x = 5 # arbitrary matrix dimension
e_ij = Array{Float64,2}(undef,x,x)
for i=1:x
for j=1:x
e_ij[i,j] = i*j/2.3 #dummy numbers, but not the case in real code
end
end
vdwTable = Tables([e_ij[i,j] for i=1:x,j=1:x])
Run Code Online (Sandbox Code Playgroud)
我使用临时变量e_ij来制作矩阵,因为我不希望复合Tables变体是可变的.所以,我的理由是,通过首先在虚拟变量中生成表格e_ij,然后我可以初始化Tables我真正想要的不可变量.
如果我注释掉结构的外部构造函数,Tables它就可以工作.但是,我实际上希望有几个不同的外部构造函数用于不同字段未传递数据以进行初始化的情况.在这些情况下,我想给它们默认矩阵.
我得到的错误如下:ERROR: LoadError: StackOverflowError:在线vdwTable = Tables([e_ij[i,j] for i=1:x,j=1:x])
我很难在 excel 文件中读取pandas DataFrame并将存储的矩阵转换为numpy array. 我认为问题的一部分是矩阵存储不当。但是,我无法控制电子表格,这就是它发送给我的方式。
例如,这是存储在单元格中的字符串
[[[ 0. 0. 0.107851]
[ 0. 0. -0.862809]]]
Run Code Online (Sandbox Code Playgroud)
我在行中读取DataFrame, 并将每个单元格保存到一个变量中。然后我尝试将此特定变量转换为 a,np.array因为这些数字代表两组 x、y、z 坐标。
我已经尝试过np.fromstring,np.asarray但无济于事。它会将字符串转换为一个 numpy 数组,但如果括号内仍为字符,这将是一个可怕的混乱。我试过使用 np.squeeze 去掉括号,但它说维度不是 1。
如果我使用np.asarray(item._coord, dtype=float)然后它失败说它不能将字符串转换为浮点数。
ValueError: could not convert string to float: '[[[ 0. 0. 0.107851] [ 0. 0. -0.862809]]]'
有一个 '\n' 出现在它的中间,在两个列表之间。我df = df.replace(r'\n', ' ',regex=True)' to clean out the在尝试数据转换之前使用了\n`。
我被卡住了
我有一个包含STL向量的对象.我从矢量大小为零开始,并使用push_back它来添加它.所以,push_back工作正常.
在我的代码中,向量中的每个元素代表一个原子.因此,该STL载体在其内部的对象是"分子".
当我试图从我的分子中移除一个原子,即从阵列中擦除其中一个元素时,该erase()功能不起作用.其他方法可以工作,如size()和clear().clear()删除所有元素,这是过度的.erase()正是我想要的,但由于某种原因它不起作用.
这是我的代码的一个非常简化的版本.但是,它确实代表了问题.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class atomInfo
{
/* real code has more variables and methods */
public:
atomInfo () {} ;
};
class molInfo
{
/* There is more than 1 atom per molecule */
/* real code has more variables and methods */
public:
vector <atomInfo> atom;
molInfo () {};
};
int main () …Run Code Online (Sandbox Code Playgroud) 我想定义一个结构来容纳3个元素的向量
mutable struct Coords
r::Array{Float64,3} # essentially holds x,y,z coords
end
Run Code Online (Sandbox Code Playgroud)
现在,我想对这些结构进行排列,并为每个结构内的向量提供随机值。
这是我淡出的地方。我尝试了一些我将要描述的东西,但是没有一个起作用。
试用1:
x = 10 # I want the array to be of length 10
arrayOfStructs::Array{Coords,x}
for i=1:x
arrayOfStructs[i].r = rand(3)
end
Run Code Online (Sandbox Code Playgroud)
错误消息是
ERROR: LoadError: MethodError: Cannot `convert` an object of type Int64 to an object of type Array{C
oords,10}
Closest candidates are:
convert(::Type{T<:Array}, ::AbstractArray) where T<:Array at array.jl:489
convert(::Type{T<:AbstractArray}, ::T<:AbstractArray) where T<:AbstractArray at abstractarray.jl:1
4
convert(::Type{T<:AbstractArray}, ::LinearAlgebra.Factorization) where T<:AbstractArray at C:\User
s\julia\AppData\Local\Julia-1.0.2\share\julia\stdlib\v1.0\LinearAlgebra\src\factorization.jl:46
...
Stacktrace:
[1] setindex!(::Array{Array{Coords,10},1}, ::Int64, …Run Code Online (Sandbox Code Playgroud) 在Julia中是否有可能有两个具有相同名称的结构,但是可以分配不同的类型,从而可以区分?
我一直在阅读https://docs.julialang.org/en/v1/manual/types/#Parametric-Types-1,它似乎正在引导我想要的东西,但我无法让它工作......
在用于分子模拟的力场中,存在用于描述分子中的扭转角的二面角参数.有不同种类,例如目的让我们将它们限制为2种:正确和不正确.我想有两个结构,都称为二面角,但给出了"正确"和"不正确"的类型.然后,我将使用特定于每种类型的方法来计算由于二面角引起的力.我认为抽象的参数类型让我最接近我想要的但我无法将它们排序......
abstract type proper end
abstract type improper end
struct Dihedral <: proper
ai::Int64
kparam::Vector{Float64}
end
struct Dihedral <: improper
ai:Int64
kparam::Float64
end
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用......我尝试过使用
abstract type dihedral end
abstract type proper <: dihedral end
abstract type improper <: dihedral end
struct Dihedral <: dihedral{proper}
...
end
struct Dihedral <: dihedral{improper}
...
end
Run Code Online (Sandbox Code Playgroud)
但我总是因重新定义而陷入困境 Dihedral
ERROR: LoadError: invalid redefinition of constant Dihedral
Stacktrace:
[1] top-level scope at none:0
Run Code Online (Sandbox Code Playgroud)
我的想法是我可以添加更多类型的二面角,我需要做的就是添加他们的方法,模拟将自动使用新的dihedral.methods.如果我尝试使用不同名称的结构,那么我开始使用if语句将程序引导到正确的结构,然后使用正确的方法......这就是我想要避免的,即,
if dihedraltype == "proper"
struct_proper(...)
elseif dihedraltype =="improper" …Run Code Online (Sandbox Code Playgroud) 我想在所有的a值row或column一的Matrix零级的(Float64在这种情况下),而不诉诸for循环手册。
fill!并zero在整个矩阵上工作,但不在单个列或行上工作(至少我的尝试失败了......即,fill!(tester[:,1],0.0)不起作用。
这是手动方法的示例
tester = [[22.2 33.3 44.4]; [44.4 55.5 66.6]; [77.7 88.8 99.9] ]
for i = 1:size(tester,1)
tester[i,1] = 0.0
end
Run Code Online (Sandbox Code Playgroud)
初始化和修改的输出是
[22.2 33.3 44.4; 44.4 55.5 66.6; 77.7 88.8 99.9] <-- Initial matrix
[0.0 33.3 44.4; 0.0 55.5 66.6; 0.0 88.8 99.9] <-- Correct change
Run Code Online (Sandbox Code Playgroud)
领悟也许可以用来让for loop看起来更整洁一点,但这只是化妆品。我想知道是否有类似fill!或zero可以使用的实际功能?
julia ×4
fortran ×2
pandas ×2
arrays ×1
c++ ×1
dataframe ×1
fortran2003 ×1
fortran2008 ×1
matplotlib ×1
numpy ×1
python ×1
python-3.x ×1
scatter-plot ×1
stl ×1
struct ×1