Skip to content

Instantly share code, notes, and snippets.

@taojy123
Created September 27, 2020 06:01
Show Gist options
  • Save taojy123/2cffced3c0ea80b2869c22ed41323e2f to your computer and use it in GitHub Desktop.
Save taojy123/2cffced3c0ea80b2869c22ed41323e2f to your computer and use it in GitHub Desktop.
LR on sklearn
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
X = []
y = []
lines = open('data.csv').readlines()
for line in lines[1:]:
line = line.strip()
if not line:
continue
id, _y, _x1, _x2 = line.split(',')
X.append([int(_x1), int(_x2)])
y.append(int(_y))
X_train, y_train = X, y
X_test, y_test = X, y
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LogisticRegression(max_iter=50)
model.fit(X_train, y_train)
score = model.score(X_test, y_test)
print(score)
print(dir(model))
print(model.coef_)
print(model.intercept_)
print(model.max_iter)
print(model.n_iter_)
print('--------------------')
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))
print('------------')
xx = (21, 52)
print(model.predict([xx]))
print(model.predict_proba([xx]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment