简体   繁体   English

sklearn 线性回归系数具有单值输出

[英]sklearn Linear Regression coefficients have single value output

I am using dataset to see the relationship between salary and college GPA.我正在使用数据集来查看薪水和大学 GPA 之间的关系。 I am using sklearn linear regression model.我正在使用 sklearn 线性回归模型。 I think the coefficients should be intercept and the coff.我认为系数应该是截距和 coff。 value of corresponding feature.对应特征的值。 But the model is giving a single value.但该模型给出了一个单一的值。

from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LinearRegression

# Use only one feature : CollegeGPA
labour_data_gpa = labour_data[['collegeGPA']]

# salary as a dependent variable
labour_data_salary = labour_data[['Salary']]

# Split the data into training/testing sets
gpa_train, gpa_test, salary_train, salary_test = train_test_split(labour_data_gpa, labour_data_salary)

# Create linear regression object
 regression = LinearRegression()

# Train the model using the training sets (first parameter is x )
 regression.fit(gpa_train, salary_train)

#coefficients 
regression.coef_

The output is : Out[12]: array([[ 3235.66359637]])

Try:尝试:

regression = LinearRegression(fit_intercept =True)
regression.fit(gpa_train, salary_train)

and the results will be in结果将在

regression.coef_
regression.intercept_

In order to get a better understanding of your linear regression, you maybe should consider another module, the following tutorial helps: http://statsmodels.sourceforge.net/devel/examples/notebooks/generated/ols.html为了更好地理解您的线性回归,您可能应该考虑另一个模块,以下教程有帮助: http : //statsmodels.sourceforge.net/devel/examples/notebooks/generated/ols.html

salary_pred = regression.predict(gpa_test)
print salary_pred
print salary_test

I think s alary_pred = regression.coef_*salary_test .我认为 s alary_pred = regression.coef_*salary_test Have a try that printed salary_pred and salary_test via pyplot.有一个尝试,印刷salary_predsalary_test通过pyplot。 Figure can explain every thing.图可以解释一切。

Here you are training your model on a single feature gpa and a target salary :在这里,您正在根据单个特征gpa和目标salary训练模型:

regression.fit(gpa_train, salary_train)

If you train your model on multiple features eg python_gpa and java_gpa (with the target as salary ), then you would get two outputs signifying coefficients of the equation (for the linear regression model) and a single intercept.如果您在多个特征上训练您的模型,例如python_gpajava_gpa (目标为salary ),那么您将得到两个表示方程系数的输出(对于线性回归模型)和一个截距。

It is equivalent to: ax + by + c = salary (where c is the intercept, a and b are the coefficients).等价于: ax + by + c = salary (其中c是截距, ab是系数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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