-1

My problem is that after finishing all of my implementation , i find it hard to do the plotting .

You can say I am just not familiar enough with how it works.

can you please help me with this plotting this implementation, just the linear model? It's an implementation for Logistic Regression .

Beside that please do provide me with some tutorials ,that help me improve my understanding of plotting in general.

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import datasets
import seaborn as sns
import matplotlib.pyplot as plt


def sigmoid(x):
    return 1/(1+np.exp(-x))

class LogisticRegression():

    def __init__(self, lr=0.001, n_iters=1000):
        self.lr = lr
        self.n_iters = n_iters
        self.weights = None
        self.bias = None

    def fit(self, X, y):
        n_samples, n_features = X.shape
        self.weights = np.zeros(n_features)
        self.bias = 0

        for _ in range(self.n_iters):
            linear_pred = np.dot(X, self.weights) + self.bias
            predictions = sigmoid(linear_pred)

            dw = (1/n_samples) * np.dot(X.T, (predictions - y))
            db = (1/n_samples) * np.sum(predictions-y)

            self.weights = self.weights - self.lr*dw
            self.bias = self.bias - self.lr*db


    def predict(self, X):
        linear_pred = np.dot(X, self.weights) + self.bias
        y_pred = sigmoid(linear_pred)
        class_pred = [0 if y<=0.5 else 1 for y in y_pred]
        return class_pred

# training

bc = datasets.load_breast_cancer()
X, y = bc.data, bc.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234)


clf = LogisticRegression(lr=0.01)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)


def accuracy(y_pred, y_test):
    return np.sum(y_pred==y_test)/len(y_test)

acc = accuracy(y_pred, y_test)
print('implementaion runs with an accuracy equals to :',acc)

#plot
  ++++

Walid
  • 43
  • 1
  • 4
  • **Beside that please do provide me with some tutorials** is off-topic, and not allowed. – Trenton McKinney Dec 10 '22 at 16:11
  • It's not clear what you want to plot. Logistic Regression creates a model of existing data, which allows you to get a predicted result for a new input. – Trenton McKinney Dec 10 '22 at 16:12
  • Something like this [answer](https://stackoverflow.com/q/46085762/7758804). However, your `X_train` and `X-test` are mult-dimensional (many features). – Trenton McKinney Dec 10 '22 at 16:53
  • @TrentonMcKinney yeah, indeed not the same case. I want to plot something like the type of answer you give. but I don't know how. – Walid Dec 10 '22 at 19:58
  • You would have to 1) select one feature at a time, or 2) flatten the array. 1) is not likely to be informative of the model as a whole, and 2) will likely just be a mess. See [How to visualize a fitted multiple regression model?](https://stats.stackexchange.com/q/73320/257240) – Trenton McKinney Dec 10 '22 at 20:01

1 Answers1

0

For timeseries plot try::

predictions_df = pd.DataFrame(data=prediction[0:], index = y_test.index)

fig = predictions_df.plot(title=ModelName, xlabel="Dates",figsize=(20,7)).get_figure()

You get a plot like: enter image description here

You can also add ylabel= to plot

Farid
  • 33
  • 9