<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <H1> Naive Bayesan classifier</H1>
<H2>Bayesan theorem</H2>
We will try to compute the probability of having a type of Strain $P(Y=y)$ given a feature vector X (i.e. a vector containing the Input resistance, sag ratio, etc...). We will use the “naive” assumption of independence between every pair of features.
Given a class variable $Y$ and a dependent feature vector $X_1$ through $X_n$, Bayes’ theorem states the following relationship
Step1:  We use pandas to split up the matrix into the feature vectors we're interested in. We will also to convert textual category data (Strain, Gender) into an ordinal number that we can work with.
Step2:  <H2> Gaussian naive Bayesan classifier </H2>
Step3:  We now test with the classifier with the training data
Step4:  We test with some fictitious data
<ASSISTANT_TASK:>
Python Code:
%pylab inline
import pandas as pd
# first row contains units
df = pd.read_excel(io='../data/Cell_types.xlsx', sheetname='PFC',  skiprows=1)
del df['CellID'] # remove column with cell IDs
df.head() # show first elements
Explanation: <H1> Naive Bayesan classifier</H1>
<H2>Bayesan theorem</H2>
We will try to compute the probability of having a type of Strain $P(Y=y)$ given a feature vector X (i.e. a vector containing the Input resistance, sag ratio, etc...). We will use the “naive” assumption of independence between every pair of features.
Given a class variable $Y$ and a dependent feature vector $X_1$ through $X_n$, Bayes’ theorem states the following relationship:
$$P(Y | X_i) = \frac{P(Y) P(X_i|Y)}{P(X_i)}$$
End of explanation
pd.Categorical(df.Strain).codes # CB57BL is zero, GAD67 is one
df['Gender'] = pd.Categorical(df.Gender).codes
df['Strain'] = pd.Categorical(df.Strain).codes
df.head()
df.shape # as with NumPy
df.iloc[[0]].values[0] # get a row as NumPy array
# create X and Y
Y = df['Strain'].values
del df['Strain'] # remove Strain
X = [ df.iloc[[i]].values[0] for i in range(df.shape[0]) ]
len(X)==len(Y)
X[0]
Explanation: We use pandas to split up the matrix into the feature vectors we're interested in. We will also to convert textual category data (Strain, Gender) into an ordinal number that we can work with.
End of explanation
from sklearn.naive_bayes import GaussianNB
myclassifier = GaussianNB()
myclassifier.fit(X,Y)
df.iloc[[-2]].values # this is a GAD67 mice
df.iloc[[-1]].values # this is a GAD67 mice
Explanation: <H2> Gaussian naive Bayesan classifier </H2>
End of explanation
def predict(idx):
    if myclassifier.predict( X[idx]):
        print('Cell %2d is GAD67  mice'%idx)
    else:
        print('Cell %2d is CB57BL mice'%idx)
# test with training data
for i in range(df.shape[0]):
    predict(i)
Explanation: We now test with the classifier with the training data
End of explanation
d = np.array([[ -75.50, 100.25, 1.49, 24.75, 90. ,21.5, 24.5 ,60, 1, 85.95, -48.6,
              430.95, 1.5, 385.55]])
test_df = pd.DataFrame(d, columns=df.columns)
test_df
if myclassifier.predict( test_df.iloc[[0]].values[0]):
    print('Test is GAD67  mice')
else:
    print('Test is CB57BL mice')
Explanation: We test with some fictitious data
End of explanation
<END_TASK>