Search code examples
juliaknn

Having issue using Julia library


I am trying to run this code in Julia to calculate the knn value, but I get the following error when I run it.

ERROR: LoadError: syntax: extra token "ScikitLearn" after end of expression Stacktrace: [1] top-level scope @ e:\Fontbonne\CIS 585 Independent Study\Code\knn.jl:6 in expression starting at e:\Fontbonne\CIS 585 Independent Study\Code\knn.jl:6

The error seems to be the library on line 6. I have searched for a couple of hours to try and find a solution. Any help would be greatly appreciated. Here is the code:

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)))

Solution

  • That comment should have been an answer: You're doing

    from ScikitLearn.neighbors import KNeighborsClassifier
    

    which is Python syntax, not Julia syntax. If you're trying to use a Python model in ScikitLearn.jl you probably want the @sk_import macro, in your case:

    julia> @sk_import neighbors: KNeighborsClassifier
    PyObject <class 'sklearn.neighbors._classification.KNeighborsClassifier'>