027. (7.25) The basic method of sklearn linear regression

Divide the training set and test set:

X_train,X_test, y_train, y_test = sklearn.model_selection.train_test_split(train_data,train_target,test_size=0.25, random_state=0,stratify=y_train)

# train_data:所要划分的样本特征集

# train_target:所要划分的样本结果

# test_size:样本占比,如果是整数的话就是样本的数量

# random_state:是随机数的种子。
# 随机数种子:其实就是该组随机数的编号,在需要重复试验的时候,保证得到一组一样的随机数。比如你每次都填1,其他参数一样的情况下你得到的随机数组是一样的。但填0或不填,每次都会不一样。

# stratify是为了保持 split前 类的分布。

sklearn's train_test_split() function parameter meaning interpretation

Linear regression:

from sklearn.linear_model import LinearRegression

reg = linear_model.LinearRegression()
reg.fit(X,y)

LinearRegression has implemented a multiple linear regression model. Of course, it can also be used to calculate a univariate linear model by using list[list] to pass data.

  • fit(X,y,sample_weight=None): X,y are passed in as a matrix, while sample_weight is the weight of each test data, which is also passed in in array format.
  • predict(X): prediction method, will return the predicted value y_pred
  • score(X,y,sample_weight=None): scoring function, will return a score less than 1, which may be less than 0

LinearRegression stores the equation in two parts, coef_ stores the regression coefficients, and intercept_ stores the intercept. Therefore, to view the equation, it is to view the values ​​of these two variables.

Guess you like

Origin blog.csdn.net/u013598957/article/details/107577895