You are on page 1of 6

In [24]: import numpy as np

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

In [27]: import pandas as pd



# Read the CSV file into a DataFrame
data_df = pd.read_csv('data.csv')

# Now, you can work with the data using pandas
print(data_df)


Color Type Origin Stolen


0 red sports domestic yes
1 red sports domestic no
2 red sports domestic yes
3 yellow sports domestic no
4 yellow sports imported yes
5 yellow suv imported no
6 yellow suv imported yes
7 yellow suv domestic no
8 red suv imported no
9 red sports imported yes
In [28]: import pandas as pd

# Load the data from the CSV file
data = pd.read_csv('data.csv')

Color = data.groupby(['Color', 'Stolen']).size()
Type = data.groupby(['Type', 'Stolen']).size()
Origin = data.groupby(['Origin', 'Stolen']).size()
Stolen = data.Stolen.value_counts()

print(Color)
print('------------------')
print(Type)
print('------------------')
print(Origin)
print('------------------')
print('Stolen')
print(Stolen)

Color Stolen
red no 2
yes 3
yellow no 3
yes 2
dtype: int64
------------------
Type Stolen
sports no 2
yes 4
suv no 3
yes 1
dtype: int64
------------------
Origin Stolen
domestic no 3
yes 2
imported no 2
yes 3
dtype: int64
------------------
Stolen
Stolen
yes 5
no 5
Name: count, dtype: int64
In [51]: import pandas as pd

# Load the data from the CSV file
data = pd.read_csv('data.csv')

def bayestheorem():
print('Posterior [P(c|x)] - Posterior probability of the target/class (c
print('Prior [P(c)] - Prior probability of the class (target)'),
print('Likelihood [P(x|c)] - Probability of the predictor (x) given the
print('Evidence [P(x)] - Prior probability of the predictor (x))')

ct = pd.crosstab(data['Color'], data['Stolen'], margins = True)
print(ct)

Stolen no yes All


Color
red 2 3 5
yellow 3 2 5
All 5 5 10

In [54]: import pandas as pd



# Load the data from the CSV file
data = pd.read_csv('data.csv')

def bayesposterior(prior, likelihood, evidence, string):


print('Prior=', prior),
print('Likelihood=', likelihood),
print('Evidence=', evidence),
print('Equation =','(Prior*Likelihood)/Evidence')
print(string, (prior*likelihood)/evidence)

ct = pd.crosstab(data['Color'], data['Stolen'], margins = True)
print(ct)

Stolen no yes All


Color
red 2 3 5
yellow 3 2 5
All 5 5 10
In [55]: import pandas as pd

data = pd.read_csv('data.csv')

ct = pd.crosstab(data['Color'], data['Stolen'], margins = True)
print(ct)

ct.columns = ["no","yes","rowtotal"]
ct.index= ["red","yellow","coltotal"]
ct / ct.loc["coltotal","rowtotal"]

ct / ct.loc["coltotal"]

ct.div(ct["rowtotal"], axis=0)

Stolen no yes All


Color
red 2 3 5
yellow 3 2 5
All 5 5 10

Out[55]: no yes rowtotal

red 0.4 0.6 1.0

yellow 0.6 0.4 1.0

coltotal 0.5 0.5 1.0

In [39]: import pandas as pd



# Load the data from the CSV file
data = pd.read_csv('data.csv')

pd.crosstab(data['Color'], data['Stolen'], margins = True)

Out[39]: Stolen no yes All

Color

red 2 3 5

yellow 3 2 5

All 5 5 10
In [38]: import pandas as pd

# Load the data from the CSV file
data = pd.read_csv('data.csv')

pd.crosstab(data['Type'], data['Stolen'], margins = True)

Out[38]: Stolen no yes All

Type

sports 2 4 6

suv 3 1 4

All 5 5 10

In [41]: import pandas as pd



# Load the data from the CSV file
data = pd.read_csv('data.csv')

pd.crosstab(data['Origin'], data['Stolen'], margins = True)

Out[41]: Stolen no yes All

Origin

domestic 3 2 5

imported 2 3 5

All 5 5 10

In [40]: import pandas as pd



# Load the data from the CSV file
data = pd.read_csv('data.csv')

pd.crosstab(index=data['Stolen'],columns="count", margins=True)

Out[40]: col_0 count All

Stolen

no 5 5

yes 5 5

All 10 10

In [1]: import pandas as pd



# Load the data from the CSV file
data = pd.read_csv('data.csv')

p_x_yes = ((3/5)*(1/5)*(2/5))
print('The probability of the predictors given stolen car is', '%.3f'%p_x_ye

The probability of the predictors given stolen car is 0.048


In [2]: import pandas as pd

# Load the data from the CSV file
data = pd.read_csv('data.csv')

p_x_no = ((2/5)*(3/5)*(3/5))
print('The probability of the predictors given not stolen car is ', '%.3f'%p

The probability of the predictors given not stolen car is 0.144

In [4]: import pandas as pd



# Load the data from the CSV file
data = pd.read_csv('data.csv')

yes = (5/10)
no = (5/10)
print('The probability of car is stolen ', '%.3f'% yes)
print('The probability of car is not stolen', '%.3f'% no)

The probability of car is stolen 0.500


The probability of car is not stolen 0.500

In [5]: import pandas as pd



# Load the data from the CSV file
data = pd.read_csv('data.csv')

yes_x = p_x_yes*yes
print('The probability of stolen car given predictors is', '%.3f'%yes_x)

no_x = p_x_no*no
print('The probability of car is not stolen given predictors is', '%.3f'%no_

if yes_x > no_x:
print('"Car is stolen when the color is red, the type is SUV, the origin i
else:
print('"Car is not stolen when the color is red, the type is SUV, the orig

The probability of stolen car given predictors is 0.024


The probability of car is not stolen given predictors is 0.072
"Car is not stolen when the color is red, the type is SUV, the origin is d
omestic"

You might also like