i 有系数和常数(alpha)。我想像这个例子一样将这些值相乘并相加。 (必须完成300000行)
预测 = 常量 + (valOfRow1 * col1) + (-valOfRow1 * col2) + (-valOfRow1 * col3) + (valOfRow1 * col4) + (valOfRow1 * col5)
预测 = 222 + (555-07 * col1) + (-555-07 * col2) + (-66* col3) + (55* 第 4 列) + (777* 第 5 列)
我有一个一行数据帧,其中包含这样的系数和常数
col1 | col2 | col3 | col4 | 第5栏 | 常数 | |
---|---|---|---|---|---|---|
2.447697e-07 | -5.214072e-07 | -0.000003 | 0.000006 | 555 | 222 |
以及另一个名称完全相同但具有每月值的数据框。
col1 | col2 | col3 | col4 | 第5栏 |
---|---|---|---|---|
16711 | 17961 | 0 | 20 | 55 |
我已经尝试对列进行排序,然后我取它们的乘积
df.dot
。
selected_columns = selected_columns.sort_index(axis=1)
#mean_coefficients dataframe 21th (starting from 0) is constant so i use the other columns
selected_columns['predicted_Mcap']=selected_columns.dot(mean_coefficients.iloc[:,0:20])+mean_coefficients['const']
我使用
mean_coefficients.iloc[:,0:20]
的原因是因为我不想在乘法中得出 const
的结论,只需在最后添加即可。
所以我计算了预测值,但是当我在 Excel 中检查时,该值并不相同。
我计算得对吗?
检查此方法是否可以解决您的任务:
import pandas as pd
# Load the coefficients and variables data frames
df_coefficients = pd.read_clipboard()
df_variables = pd.read_clipboard()
def predict(df_coefficients: pd.DataFrame, df_variables: pd.DataFrame) -> pd.Series:
"""
Predicts the value of the dependent variable based on the values of the independent variables.
:param df_coefficients: DataFrame with the coefficients of the independent variables.
:param df_variables: DataFrame with the values of the independent variables.
:return: Series with the predicted values of the dependent variable.
"""
result = []
# Convert the constants to a pandas Series and remove them from the coefficients DataFrame
constants = df_coefficients.iloc[:]['constant']
df_coefficients.drop(['constant'], inplace=True, axis=1)
# Iterate over the rows of the coefficients DataFrame and calculate the prediction
for idx, val in constants.items():
prediction: float = val + (df_coefficients.iloc[idx][:] * df_variables.iloc[idx][:]).sum()
print(f'prediction {idx}: {prediction}')
result.append(prediction)
return pd.DataFrame({'prediction': result})
result = predict(
df_coefficients=df_coefficients,
df_variables=df_variables
)
result
预测:30746.99484535174
最好的!