Home > front end >  Concatenate a numpy array of strings to numpy array of numbers
Concatenate a numpy array of strings to numpy array of numbers

Time:12-21

I am learning multiple linear regression in python, and for one particular instance I have the following equation: y=b m1x1 m2x2 ... m5x5. I will like to print out the same equation on my terminal in the same format with the values inserted for m and b

As of now I have the following code implemented:

insert numpy as np
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
...
print("b= ",regressor.intercept_) # b= 42467.529248548686
m= np.array(regressor.coef_)
print(m) # [8.66e 01 -8.73e 02  7.86e 02  7.73e-01  3.29e-02  3.66e-02] 
x_var= ['x'  str(i) for i in range(1,6)]
print(x_var) # it gives ['x1', 'x2', 'x3', 'x4', 'x5']

I know that my question might be super basic, but how should how do I concatenate the numpy array of integers m with array of strings x_var?

Thanks in advance.

CodePudding user response:

you can use the zip function:

equation = []
for coef, var in zip(m, ['']   x_var):
    equation.append(str(coef)   var)

print("   ".join(equation))

or

equation = [str(m[0])]
for coef, var in zip(m[1:], x_var):
    equation.append(str(coef)   var)

print("   ".join(equation))
  • Related