When running the following code, I ran into the problem: All arrays must be of same length. I am trying to generate a pandas dataframe so that I can plot it using seaborn lmplot, but it's not working.
I tried this:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#file address
TOKEN_SPAM_PROB_FILE="SpamData/03_Testing/prob-spam.txt"
TOKEN_NONSPAM_PROB_FILE="SpamData/03_Testing/prob-nonspam.txt"
TOKEN_ALL_PROB_FILE="SpamData/03_Testing/prob-all-tokens.txt"
TEST_FEATURE_MATRIX="SpamData/03_Testing/test-features.txt"
TEST_TARGET_FILE="SpamData/03_Testing/test-target.txt"
VOCAB_SIZE=2500
#features
x_test=np.loadtxt(TEST_FEATURE_MATRIX, delimiter=" ")
#target
y_test=np.loadtxt(TEST_TARGET_FILE, delimiter=" ")
#token probabilitis
prob_token_spam=np.loadtxt(TOKEN_SPAM_PROB_FILE, delimiter=" ")
prob_token_nonspam=np.loadtxt(TOKEN_NONSPAM_PROB_FILE, delimiter=" ")
prob_all_token=np.loadtxt(TOKEN_ALL_PROB_FILE, delimiter=" ")
PROB_SPAM=0.3116
joint_log_spam=x_test.dot(np.log(prob_token_spam) - np.log(prob_all_token)) + np.log(PROB_SPAM)
joint_log_nonspam=x_test.dot(np.log(prob_token_nonspam) - np.log(prob_all_token)) + np.log(1-PROB_SPAM)
prediction=joint_log_spam > joint_log_nonspam
#simplification
joint_log_spam=x_test.dot(np.log(prob_token_spam)) + np.log(PROB_SPAM)
joint_log_nonspam=x_test.dot(np.log(prob_token_nonspam)) + np.log(1-PROB_SPAM)
correct_doc=[np.where(y_test==x)[0][0] for x in prediction]
# print(correct_doc)
total=0
for i in correct_doc:
if i!=0:
total+=1
# np.digitize(y_test, prediction)
print(total)
correct_doc_total=total
correct_docs=correct_doc_total
print("Docs Classified correctly are:", correct_docs)
numbdocs_wrong=x_test.shape[0]-correct_docs
print("Docs classified incorrectly are:", numbdocs_wrong)
fraction_wrong = numbdocs_wrong/len(x_test)
print('Fraction classified incorrectly is {:.2%}'.format(fraction_wrong))
print('Accuracy of the model is {:.2%}'.format(1-fraction_wrong))
#Data Visualisation
yaxis_label = 'P(X | Spam)'
xaxis_label = 'P(X | Nonspam)'
linedata = np.linspace(start=-14000, stop=1, num=1000)
print("The shape of joint_log_spam is:", joint_log_spam.shape)
print("The shape of joint_log_nonspam is:", joint_log_nonspam.shape)
print("The shape of x_test is:", x_test.shape)
# Chart Styling
sns.set_style('whitegrid')
labels = 'Actual Category'
summary_df = pd.DataFrame({yaxis_label:joint_log_spam, xaxis_label:joint_log_nonspam, labels:y_test})
sns.lmplot(x=joint_log_nonspam, y=joint_log_spam, data=summary_df, size=6.5, fit_reg=False,
scatter_kws={'alpha': 0.5, 's': 25})
plt.xlim([-2000, 1])
plt.ylim([-2000, 1])
plt.plot(linedata, linedata, color='black')
sns.plt.show()
The link of the path folder is given below:
https://drive.google.com/drive/folders/15M7-VcUZw7gkLWxlJ8MDKLm6muYIREoT?usp=sharing
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In [23], line 1
----> 1 summary_df = pd.DataFrame({yaxis_label: joint_log_spam, xaxis_label: joint_log_nonspam, labels: y_test})
File ~\anaconda3\envs\py11\Lib\site-packages\pandas\core\frame.py:664, in DataFrame.__init__(self, data, index, columns, dtype, copy)
658 mgr = self._init_mgr(
659 data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy
660 )
662 elif isinstance(data, dict):
663 # GH#38939 de facto copy defaults to False only in non-dict cases
--> 664 mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
665 elif isinstance(data, ma.MaskedArray):
666 import numpy.ma.mrecords as mrecords
File ~\anaconda3\envs\py11\Lib\site-packages\pandas\core\internals\construction.py:493, in dict_to_mgr(data, index, columns, dtype, typ, copy)
489 else:
490 # dtype check to exclude e.g. range objects, scalars
491 arrays = [x.copy() if hasattr(x, "dtype") else x for x in arrays]
--> 493 return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
File ~\anaconda3\envs\py11\Lib\site-packages\pandas\core\internals\construction.py:118, in arrays_to_mgr(arrays, columns, index, dtype, verify_integrity, typ, consolidate)
115 if verify_integrity:
116 # figure out the index, if necessary
117 if index is None:
--> 118 index = _extract_index(arrays)
119 else:
120 index = ensure_index(index)
File ~\anaconda3\envs\py11\Lib\site-packages\pandas\core\internals\construction.py:666, in _extract_index(data)
664 lengths = list(set(raw_lengths))
665 if len(lengths) > 1:
--> 666 raise ValueError("All arrays must be of the same length")
668 if have_dicts:
669 raise ValueError(
670 "Mixing dicts with non-Series may lead to ambiguous ordering."
671 )
ValueError: All arrays must be of the same length