<jupyter_start><jupyter_text>In Statsmodels<jupyter_code>from sklearn.datasets import make_classification

X0, y = make_classification(n_features=1, n_redundant=0, n_informative=1, 
                            n_clusters_per_class=1, random_state=4)
X = sm.add_constant(X0)
# chisqprob 함수가 없다는 오류가 발생하면 다음 두 줄을 실행한다.
from scipy import stats
stats.chisqprob = lambda chisq, df: stats.chi2.sf(chisq, df)

logit_mod = sm.Logit(y, X)
logit_res = logit_mod.fit(disp=0)
print(logit_res.summary())
from sklearn.datasets import load_iris
df = pd.DataFrame(iris.data, columns=['sepal_length','sepal_width','petal_length','petal_width'])
sy = pd.Series(iris.target, dtype="category")
sy = sy.cat.rename_categories(iris.target_names)

df.tail()
df['species'] = sy
df = df[df['species'] != 'virginica' ]
df.tail()
dic = {'setosa' : 0, 'versicolor' : 1}
df['species'] = df['species'].map(dic)
df.tail()
model = sm.Logit.from_formula("species ~ sepal_length", df)
result = model.fit(disp=0)
print(result.summary())
dfx = df['sepal_length']
dfy = df['species']
df['predict'] = result.predict(dfx)
df['predict_01'] = np.array([1 if i > 0.5 else 0 for i in df.predict.values])
from sklearn.metrics import confusion_matrix
confusion_matrix(df.species, df.predict_01)
from sklearn.metrics import classification_report
print(classification_report(df.species, df.predict_01))
from sklearn.metrics import roc_curve
from sklearn.linear_model import LogisticRegression

fpr, tpr, thresholds = roc_curve(df.species, result.predict(df.sepal_length))
plt.plot(fpr, tpr, label="Logistic Regression")
plt.plot([0, 1], [0, 1], 'k--', label="random guess")
plt.xlabel('False Positive Rate (Fall-Out)')
plt.ylabel('True Positive Rate (Recall)')
plt.title('Receiver operating characteristic example')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14657079646017698, 'fraction_numerical': 0.014380530973451327, 'mean_word_length': 6.294354838709677, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '133904', 'license': 'MIT', 'n_tokens_mistral': 713, 'n_tokens_neox': 689, 'n_words': 139, 'path': 'machinelearning/classification/logistic regression/logistic_regression.ipynb', 'repo_name': 'Junhojuno/TIL', 'size': '1808'}