简体   繁体   English

将 numpy 字符串数组连接到 numpy 数字数组

[英]Concatenate a numpy array of strings to numpy array of numbers

I am learning multiple linear regression in python, and for one particular instance I have the following equation: y=b+m1x1+m2x2+...+m5x5 .我在 python 中学习多元线性回归,对于一个特定的例子,我有以下等式: 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我想在我的终端上以相同的格式打印出相同的方程,并为mb插入值

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 ?我知道我的问题可能是超级基本的,但是我应该如何将整数m的 numpy 数组与字符串数组x_var连接起来?

Thanks in advance.提前致谢。

you can use the zip function:您可以使用 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM