<jupyter_start><jupyter_text>Logistic Regression<jupyter_code>import numpy as np
import pandas as pd
from sklearn import linear_model

# Load data
file_name = "https://raw.githubusercontent.com/rajeevratan84/datascienceforbusiness/master/heights_weights.csv"
df = pd.read_csv(file_name)
df.head()
df.shape
# Plotting our data
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt

ax = sns.scatterplot(x="Height", y="Weight", hue="Male", data=df)
# Extract the columns we'll use for our data

x = df.iloc[:,0:2].values 
y = df.iloc[:,2].values
# Split data into our test and training datasets
from sklearn.model_selection import train_test_split

X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size=0.3, random_state=0)
# Fit (train) the Logistic Regression classifier
clf = linear_model.LogisticRegression(C=1e40, solver='newton-cg')
fitted_model = clf.fit(X_train, Y_train)
# Predict based on height (inches) and weight (lbs)
height = 61
weight = 125

# Get prediction
prediction = clf.predict([(height,weight)])

if prediction[0]:
  result = "Male"
else:
  result = "Female" 

print("Person is " + result)<jupyter_output>Person is Female<jupyter_text>Displaying our theta parameter values![alt text](https://miro.medium.com/max/2220/1*siWAvCzFy2zp81pDNoa4UA.png)<jupyter_code># We have 3 values of theta

# For theta_0:
print( fitted_model.intercept_ )

# For theta_1 and theta_2:
print( fitted_model.coef_ )
# Creating our Logistic Regression Plot
import numpy as np
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="white")

xx, yy = np.mgrid[-5:5:.01, -5:5:.01]
grid = np.c_[xx.ravel(), yy.ravel()]
probs = clf.predict_proba(grid)[:, 1].reshape(xx.shape)

f, ax = plt.subplots(figsize=(8, 6))
contour = ax.contourf(xx, yy, probs, 25, cmap="RdBu",
                      vmin=0, vmax=1)
ax_c = f.colorbar(contour)
ax_c.set_label("$P(y = 1)$")
ax_c.set_ticks([0, .25, .5, .75, 1])

ax.scatter(X_train[100:,0], X_train[100:, 1], c=Y_train[100:], s=50,
           cmap="RdBu", vmin=-.2, vmax=1.2,
           edgecolor="white", linewidth=1)

ax.set(aspect="equal",
       xlim=(-5, 5), ylim=(-5, 5),
       xlabel="$X_1$", ylabel="$X_2$")
f, ax = plt.subplots(figsize=(8, 6))
ax.contour(xx, yy, probs, levels=[.5], cmap="Greys", vmin=0, vmax=.6)

ax.scatter(X_train[100:,0], X_train[100:, 1], c=Y_train[100:], s=50,
           cmap="RdBu", vmin=-.2, vmax=1.2,
           edgecolor="white", linewidth=1)

ax.set(aspect="equal",
       xlim=(-5, 5), ylim=(-5, 5),
       xlabel="$X_1$", ylabel="$X_2$")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16127805249144161, 'fraction_numerical': 0.0395587675922404, 'mean_word_length': 4.78021978021978, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 2, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '95302', 'license': 'MIT', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1065, 'n_words': 229, 'path': 'notebooks/6.1 Logistic Regression.ipynb', 'repo_name': 'sparsh-ai/Data-Science-Analytics-AI-for-Business-the-Real-World-', 'size': '2629'}