julia 操作
1.normal data
Julia 中的索引都是从 1 开始的,最后一个元素的索引与字符串长度相同,都是 n
在任何索引表达式中,关键词 end 都是最后一个索引值(由 endof(str) 计算得到)的缩写。可以对字符串做 end 算术或其它运算:
regular expression
https://www.w3cschool.cn/julia/y4vh1jfe.html
浮点数类型中存在两个零 ,正数的零和负数的零。它们相等,但有着不同的二进制表示
string(greet, ", ", whom, ".\n")
"$greet, $whom.\n" #Julia 允许使用 $ 来内插字符串文本
"1 + 2 = $(1 + 2)" #可以使用小括号将任意表达式内插
"v: $v" #v = [1,2,3]
print("I have \$100 in my account.\n") #v要在字符串文本中包含 $ 文本
search("xylophone", 'o', 5) #7
repeat(".:Z:.", 10) #".:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:."
endof(str) #给出 str 的最大(字节)索引值
length(str) #给出 str 的字符数
i = start(str) # 给出第一个可在 str 中被找到的字符的有效索引值(一般为 1 )
#regular expression
r"^\s*(?:#|$)"
float32(-1.5)
0.0 == -0.0 #true
bits(0.0) bits(-0.0)
eps(Float32) #可以用来检查 1.0 和下一个可表示的浮点数之间的间距
2x^2 - 3x + 1 #2^3x 被解析为 2^(3x)
NaN != NaN #true
NaN == NaN #true
NaN > NaN #true
NaN < NaN #true
Bool运算符
!x 非 true 和 false 互换
位运算符
~x 按位取反
x & y 按位与
x | y 按位或
x $ y 按位异或
x >>> y 向右 逻辑移位 (高位补 0 )
x >> y 向右 算术移位 (复制原高位)
x << y 向左逻辑/算术移位
有限数按照正常方式做比较。
正数的零等于但不大于负数的零。
Inf 等于它本身,并且大于所有数, 除了 NaN。
-Inf 等于它本身,并且小于所有数, 除了 NaN。
NaN 不等于、不大于、不小于任何数,包括它本身
函数 测试
isequal(x, y) x 是否等价于 y
isfinite(x) x 是否为有限的数
isinf(x) x 是否为无限的数
isnan(x) x 是否不是数
isequal 也可以用来区分有符号的零, isequal(-0.0, 0.0)#false
链式比较
1.函数
function f(x,y)
x + y
end
1.interact with MAT
#To read all variables from a MAT file as a Dict:
vars = matopen("matfile.mat")
varnames = names(vars )
close(file)
newdata = hcat(vars["FD"]["TimeStamp"],vars["FD"]["FactorData"] )
df = DataFrame(newdata)
rename!(df, Dict(:x1=>:TimeStamp))
#write as a dict
matwrite("matfile.mat", Dict(
"myvar1" => 0,
"myvar2" => 1
); compress = true)
DataFrame
2. Dataframe
2. text
f = open("mydata.txt", "r")
lines = readlines(f) # 将每一行的文本分开保存到一个数组里
for line in lines
println(line)
end
close(f)
julia dataframe
readin list of csv into single datafame
https://stackoverflow.com/questions/58607996/reading-csv-file-in-loop-dataframe-julia
vars = matread("matfile.mat")
#To write a variable to a MAT file:
file = matopen("matfile.mat", "w")
write(file, "varname", variable)
close(file)
#To read a single variable from a MAT file (compressed files are detected and handled automatically):
file = matopen("matfile.mat")
read(file, "varname") # note that this does NOT introduce a variable ``varname`` into scope
close(file)
# read in the files into a vector
a = CSV.read.(["0.$i.csv" for i in 1:8])
# add an indicator column
for i in 1:8
a[i][!, :id] .= i
end
# create a single data frame with indicator column holding the source
b = reduce(vcat, a)
df1 = DataFrame(b = [:Hi, :Med, :Hi, :Low, :Hi],
x = ["A", "E", "I", "O","A"],
y = [6, 3, 7, 2, 1],
z = [2, 1, 1, 2, 2])
df1
df1 = sort(df1,[:z]) # sorted z then y
#index doesn't change with sorting
#The simplest way of constructing a DataFrame is to pass column vectors using keyword arguments or pairs
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
#Constructing Column by Column
df = DataFrame()
df.A = 1:8
#Constructing Row by Row
df = DataFrame(A = Int[], B = String[])
push!(df, (1, "M"))
push!(df, Dict(:B => "F", :A => 3))
df = DataFrame(a=[1, 2, 3], b=[:a, :b, :c])
# write DataFrame out to CSV file
CSV.write("dataframe.csv", df)
DataFrame(CSV.File(input))
#head tail
first(df, 6)
last(df, 6)
DataFrame(a = 1:2, b = [1.0, missing], c = categorical('a':'b'), d = [1//2, missing])
df[1:3, :] #: indicates that all items (rows or columns depending on its position) should be retained:
df[:, [:A, :B]]
#It is also possible to use a regular expression as a selector of columns matching it:
df[!, r"x"] #any one contains x in columns
df[!, Not(:x1)] #not x1
df[df.A .> 500, :]
df[(df.A .> 500) .& (300 .< df.C .< 400), :]
df[!, [:A]] and df[:, [:A]] return a DataFrame object, while df[!, :A] and df[:, :A] return a vector
Columns can be directly (i.e. without copying) accessed via df.col, df.“col”, df[!, :col] or df[!, “col”]. The two latter syntaxes are more flexible as they allow passing a variable holding the name of the column, and not only a literal name
Note that column names can be either symbols (written as :col, :var"col" or Symbol(“col”)) or strings (written as “col”). Note that in the forms df.“col” and :var"col" variable interpolation into a string using $ does not work.
Since df[!, :col] does not make a copy
get a copy of the column use df[:, :col]
To get column names as Symbols use the propertynames function:
propertynames(df)
Column names can be obtained as strings using the names function:
names(df)
size(df)
1. julia function
x,y = y,x
map(sqrt,[1,2,3]) # 等价于 [sqrt(1),sqrt(2),sqrt(3)]
map(x-x^2,[1,2,3])
g2 = x -> x^2 #lambda function
g2(3.5), g2("I ♡ Julia ") # 使用 \heartsuit + TAB 输出 ♡ 字符
array
counting = [2 * i for i in 1:10]
push!(mysequence,100) #将数据项添加至数组的末尾
pop!(mysequence) #现在,删除100
函数小技巧
- push!: 以!结尾的函数。!在函数名末尾,这是什么意思?更改或修改其输入的函数称为变异函数
- 逐点应用的功能,f.(x, y)和x .+ y(称为“广播”)
当我们添加一个.在函数名称之后,我们告诉Julia我们想要通过传递给函数的输入“ 广播 ”该函数。这意味着我们希望在输入上以元素方式应用该功能; 换句话说,它将函数应用于输入的每个元素,并返回具有新计算值的数组。
g2 = x -> x^2
map(g2, [1,2,3,4])
(x->x^2) (2) # (2)是调用
g(x) = x^2 #simple fast defintion
宏macro
using PyCall
@pyimport numpy as np