<jupyter_start><jupyter_text><jupyter_code><jupyter_output><empty_output><jupyter_text>Creating the matrix for control group<jupyter_code>control= [[2, 3, 0], [1, 2, 0], [2, 1, 0]]<jupyter_output><empty_output><jupyter_text>Creating the matrix for disease group<jupyter_code>disease= [[6, 7, 1], [7, 9, 1], [8, 9, 1]]
full= control + disease
full
len(full)
full[0]<jupyter_output><empty_output><jupyter_text>We can create a dictionary object where each key is the class value and then add a list of all the records as the value in the dictionary.<jupyter_code>def separate(data):
	separated = dict()
	for i in range(len(data)):
		vector = data[i]
		class_value = vector[-1]
		if class_value not in separated:
			separated[class_value] = list()
		separated[class_value].append(vector)
	return separated
separated= separate(full)
for key in separated:
	print(key)
	for row in separated[key]:
		print(row)
def mean(numbers):
	return sum(numbers)/float(len(numbers))
def sqrt(number):
  return number ** 0.5
# Calculate the standard deviation of a list of numbers
def stdev(numbers):
	avg = mean(numbers)
	variance = sum([(x-avg)**2 for x in numbers]) / float(len(numbers)-1)
	return sqrt(variance)<jupyter_output><empty_output><jupyter_text>We need two statistics from a given set of data.We’ll see how these statistics are used in the calculation of probabilities in a few steps. The two statistics we require from a given dataset are the mean and the standard deviation (average deviation from the mean).<jupyter_code># Calculate the mean, stdev and count for each column in a dataset
def summarize_dataset(dataset):
	summaries = [(mean(column), stdev(column), len(column)) for column in zip(*dataset)]
	del(summaries[-1])
	return summaries
full
summarize_dataset(full)
def summarize_by_class(dataset):
	separated = separate(dataset)
	summaries = dict()
	for class_value, rows in separated.items():
		summaries[class_value] = summarize_dataset(rows)
	return summaries
summarize_by_class(full)
pi= 3.141592
def exp(x):
  
  return 2.718281 ** x<jupyter_output><empty_output><jupyter_text>Calculating the probability or likelihood of observing a given real-value like X1 is difficult.One way we can do this is to assume that X1 values are drawn from a distribution, such as a bell curve or Gaussian distribution.A Gaussian distribution can be summarized using only two numbers: the mean and the standard deviation. Therefore, with a little math, we can estimate the probability of a given value. This piece of math is called a Gaussian Probability Distribution Function (or Gaussian PDF) and can be calculated as:    f(x) = (1 / sqrt(2 * PI) * sigma) * exp(-((x-mean)^2 / (2 * sigma^2)))Where sigma is the standard deviation for x, mean is the mean for x and PI is the value of pi.<jupyter_code>def calculate_probability(x, mean, stdev):
	exponent = exp(-((x-mean)**2 / (2 * stdev**2 )))
	return (1 / (sqrt(2 * pi) * stdev)) * exponent
print(calculate_probability(1.0, 1.0, 1.0))<jupyter_output>0.3989423219002315<jupyter_text>Now it is time to use the statistics calculated from our training data to calculate probabilities for new data.Probabilities are calculated separately for each class. This means that we first calculate the probability that a new piece of data belongs to the first class, then calculate probabilities that it belongs to the second class, and so on for all the classes.The probability that a piece of data belongs to a class is calculated as follows:    P(class|data) = P(X|class) * P(class)You may note that this is different from the Bayes Theorem described above.The division has been removed to simplify the calculation.This means that the result is no longer strictly a probability of the data belonging to a class. The value is still maximized, meaning that the calculation for the class that results in the largest value is taken as the prediction. This is a common implementation simplification as we are often more interested in the class prediction rather than the probability.<jupyter_code>def calculate_class_probabilities(summaries, row):
	total_rows = sum([summaries[label][0][2] for label in summaries])
	probabilities = dict()
	for class_value, class_summaries in summaries.items():
		probabilities[class_value] = summaries[class_value][0][2]/float(total_rows)
		for i in range(len(class_summaries)):
			mean, stdev, count = class_summaries[i]
			probabilities[class_value] *= calculate_probability(row[i], mean, stdev)
	return probabilities
summaries = summarize_by_class(full)
calculate_class_probabilities(summaries, full[0])<jupyter_output><empty_output><jupyter_text>Thus, function calculate_class_probabilities predicts higher p value for class 0, which is correct.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08794445613296865, 'fraction_numerical': 0.016831474857984433, 'mean_word_length': 5.830459770114943, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '418743', 'license': 'MIT', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1393, 'n_words': 554, 'path': 'Untitled8.ipynb', 'repo_name': 'sangeeta97/Data-Analysis', 'size': '4753'}