import matplotlib.pyplot as plt import numpy as np import pandas as pd from sklearn.linear_model import LogisticRegression from sklearn.metrics import classification_report, confusion_matrix # https://realpython.com/logistic-regression-python/ # For the purpose of this example, let’s just create arrays for the input (π‘₯) and output (𝑦) values: X = np.arange(10).reshape(-1, 1) y = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1]) #y = np.array([0, 1, 0,1, 1, 1, 1, 1, 0, 1]) # The array X is required to be two-dimensional as matrix. print(X) # Once you have the input and output prepared, you can create and define your classification model. # Other solver options are 'newton-cg', 'lbfgs', 'sag', and 'saga'. model = LogisticRegression(solver='liblinear', random_state=0) model.fit(X, y) print(model) # You can use the fact that .fit() returns the model instance and chain the last two statements. # model = LogisticRegression(solver='lbfgs', C=1, random_state=0).fit(X, y) model = LogisticRegression(solver='liblinear', C=1, random_state=0).fit(X, y) print(model.intercept_) #array([-1.04608067]) print(model.coef_,'\n') # The first column is the probability of the predicted output being zero, that is 1 - 𝑝(π‘₯). # The second column is the probability that the output is one, or 𝑝(π‘₯). print(model.predict_proba(X),'\n') print(model.predict(X)) print(model.score(X, y)) # One false positive prediction: The fourth observation is a zero that was wrongly predicted as one. print(confusion_matrix(y, model.predict(X))) # It’s often useful to visualize the confusion matrix. cm = confusion_matrix(y, model.predict(X)) plt.rcParams.update({'font.size': 16}) fig, ax = plt.subplots(figsize=(4, 4)) ax.imshow(cm) ax.grid(False) ax.xaxis.set(ticks=(0, 1), ticklabels=('Predicted 0s', 'Predicted 1s')) ax.yaxis.set(ticks=(0, 1), ticklabels=('Actual 0s', 'Actual 1s')) ax.set_ylim(1.5, -0.5) for i in range(2): for j in range(2): ax.text(j, i, cm[i, j], ha='center', va='center', color='red') plt.show() # The code above creates a heatmap that represents the confusion matrix: # You can get a more comprehensive report on the classification with print(classification_report(y, model.predict(X))) from utilities2 import plot_classifier, plot_confusion_matrix """ X= np.hstack((X, np.zeros((X.shape[0], 1), dtype=X.dtype))) print(X) model.fit(X, y) X1 = np.array(X) y1 = np.array(y) # X= np.array(X).reshape(1,-1) plot_classifier(model,X,y, title='LogReg Cell Classifier boundaries', annotate=False) # plot_classifier(model,X,model.predict_proba(X)[:,1], title='LogReg Cell Classifier boundaries', annotate=False) #plot_classifier(model,ar[:,0],ar[:,1], title='LR Cell Classifier boundaries', annotate=False) plt.show() """ import seaborn as sns def ap_log_regplot(ap_X, ap_y): plt.figure(figsize=(10,5)) sns.regplot(ap_X, model.predict(X), logistic=True, color='green') return None #ap_log_regplot(X, y) #plt.show() sns.set(style = 'whitegrid') sns.regplot(X, model.predict_proba(X)[:,1], logistic=True, scatter_kws={"color": "red"}, line_kws={"color": "blue"}) #label=model.predict(X)) # sns.regplot(X, model.predict(X), logistic=True) plt.title('Logistic Probability Plot') plt.show() #df_confusion_matrix=pd.crosstab(y,model.predict(X)[:,1],rownames=['Actual'],colnames=['Predicted'], normalize=True) #plot_confusion_matrix(df_confusion_matrix, title='Cell Confusion matrix', cmap=plt.cm.Paired) sns.heatmap(cm, annot=True) plt.title('heatmap confusion matrix') plt.show() """ plt.scatter(X[:], y[:], s=5, color='blue', label="original") #plt.plot(x_ax, y, lw=1.5, color="red", label="predicted") plt.title('feature contribution') plt.legend() plt.show() """ # You can also implement logistic regression in Python with the StatsModels package. import statsmodels.api as sm y = np.array([0, 1, 0, 0, 1, 1, 1, 1, 1, 1]) x = sm.add_constant(X) print(x, y) smodel = sm.Logit(y, x) result = smodel.fit(method='newton') print(result.params) print(result.predict(x)) print((result.predict(x) >= 0.5).astype(int)) print(result.pred_table()) print(result.summary(),'\n') # These are detailed reports with values that you can obtain with appropriate methods and attributes. print(result.summary2()) # For more information, check out the official documentation related to LogitResults. # https://www.statsmodels.org/stable/generated/statsmodels.discrete.discrete_model.LogitResults.html