使用 Julia 库时遇到问题
Having issue using Julia library
我正在尝试 运行 Julia 中的这段代码来计算 knn 值,但是当我 运行 它时出现以下错误。
错误:LoadError:语法:表达式结束后的额外标记“ScikitLearn”
堆栈跟踪:
[1] 顶级范围
@e:\Fontbonne\CIS585独立Study\Code\knn.jl:6
在从 e:\Fontbonne\CIS 585 Independent Study\Code\knn.jl:6
开始的表达式中
错误似乎是第6行的库。我已经搜索了几个小时试图找到解决方案。任何帮助将不胜感激。
这是代码:
import Pkg
Pkg.add("ScikitLearn")
using ScikitLearn: fit!, predict, @sk_import
using DataFrames, CSV, DataStructures
from ScikitLearn.neighbors import KNeighborsClassifier
from ScikitLearn.model_selection import train_test_split
from ScikitLearn.metrics import accuracy_score
function splitTrainTest(data, at = 0.8)
n = nrow(data)
ind = shuffle(1:n)
train_ind = view(ind, 1:floor(Int, at*n))
test_ind = view(ind, (floor(Int, at*n)+1):n)
return data[train_ind,:], data[test_ind,:]
end
# data preparation
df = open("breast-cancer.data") do file
read(file, String)
end
print(df)
X, y = splitTrainTest(df)
# split data into train and test
x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=0.8)
# make model
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(x_train, y_train)
# check accuracy
print(accuracy_score(y_test, knn.predict(x_test)))
该评论应该是一个答案:你正在做
from ScikitLearn.neighbors import KNeighborsClassifier
这是 Python 语法,不是 Julia 语法。如果您尝试在 ScikitLearn.jl 中使用 Python 模型,您可能需要 @sk_import
宏,在您的情况下:
julia> @sk_import neighbors: KNeighborsClassifier
PyObject <class 'sklearn.neighbors._classification.KNeighborsClassifier'>
我正在尝试 运行 Julia 中的这段代码来计算 knn 值,但是当我 运行 它时出现以下错误。
错误:LoadError:语法:表达式结束后的额外标记“ScikitLearn” 堆栈跟踪: [1] 顶级范围 @e:\Fontbonne\CIS585独立Study\Code\knn.jl:6 在从 e:\Fontbonne\CIS 585 Independent Study\Code\knn.jl:6
开始的表达式中错误似乎是第6行的库。我已经搜索了几个小时试图找到解决方案。任何帮助将不胜感激。 这是代码:
import Pkg
Pkg.add("ScikitLearn")
using ScikitLearn: fit!, predict, @sk_import
using DataFrames, CSV, DataStructures
from ScikitLearn.neighbors import KNeighborsClassifier
from ScikitLearn.model_selection import train_test_split
from ScikitLearn.metrics import accuracy_score
function splitTrainTest(data, at = 0.8)
n = nrow(data)
ind = shuffle(1:n)
train_ind = view(ind, 1:floor(Int, at*n))
test_ind = view(ind, (floor(Int, at*n)+1):n)
return data[train_ind,:], data[test_ind,:]
end
# data preparation
df = open("breast-cancer.data") do file
read(file, String)
end
print(df)
X, y = splitTrainTest(df)
# split data into train and test
x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=0.8)
# make model
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(x_train, y_train)
# check accuracy
print(accuracy_score(y_test, knn.predict(x_test)))
该评论应该是一个答案:你正在做
from ScikitLearn.neighbors import KNeighborsClassifier
这是 Python 语法,不是 Julia 语法。如果您尝试在 ScikitLearn.jl 中使用 Python 模型,您可能需要 @sk_import
宏,在您的情况下:
julia> @sk_import neighbors: KNeighborsClassifier
PyObject <class 'sklearn.neighbors._classification.KNeighborsClassifier'>