ValueError: could not convert string to float: '-0.33' appears

Asked 2 years ago, Updated 2 years ago, 25 views

ValueError: could not convert string to float: '-0.33' appears
The data is a picture of .

Block Quotation

import numpy as np
from sklearn.linear_model import LinearRegression
arr2d_data=np.loadtxt(fname="/Users/ryotakumazawa/Desktop/]]]lp;:.............../polyreg.csv", delimiter=",")
array_x =arr2d_data[:,0]
array_y=arr2d_data[:,1]
list2d_X = [ ]
for x in array_x:
    list2d_X+=[[x,x**2,x**3]]
arr2d_X = np.array(list2d_X)
reg = LinearRegression()
reg.fit(arr2d_X, array_y)
print('[0:2f]'.format(reg.coef_[0]))
print('[O:2f]'.format(reg.coef_[1]))
print('[0:2f]'.format(reg.coef_[2]))
print('[0:2f]'.format(reg.intercept_))

python

2022-09-30 19:46

2 Answers

You can use the f-string and round() functions.

print(f"{round(reg.coef_[0], 2)}")
print(f"{round(reg.coef_[1], 2)}")
print(f"{round(reg.coef_[2], 2)}")
print(f"{round(reg.intercept_,2)}")


2022-09-30 19:46

print('[0:2f]'.format(reg.coef_[0]))
print('[O:2f]'.format(reg.coef_[1]))
print('[0:2f]'.format(reg.coef_[2]))
print('[0:2f]'.format(reg.intercept_))

If it's not a copy and paste mistake,

print('[O:2f]'.format(reg.coef_[1]))

Maybe it's because this line is print('[0:2f]'... instead of print('[O:2f]'.... It's easy to mistake 0(zero) and O(o) so I think it's also necessary to try to use something different in the editor font.


2022-09-30 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.