-1

I am using dataset to see the relationship between salary and college GPA. I am using sklearn linear regression model. I think the coefficients should be intercept and the 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]])
Sijan Bhandari
  • 2,941
  • 3
  • 23
  • 36

3 Answers3

5

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

Sosel
  • 1,678
  • 1
  • 16
  • 31
0
salary_pred = regression.predict(gpa_test)
print salary_pred
print salary_test

I think salary_pred = regression.coef_*salary_test. Have a try that printed salary_pred and salary_test via pyplot. Figure can explain every thing.

チーズパン
  • 2,752
  • 8
  • 42
  • 63
Windyground
  • 119
  • 6
0

Here you are training your model on a single feature gpa and a target salary:

regression.fit(gpa_train, salary_train)

If you train your model on multiple features e.g. 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.

It is equivalent to: ax + by + c = salary (where c is the intercept, a and b are the coefficients).

thepunitsingh
  • 713
  • 1
  • 12
  • 30
Shariq Ehsan
  • 186
  • 1
  • 8