[Solved] Converting list into array in python – Machine Learning


It’s not a must, but it’s very convenient because of a huge amount of handy and very fast (vectorized) functions/methods, provided by Numpy/SciPy modules.

Actually most of the machine-learning methods (at least in the sklearn module) will try to convert input arrays into Numpy arrays, in order to be able to use Numpy’s functions/methods.

Consider the following demo, where i’m not using Numpy arrays, but a “Vanilla” Python lists:

from sklearn.linear_model import LinearRegression

X = [[1,2,3], [4, 5, 6], [7,8,9]]
y = [30, 20, 10]

lr = LinearRegression().fit(X, y)

pred = lr.predict([[13,14,15], [16,17,18]])

print(pred)

print(type(pred))

Output:

[-10. -20.]

<class 'numpy.ndarray'>

solved Converting list into array in python – Machine Learning