Logistic Regression

This is logistic regression from scratch using python.

x = [0, 1, 2, 3, 5, 6, 7, 8] y = [0, 0, 0, 0, 1, 1, 1, 1] import numpy as np import pandas as pd import matplotlib.pyplot as plt plt.plot(x, y) plt.savefig('plot.png') def slopeDerivative(x, y, slope, yInt): length = len(x) total = 0 for i in range(length): predicted = (slope * x[i]) + yInt difference = (y[i] - predicted) * x[i] total += difference returnValue = (-2/length) * total return returnValue def interceptDerivative(x, y, slope, yInt): length = len(x) total = 0 for i in range(length): predicted = (slope * x[i]) + yInt difference = (y[i] - predicted) total += difference returnValue = (-2/length) * total return returnValue m = 0 c = 0 l = 0.0001 iterations = 100000 for i in range(iterations): derivativeSlope = slopeDerivative(x, y, m ,c) derivativeIntercept = interceptDerivative(x, y, m ,c) m = m - (l * derivativeSlope) print (m) c = c - (l * derivativeIntercept) m = round(m, 3) c = round(c, 3) print ("The slope is " + str(m) + " and the y - intercept = " + str(c)) newY = [] biggest = [] howBigIsTheTumor = float(input("How big is the tumor in cm? ")) userPrediction = 1 / (1 + (2.71828 ** (-1 * ((howBigIsTheTumor*m)+c)))) print ("There is a " + str(userPrediction) + " that the tumor is malignant!") for f in range(0, 8): xValues = [] recentValue = f for i in range(10000): xValues.append(recentValue) recentValue += 0.0001 for i in range(len(xValues)): yprime = 1 / (1 + (2.71828 ** (-1 * ((xValues[i]*m)+c)))) newY.append(yprime) for n in range(len(xValues)): biggest.append(xValues[n]) plt.plot(biggest, newY) plt.savefig("graph")

Naive Bayes Classifier

This is an example of the naive Bayes classifier. In this code, I used old data to determine whether it was a good day to golf.


import math from statistics import mean # here we will find the answer to playing assuming the outlook is sunny, the temperature is cool, the humidity is high, and the wind is strong outlook = ["sunny", "sunny", "overcast", "rainy", "rainy", "rainy", "overcast", "sunny", "sunny", "rainy", "sunny", "overcast", "overcast", "rainy"] temperature = ["hot", "hot", "hot", "mild", "cool", "cool", "cool", "mild", "cool", "mild", "mild", "mild", "hot", "mild"] humidity = ["high", "high", "high", "high", "normal", "normal","normal", "high", "normal", "normal", "normal", "high", "normal", "high"] windy = ["false", "true", "false", "false", "false", "true", "true", "false", "false", "false", "true", "true", "false", "true"] play = ["no", "no", "yes", "yes", "yes", "no", "yes", "no", "yes", "yes", "yes", "yes", "yes", "no"] #print (len(temperature)) #print (len(outlook)) #print (len(humidity)) #print (len(windy)) #print (len(play)) sunny = 0 cool = 0 high = 0 strong = 0 for i in range(14): if outlook[i] == "sunny": sunny+= 1 if temperature[i] == "cool": cool += 1 if humidity[i] == "high": high+= 1 if windy[i] == "true": strong+= 1 print (sunny) print (cool) print (high) print (strong) countYes = 0 countNo = 0 length = len(play) for i in range(length): if play[i] == "no": countNo += 1 else: countYes += 1 print (countNo) print (countYes) probYesWindy = 0 probNoWindy = 0 probYesNoWindy = 0 probNoNoWindy = 0 probYes = countYes / 14 probNo = countNo / 14 for i in range(length): if play[i] == "yes": if windy[i] == "false": probYesNoWindy += 1 if windy[i] == "true": probYesWindy += 1 if play[i] == "no": if windy[i] == "false": probNoNoWindy += 1 if windy[i] == "true": probNoWindy += 1 YesWindy = probYesWindy/countYes NoWindy = probNoWindy / countNo YesNoWindy = probYesNoWindy/countYes NoNoWindy = probNoNoWindy / countNo print (YesWindy) print (NoWindy) probYesHigh = 0 probNoHigh = 0 probYesNormal = 0 probNoNormal = 0 for i in range(length): if play[i] == "yes": if humidity[i] == "high": probYesHigh += 1 if humidity[i] == "normal": probYesNormal += 1 if play[i] == "no": if humidity[i] == "high": probNoHigh += 1 if humidity[i] == "normal": probNoNormal += 1 yesHigh = probYesHigh/countYes noHigh = probNoHigh / countNo yesNormal = probYesNormal/countYes noNormal = probNoNormal / countNo print (yesHigh) print (noHigh) probYesSunny = 0 probNoSunny = 0 probYesOvercast = 0 probNoOvercast = 0 probYesRainy = 0 probNoRainy = 0 for i in range(length): if play[i] == "yes": if outlook[i] == "sunny": probYesSunny += 1 if outlook[i] == "overcast": probYesOvercast += 1 if outlook[i] == "rainy": probYesRainy += 1 if play[i] == "no": if outlook[i] == "sunny": probNoSunny += 1 if outlook[i] == "overcast": probNoOvercast += 1 if outlook[i] == "rainy": probNoRainy += 1 YesSunny = probYesSunny/countYes NoSunny = probNoSunny / countNo YesOvercast = probYesOvercast/countYes NoOvercast = probNoOvercast / countNo YesRainy = probYesRainy / countYes NoRainy = probNoRainy / countNo print (YesSunny) print (NoSunny) probYesHot = 0 probNoHot = 0 probYesMild = 0 probNoMild = 0 probYesCool = 0 probNoCool = 0 for i in range(length): if play[i] == "yes": if temperature[i] == "hot": probYesHot += 1 if temperature[i] == "mild": probYesMild += 1 if temperature[i] == "cool": probYesCool += 1 if play[i] == "no": if temperature[i] == "hot": probNoHot += 1 if temperature[i] == "mild": probNoMild += 1 if temperature[i] == "cool": probNoCool += 1 YesHot = probYesHot/countYes NoHot = probNoHot / countNo YesMild = probYesMild /countYes NoMild = probNoMild / countNo YesCool = probYesCool / countYes NoCool = probNoCool / countNo print (YesCool) print (NoCool) pYes = (YesSunny) * (YesCool) * (yesHigh) * (YesWindy) * (probYes) pNo = (NoSunny) * (NoCool) * (noHigh) * (NoWindy) * (probNo) print (pYes) print (pNo) commonP = (sunny/14) * (high / 14) * (cool /14) * (strong/ 14) finalYes= pYes / commonP finalNo = pNo / commonP if finalYes > finalNo: print ("Go out and play. The chances of you playing are " + str(finalYes) + " and the chances you don't are " + str(finalNo)) else: print ("Do not go out and play. The chances of you playing are " + str(finalYes) + " and the chances you don't are " + str(finalNo))
1