I am a beginner of python and I met a question regarding prediction output.
I tried to predict test data 'P1TestingData' in ComplexModel and write it to a CSV file. However, when I print 'prediction' which contains my prediction data out, it shows that there is a numbering starting from 0 before each of my data. How do I remove those numbering? I tried to write it to CSV file but the numberings are still there
Input:
ComplexModel.params
P1TestingData = pd.read_csv('P1TestingData.csv', header=0)
prediction = ComplexModel.predict(P1TestingData)
print(prediction)
f = open("abc.csv", "w")
f.write(str(prediction))
f.close()
Output:
0 53.030011
1 -4.019369
2 14.452136
3 3.773111
4 14.535092
5 42.755951
6 6.566402
7 -0.399492
8 32.981286
9 21.616334
10 27.053343
11 20.591277
12 42.253588
13 9.253549
14 5.583377
15 8.145914
16 13.759630
17 -0.371573
18 5.036368
19 -0.338478
20 65.272560
It seems that str(prediction)
is giving you a string like that: "0 53.030011 \\n 1 -4.019369 ..."
Therefore you can split your string with the function split
.
For exemple, if you wanna split the string to have each line in a array you can do this : str(prediction).split("\\n")
. This will give you an array like that : ["0 53.030011", "1 -4.019369", ...]
Then you want to have only the second number. Same thing, you want to split the string but this time with the space character as your separator.
The final code should looks like this:
prediction_str = str(prediction)
lines = prediction_str.split("\n")
numbers = [line.split(" ")[-1] for line in lines]
print(numbers)
The third line is a bit complicated. What it said is you want to create a new array, for each element in lines. For each line, you split the string into another array with the space character as the separator. "1 -4.019369" becomes [1, -4.019369]. Taking the last item (with [-1]) will get your number.
EDIT: I am not really aware how csv file are working. This is a workaround in your situation, if you cannot gather your numbers in another way.
You can eliminate index column of Pandas
by setting index=False
.
CSV output:
prediction.to_csv(filename, index=False)
Print:
print(prediction.to_string(index=False))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.