I want to predict a parameter based on 3 features and 1 target. Here is my input file (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
Here is my code:
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)
According to the trend, I expect to get a value around 0.1875 if I give 0.375 as input for all of the features. However the code predicts this:
[[0.44203368]]
Which is not correct. I do not know where the problem is. Does anybody know how I can fix it?
Thanks
Your data is not linear. I have plotted just one dimension since the features are identical:
Approximating a Non-Linear function by a LinearRegression model creates bad results, like you experienced. You could try to model a better fitting function and fit its parameters with scipy: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html