edit(guide): restructuring the sample code (#29654)
Add some comment, edit some line of codes to make it easier to understand.
This commit is contained in:
committed by
Randell Dawson
parent
df1eff0d9f
commit
6e80bb3c79
@ -51,21 +51,28 @@ Apply directly by using scikit library, thus making linear regression easy to us
|
||||
import pandas as pd
|
||||
from sklearn.cross_validation import train_test_split
|
||||
from sklearn.linear_model import LinearRegression as lr
|
||||
from sklearn import metrics
|
||||
|
||||
# Load the data
|
||||
train = pd.read_csv('../input/train.csv')
|
||||
test = pd.read_csv('../input/test.csv')
|
||||
|
||||
# Separate the features and labels
|
||||
X = train.iloc[:, 0:4].values
|
||||
y = train.iloc[:, 4].values
|
||||
|
||||
# Split the data into train and test
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
|
||||
X_train
|
||||
|
||||
# Build the model
|
||||
model = lr()
|
||||
model.fit(X_train, y_train)
|
||||
print(model.score(X_train,y_train))
|
||||
print("Training score: ", model.score(X_train,y_train))
|
||||
print("Gradient: ", model.coef_)
|
||||
print("y-intercept: ", model.intercept_)
|
||||
|
||||
# Evaluating the model on test data
|
||||
y_pred_class = model.predict(X_test)
|
||||
model.score(X_train,y_train)
|
||||
print(model.coef_)
|
||||
print(model.intercept_)
|
||||
# calculate accuracy
|
||||
from sklearn import metrics
|
||||
print(metrics.accuracy_score(y_test, y_pred_class))
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user