Simple logistic regression with Statsmodels: Adding an intercept and visualizing the logistic regression equation - data-visualization

Using Statsmodels, I am trying to generate a simple logistic regression model to predict whether a person smokes or not (Smoke) based on their height (Hgt).
I have a feeling that an intercept needs to be included into the logistic regression model but I am not sure how to implement one using the add_constant() function. Also, I am unsure why the error below is generated.
This is the dataset, Pulse.CSV: https://drive.google.com/file/d/1FdUK9p4Dub4NXsc-zHrYI-AGEEBkX98V/view?usp=sharing
The full code and output are in this PDF file: https://drive.google.com/file/d/1kHlrAjiU7QvFXF2a7tlTSFPgfpq9bOXJ/view?usp=sharing
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
raw_data = pd.read_csv('Pulse.csv')
raw_data
x1 = raw_data['Hgt']
y = raw_data['Smoke']
reg_log = sm.Logit(y,x1,missing='Drop')
results_log = reg_log.fit()
def f(x,b0,b1):
return np.array(np.exp(b0+x*b1) / (1 + np.exp(b0+x*b1)))
f_sorted = np.sort(f(x1,results_log.params[0],results_log.params[1]))
x_sorted = np.sort(np.array(x1))
plt.scatter(x1,y,color='C0')
plt.xlabel('Hgt', fontsize = 20)
plt.ylabel('Smoked', fontsize = 20)
plt.plot(x_sorted,f_sorted,color='C8')
plt.show()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
4729 try:
-> 4730 return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
4731 except KeyError as e1:
((( Truncated for brevity )))
IndexError: index out of bounds

Intercept is not added by default in Statsmodels regression, but if you need you can include it manually.
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
raw_data = pd.read_csv('Pulse.csv')
raw_data
x1 = raw_data['Hgt']
y = raw_data['Smoke']
x1 = sm.add_constant(x1)
reg_log = sm.Logit(y,x1,missing='Drop')
results_log = reg_log.fit()
results_log.summary()
def f(x,b0,b1):
return np.array(np.exp(b0+x*b1) / (1 + np.exp(b0+x*b1)))
f_sorted = np.sort(f(x1,results_log.params[0],results_log.params[1]))
x_sorted = np.sort(np.array(x1))
plt.scatter(x1['Hgt'],y,color='C0')
plt.xlabel('Hgt', fontsize = 20)
plt.ylabel('Smoked', fontsize = 20)
plt.plot(x_sorted,f_sorted,color='C8')
plt.show()
This will also resolve the error as there was no intercept in your initial code.Source

Related

Curve fitting exponential function with semilog x-axis

I'm having trouble fitting some date onto an exponential function with a semilog x-axis.
Following is the code:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
kd=np.array[0.735420099, 0.700823723, 0.647775947,0.613179572,0.573970346,0.54398682,0.454036244,0.371004942,0.292586491,0.271828666,0.21878089,0.165733114,0.157660626,0.151894563]
ADAR = np.array[0.001012268,0.002028379,0.004015198,0.005931555,0.007948127,0.010143277,0.019594977,0.039746044,0.076782168,0.101639121,0.193968714,0.574178304,0.778822803,0.9878803]
def func(x,a,c,d):
return a*np.exp(-c*x)+d
init_v = (1,1e-6,0)
opt,pcov = curve_fit(func,ADAR,kd,init_v)
a,c,d = opt
x2 = np.linspace(0.001,1)
y2 = func(x2,a,c,d)
plt.grid(True, which = "both")
fig = plt.figure()
ax = plt.gca()
ax.scatter(ADAR,kd, c = 'blue')
ax.set_xscale('log')
plt.xlim([0.001,1])
plt.ylim([0,0.8])
plt.plot(x2,y2, '-', label = 'fit')
plt.legend()
plt.title('Area pressure coefficient')
plt.xlabel('AD/AR')
plt.ylabel('kd')
plt.show
Trying to fit the scatter plot:
Using Scipy Curve_fit with initial guesses I am unable to get a close fit of the data. Am I using the wrong function for this?

How to plot pointcloud2 in matplotlib

I have a sensor_msgs/PointCloud2 with [x,y,z] and how can I plot it in real-time in matplotlib like this code here. I already changed the type from Odometry to pointcloud2 but I don't know what to change in odom_callback or how to change the code in order to plot it in matplotlib. Can someone has an idea how to plot pointcloud2 in matplotlib
import matplotlib.pyplot as plt
import rospy
import tf
from sensor_msgs.msg import PointCloud2
from tf.transformations import quaternion_matrix
import numpy as np
from matplotlib.animation import FuncAnimation
class Visualiser:
def __init__(self):
self.fig, self.ax = plt.subplots()
self.ln, = plt.plot([], [], 'ro')
self.x_data, self.y_data = [] , []
def plot_init(self):
self.ax.set_xlim(0, 10000)
self.ax.set_ylim(-7, 7)
return self.ln
def getYaw(self, pose):
quaternion = (pose.orientation.x, pose.orientation.y, pose.orientation.z,
pose.orientation.w)
euler = tf.transformations.euler_from_quaternion(quaternion)
yaw = euler[2]
return yaw
def odom_callback(self, msg):
yaw_angle = self.getYaw(msg.pose.pose)
self.y_data.append(yaw_angle)
x_index = len(self.x_data)
self.x_data.append(x_index+1)
def update_plot(self, frame):
self.ln.set_data(self.x_data, self.y_data)
return self.ln
rospy.init_node('publisher_node')
vis = Visualiser()
sub = rospy.Subscriber('/scan3dd', PointCloud2, vis.odom_callback)
ani = FuncAnimation(vis.fig, vis.update_plot, init_func=vis.plot_init)
plt.show(block=True)

Matplotlib scalarformatter not converting y-ticks to standard form

I am trying to use ScalarFormatter to convert my y-axis ticks into standard form, but after much experimenting I'm experiencing no success and I can't figure out why. MWE:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import scipy.constants as const
k = 1/(4*np.pi*const.epsilon_0)
def force_e(q_1,q_2,x):
return k*q_1*q_2/x**2
d = 10
delta = 0.1*d
x = np.linspace(0+delta,d-delta,1000)
q1 = 2e-6
q2 = -2e-6
q = 1e-9
F = force_e(q1,q,x) - force_e(q2,q,d-x)
fig,ax1 = plt.subplots(1, figsize=(6,6))
ax1.plot(x, F)
ax1.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter(useMathText=True))
ax1.set_xlabel("Separation $r$ (m)")
ax1.set_ylabel("Force $F$ (N)")
plt.tight_layout()
plt.show()
It produces a figure as so:
where as I would like it to display 1.75x10^5 etc.

Array is not iterable, but how to use as argument in zip()?

I want to plot 3D visualizations of DICOM Scans, but I am stuck on this error.
I am using the marching cubes method. First from mesh verts and faces and returned then passed into plt_3d. Modules imported:
import numpy as np
import pydicom as pyd
import os
import matplotlib.pyplot as plt
from glob import glob
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import scipy.ndimage
from skimage import morphology
from skimage import measure
from skimage.transform import resize
from sklearn.cluster import KMeans
from plotly import version
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.tools import FigureFactory as FF
from plotly.graph_objs import *
init_notebook_mode(connected=True)
imgs_after_resamp is the pixel array(3d) which contains the DICOM Data
def make_mesh(image, threshold=-300, step_size=1):
p = image.transpose(2,1,0)
verts, faces, norm, val = measure.marching_cubes_lewiner(p, threshold,
step_size=step_size, allow_degenerate=True)
print(verts)
return verts, faces
def plotly_3d(verts, faces):
x,y,z = zip(*verts)
fig = FF.create_trisurf(x=x,
y=y,
z=z,
plot_edges=False,
colormap=colormap,
simplices=faces,
backgroundcolor='rgb(64, 64, 64)',
title="Interactive Visualization")
iplot(fig)
def plt_3d(verts, faces):
print(“Drawing”)
x,y,z = zip(*verts)
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
mesh = Poly3DCollection(verts[faces], linewidths=0.05, alpha=1)
face_color = [1, 1, 0.9]
mesh.set_facecolor(face_color)
ax.add_collection3d(mesh)
ax.set_xlim(0, max(x))
ax.set_ylim(0, max(y))
ax.set_zlim(0, max(z))
ax.set_axis_bgcolor((0.7, 0.7, 0.7))
plt.show()
v, f = make_mesh(imgs_after_resamp, 350)
new3d=np.vectorize(plt_3d)
new3d(v,f)
ValueError Traceback (most recent call last)
in
----> 1 plot_ve(imgs_re,400)
TypeError: type object argument after * must be an iterable, not numpy.float32

How can I convert Arduino signal from Python to Fast Fourier transform?

I'm now trying to convert the signal into a Fast Fourier transform in Python and draw a graph. I have a problem with Len here. How can I fix this? And does anyone have any other ideas about converting Fast Fourier transform?
Exception has occurred: TypeError
object of type 'method' has no len()
That is my problem.
from PyQt5.QtWidgets import*
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import random
from PyQt5 import QtCore, QtGui, QtWidgets
import datetime
import serial
import time
import random
import numpy as np
from matplotlib import animation
from collections import deque
import threading
x = 0
value = [0]
ser = serial.Serial('com5', 9600)
class scope :
def data(self) :
if ser.readable() :
time.sleep(0.01)
reciving = ser.readline(ser.inWaiting())
str = reciving.decode()
if len(str) > 0 :
if str[:1] == 'X' :
value[0] = str[1:]
#print(float(value[5]))
time.sleep(0.5)
x = float(value[0])
return x
s = scope()
n = len(s.data)
Ts = 0.01
Fs = 1/Ts
# length of the signal
k = np.arange(n)
T = n/Fs
freq = k/T # two sides frequency range
freq = freq[range(int(n/2))] # one side frequency range
Y = np.fft.fft(x)/n # fft computing and normalization
Y = Y[range(int(n/2))]
fig, ax = plt.subplots(2, 1)
ax.plot(freq, abs(Y), 'r', linestyle=' ', marker='^')
ax.set_xlabel('Freq (Hz)')
ax.set_ylabel('|Y(freq)|')
#3ax.vlines(freq, [0], abs(Y))
ax.grid(True)
t = threading.Thread(target= s.data)
t.daemon = True
t.start()
plt.show()