Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test Linear Regression in Python
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import numpy as np
intercept = -2.0
beta = 5.0
n_samples = 1000
regularization = 1e30
X = np.random.normal(size=(n_samples,1))
linepred = intercept + (X*beta)
prob = np.exp(linepred) / (1.0 + np.exp(linepred))
y = (np.random.uniform(size=X.shape) < prob).astype(np.float)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
print "Percent of class 1: ", sum(y)/len(y)
plt.figure()
plt.plot(X,prob, '.', color='blue', label='model', markersize=0.5)
clf = LogisticRegression(solver='liblinear', tol=1e-10, max_iter=10000, C=regularization);
clf.fit(X_train, y_train.ravel());
print "Coeff: {}, Intercept: {}".format(clf.coef_, clf.intercept_)
print "Score over training: ", clf.score(X_train, y_train)
print "Score over testing: ", clf.score(X_test, y_test)
plt.plot(X, clf.predict_proba(X)[:,1], '.', color='red', label='clf', markersize=0.5)
tot_score = clf.score(X, y)
plt.title("Score: {}".format(tot_score))
plt.legend(loc='best');
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.