Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working #7

Open
Juphex opened this issue Jan 15, 2020 · 1 comment
Open

Not working #7

Juphex opened this issue Jan 15, 2020 · 1 comment

Comments

@Juphex
Copy link

Juphex commented Jan 15, 2020

I did something like this:

#convert to libsvm format https://github.com/zygmuntz/phraug/blob/master/csv2libsvm.py
#python csv2libsvm.py spambase_renamed_class.csv libsvm.data -1 False

data = open("spambase/libsvm.data", "r")
data2 = open("spambase/libsvm2.data", "a")

i = 0
for line in data:
    temp = line[-3:]
    white_space_index = line[2:].find(" ")
    
    if i == 0:
        white_space_index = 0
        i += 1
    
    if "-1" in temp:
        line = "-1 " + line[white_space_index + 2:-6]
    else:
        line = "1 " + line[white_space_index + 2:-6]

        #drop last index (class) and first because it is the index nr.
    data2.write(line + "\n")
data.close()
data2.close()
@Juphex
Copy link
Author

Juphex commented Feb 1, 2020

When you know the highest occuring index you can do something like this:

data = open("iris.scale", "r")
def read_libsvm(data, col_length):
    new_data = np.zeros((1,col_length))
    i = 0
    for line in data:
        label = line[:1]
        #split splits on whitespace by default
        x = line[1:].split()
        if i > 0:
            new_data = np.vstack((new_data, np.zeros((1,col_length))))
        for j in range(len(x)):
            temp_string = x[j].split(":")
            new_data[i, int(temp_string[0]) - 1] = np.float32(temp_string[1])
        new_data[i, col_length - 1] = int(label)
        i += 1
    return new_data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant