简体   繁体   中英

Linear Regression's coefficient and intercept kept changing

bach_sal = bach['Salary']
masters_sal = masters['Salary']
phd_sal = phd['Salary']
deg_category_sal = [assoc_sal, bach_sal, masters_sal, phd_sal]

x = np.array(assoc['Person'])
y = np.array(assoc_sal)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.7)
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(x_train.reshape(-1,1),y_train)
print (lr.coef_)
print (lr.intercept_)

I received the coefficient and y intercept but everytime I run the code, they change to a different number. Please help. Thank you.

This is because you did not set the random_state in the train_test split function. This shuffles the cut for test data on each run and as a result, the coefficient changes slightly. Set the random_state to an integer to get the same answer over multiple runs.

https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html

This will keep changing until you specify a fixed value of random_state to your train test line number.

x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.3, random_state=42)

After making this change you might get exact same values. In general, your train and test data will keep on changing until you specify set rows for training and test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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