Skip to content

Instantly share code, notes, and snippets.

@MinaGabriel
Last active July 17, 2019 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MinaGabriel/13106197696a8644dc3fe5394f3c99d1 to your computer and use it in GitHub Desktop.
Save MinaGabriel/13106197696a8644dc3fe5394f3c99d1 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model, datasets
from scipy.special import expit
from scipy.stats import binom
from scipy import stats
import pandas as pd
plt.subplot(2, 1, 1)
# retrieving data
iris = datasets.load_iris()
X = iris.data[0:100, 0:1]
y = iris.target[0:100]
df = pd.DataFrame(X)
bins = np.linspace(3.9, 7.0, 200)
mu, sigma = df[0:50][0].mean(), df[0:50][0].std() # mean and standard deviation
plt.hist(df[0:50][0], 10, density=True, color='green',
alpha=0.1, edgecolor='black')
plt.plot(bins, 1 / (sigma * np.sqrt(2 * np.pi)) *
np.exp(- (bins - mu) ** 2 / (2 * sigma ** 2)), '--',
linewidth=1, color='r', label='Iris-setosa $\mu = $' + str(round(mu, 2))
+ ', $\sigma =$' + str(round(sigma, 2)))
mu, sigma = df[50:100][0].mean(), df[50:100][0].std() # mean and standard deviation
print(mu)
plt.hist(df[50:100][0], 10, density=True, color='blue', alpha=0.1,
edgecolor='red')
plt.plot(bins, 1 / (sigma * np.sqrt(2 * np.pi)) *
np.exp(- (bins - mu) ** 2 / (2 * sigma ** 2)), '--',
linewidth=1, color='black', label='Iris-versicolor $\mu = $' + str(round(mu, 2))
+ ', $\sigma =$' + str(round(sigma, 2)))
plt.xlabel(u'Sepal Length')
plt.ylabel(u'Frequency')
plt.title('1 feature, 2 classes, PDF')
plt.legend()
plt.subplot(2, 1, 2)
plt.title('1 feature, 2 classes')
plt.axhline(.5, color='.5')
plt.scatter(X[0:50, 0], y[0:50], color='green', alpha=0.1, edgecolor='black', label='Iris-setosa')
plt.scatter(X[50:100, 0], y[50:100], color='blue', alpha=0.1, edgecolor='red', label='Iris-versicolor')
plt.xlabel('Sepal length')
plt.ylabel('Binary Class')
plt.legend()
clf = linear_model.LogisticRegression()
clf.fit(X[0:100, 0].reshape(-1, 1), y[0:100])
print(X[0:100, 0])
print(X[0:100, 0].reshape(1, -1))
print(y[0:100])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment