gpt4 book ai didi

python - 如何提高 scikit-learn 中预测的准确性

转载 作者:行者123 更新时间:2023-12-01 01:17:37 25 4
gpt4 key购买 nike

我想根据 3 个特征1 个目标预测参数。这是我的输入文件(data.csv):

feature.1   feature.2   feature.3   target
1 1 1 0.0625
0.5 0.5 0.5 0.125
0.25 0.25 0.25 0.25
0.125 0.125 0.125 0.5
0.0625 0.0625 0.0625 1

这是我的代码:

import pandas as pd
from sklearn.model_selection import train_test_split
from collections import *
from sklearn.linear_model import LinearRegression

features = pd.read_csv('data.csv')

features.head()
features_name = ['feature.1' , 'feature.2' , 'feature.3']
target_name = ['target']

X = features[features_name]
y = features[target_name]

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 42)

linear_regression_model = LinearRegression()
linear_regression_model.fit(X_train,y_train)

#Here is where I want to predict the target value for these inputs for 3 features
new_data = OrderedDict([('feature.1',0.375) ,('feature.2',0.375),('feature.3',0.375) ])

new_data = pd.Series(new_data).values.reshape(1,-1)
ss = linear_regression_model.predict(new_data)
print (ss)

根据趋势,如果我将 0.375 作为所有特征的输入,我预计会得到 0.1875 左右的值。然而代码预测了这一点:

[[0.44203368]]

这是不正确的。我不知道问题出在哪里。有人知道我该如何修复它吗?

谢谢

最佳答案

您的数据不是线性的。由于功能相同,我只绘制了一维:

enter image description here

通过线性回归模型逼近非线性函数会产生不良结果,就像您所经历的那样。您可以尝试建模一个更好的拟合函数并使用 scipy 拟合其参数:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

关于python - 如何提高 scikit-learn 中预测的准确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54172286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com