Python- ModuleNotFoundError: No module named 'PCM' - python

I am doing a voice detection project on Python3.10. However, I cannot run my code it said: "ModuleNotFoundError: No module named 'PCM" (line 7) I tried to pip install PCM and pip install confusion-matrix again and again but it's still the same. I don't know how to fix it; hopefully, someone can help me. Thank you very much!
Here is my code
####### IMPORTS #############
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, classification_report
from PCM.PCM import plot_confusion_matrix
import pickle
##### Loading saved csv ##############
df = pd.read_pickle("final_audio_data_csv/audio_data.csv")
####### Making our data training-ready
X = df["feature"].values
X = np.concatenate(X, axis=0).reshape(len(X), 40)
y = np.array(df["class_label"].tolist())
####### train test split ############
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
##### Training ############
logit_reg = LogisticRegression(max_iter=10000)
logit_reg.fit(X_train, y_train)
score = logit_reg.score(X_test, y_test)
print("Model Score: \n")
print(score)
#### Evaluating our model ###########
print("Model Classification Report: \n")
y_pred = logit_reg.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
print(classification_report(y_test, y_pred))
plot_confusion_matrix(cm, classes=["Does not have WW", "Has WW"])
#### Save the model
pickle.dump(logit_reg, open('saved_model/WWD_ML.txt', 'wb'))
'''
To load the model again run this:
>>> model = pickle.load(open('saved_model/WWD_ML.txt', 'rb'))
>>> model.predict(<-- your matrix -->) # to predict
'''
here is my terminal
PS C:\Users\adamn\Documents\voice detection\WakeWordDetection-master\WakeWordDetection-master> & C:/Users/adamn/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/adamn/Documents/voice detection/WakeWordDetection-master/WakeWordDetection-master/UsingML.py"
Traceback (most recent call last):
File "c:\Users\adamn\Documents\voice detection\WakeWordDetection-master\WakeWordDetection-master\UsingML.py", line 7, in <module>
from PCM.PCM import plot_confusion_matrix
ModuleNotFoundError: No module named 'PCM'
PS C:\Users\adamn\Documents\voice detection\WakeWordDetection-master\WakeWordDetection-master>

Related

I am having error with svm classifier in python

I am trying to run the following code
# Data Pre-processing Step
# importing libraries
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
# importing datasets
data_set = pd.read_csv('/Users/apple/Desktop/parkinsons.data')
# Extracting Independent and dependent Variable
x = data_set.iloc[:, [2, 3]].values
y = data_set.iloc[:, 4].values
# Splitting the dataset into training and test set.
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=0)
# feature Scaling
from sklearn.preprocessing import StandardScaler
st_x = StandardScaler()
x_train = st_x.fit_transform(x_train)
x_test = st_x.transform(x_test)
print(x_test)
from sklearn.svm import SVC # "Support vector classifier"
classifier = SVC(kernel='linear', random_state=0)
classifier.fit(x_train, y_train)
It has problem with the line "classifier.fit(x_train, y_train)" as follows:-
Traceback (most recent call last):
File "/Users/apple/PycharmProjects/pythonProject4/main.py", line 30, in <module>
classifier.fit(x_train, y_train)
File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.10/site- packages/sklearn/svm/_base.py", line 201, in fit
y = self._validate_targets(y)
File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.10/site- packages/sklearn/svm/_base.py", line 745, in _validate_targets
check_classification_targets(y)
File "/Users/apple/PycharmProjects/pythonProject4/venv/lib/python3.10/site-packages/sklearn/utils/multiclass.py", line 207, in check_classification_targets
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous'
Process finished with exit code 1
What is wrong with my code ? Is it due to the version ?
I am using PyCharm with python 3.10

I cant find why `.read_csv` cannot make a dataframe for `.shape` to recognize

Following a machine learning guide here: https://www.pluralsight.com/guides/scikit-machine-learning/
Running Python 3.8, might have a hunch that I need to run it in IPython but I think that opens up a new can of worms.
Also have all imported these libraries installed.
I left %matplotlib inline as a comment because i'm not running it in Jupyter.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotlib inline
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
df = pd.read_csv("diabetes.csv")
print(pd.shape)
df.describe()
y = df[diabetes.csv].values
x = df.drop('diabetes', axis=1).values
X_train. X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state=42)
X_train.shape, X_test.shape
((460,8), (308,8))
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
y_pred = logreg.predict(X_test)
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
plt.show()
The error I get when running this code is:
Traceback (most recent call last):
File "scikitmlprac.py", line 12, in
print(pd.shape)
File "C:\Python38\lib\site-packages\pandas_init_.py", line 258, in getattr
raise AttributeError(f"module 'pandas' has no attribute '{name}'")
AttributeError: module 'pandas' has no attribute 'shape'
Seems like you just have a typo here.
You've tried to print pd.shape when it should be df.shape. Hence the error.

Google Cloud Platform Kubeflow Pipeline Error

I have a Google Cloud Platform account with a Kubeflow Pipeline. The first component of the pipeline preprocesses some data and the second one trains a model (SKlearn Decision Tree Classifier) with that preprocessed data. For the purpose of showing a code sample, the sample below is a simple modification of the pipeline's second component:
import logging
import pandas as pd
import os
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn import metrics, datasets
from sklearn.model_selection import train_test_split
iris = datasets.load_iris()
X = iris.data
y = iris.target
x_train_data, x_test_data, y_train_data, y_test_data = train_test_split(X, y, test_size=0.3, random_state=1, shuffle=True)
print("Creating model")
model = DecisionTreeClassifier()
print(f"Training model ({type(model)})")
model.fit(x_train_data, y_train_data)
print("Evaluating model")
y_train_pred = model.predict(x_train_data)
print("y_train_pred: ", y_train_pred.shape)
y_test_pred = model.predict(x_test_data)
print("y_test_pred: ", y_test_pred.shape)
train_accuracy = metrics.accuracy_score(y_train_data, y_train_pred)
train_classification_report = metrics.classification_report(y_train_data, y_train_pred)
print("\nTraining result:")
print(f"Accuracy:\t{train_accuracy}")
print(f"Classification report:\t{type(train_classification_report)}\n{train_classification_report}")
test_accuracy = metrics.accuracy_score(y_test_data, y_test_pred)
test_classification_report = metrics.classification_report(y_test_data, y_test_pred)
print("\nTesting result:")
print(f"Accuracy:\t{test_accuracy}")
print(f"Classification report:\t{type(test_classification_report)}\n{test_classification_report}")
print("\nDONE !\n")
Here, instead of loading the preprocessed data, I'm using the IRIS Sklearn datset but the output is exactly the same. Everything seems to work as intended, every print statement appears on the Kubeflow platform output console as expected, however after the second component finishes executing (after the last print is correclty shown on the output console), an error appers:
Traceback (most recent call last):
File "<string>", line 181, in <module>
File "<string>", line 151, in _serialize_str
TypeError: Value "None" has type "<class 'NoneType'>" instead of str.
Do you have any idea why this is happening ?
Am I doing something wrong or is it some Google Cloud / Kubeflow Pipeline problem ?
Thanks in advance!

TypeError: Cannot clone object '<>' (type <class ''>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods

I want to use votingClassifier or EnsembleVoteClassifier voting method with 3 different models but I have this error
I need your help to solve this problem!
import numpy as np
import matplotlib.pyplot as plt
from mlxtend.classifier import EnsembleVoteClassifier
from mlxtend.plotting import plot_decision_regions
# Initializing Classifiers
clf1 = modelvgg16
clf2 = AlexNetModel
clf3 = InceptionV3Model
for model in [clf1, clf2,clf3]:
model._estimator_type = "classifier"
#print(model._estimator_type)
eclf = EnsembleVoteClassifier(clfs=[clf1, clf2,clf3],weights=[2, 1, 1], voting='soft')
X, Y = training_set.next()
Y=np.zeros(X.shape[0]) # number of calsses is 38
print("X.shape =",X.shape) # X.shape = (128, 224, 224, 3)
print("Y.shape =",Y.shape) # Y.shape = (38,)
######################### Split train+test #######################################
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.20,random_state=2)
# Whole Wine Classifier
ensemble_model.fit(x_train, y_train)
y_pred = ensemble_model.predict(x_test)
from sklearn.metrics import accuracy_score
print("accueacy : ",accuracy_score(y_test,y_pred))
for more information see my project on this link:
my project
I got the same error when running this code:

Error with model_evaluation_utils confusion matrix

I am working on this tutorial https://medium.com/#sarahcy/read-this-how-winners-create-life-changing-habits-that-actually-work-atomic-habits-by-james-ac7a3c6df911, I am currently trying to run the model evaluation part
class_labels = list(set(labels))
meu.display_model_performance_metrics(true_labels=y_test, predicted_labels=predictions, classes=class_labels)
I get this error
Prediction Confusion Matrix:
------------------------------
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/blabla/XAI/model_evaluation_utils.py", line 87, in display_model_performance_metrics
classes=classes)
File "/blabla/XAI/model_evaluation_utils.py", line 62, in display_confusion_matrix
labels=level_labels),
TypeError: __new__() got an unexpected keyword argument 'labels'
How can I solve this issue?
BR
Edit:
Here is the code upto the problematic line:
# part1
import pandas as pd
import numpy as np
import model_evaluation_utils as meu
import matplotlib.pyplot as plt
from collections import Counter
import shap
import eli5
import warnings
warnings.filterwarnings('ignore')
plt.style.use('fivethirtyeight')
shap.initjs()
#part 2
data, labels = shap.datasets.adult(display=True)
labels = np.array([int(label) for label in labels])
print(data.shape , labels.shape)
data.head()
#part 3
Counter(labels)
#part 4
cat_cols = data.select_dtypes(['category']).columns
data[cat_cols] = data[cat_cols].apply(lambda x: x.cat.codes)
data.head()
#part 5
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.3, random_state=42)
print(X_train.shape, X_test.shape)
X_train.head(3)
data_disp, labels_disp = shap.datasets.adult(display=True)
X_train_disp, X_test_disp, y_train_disp, y_test_disp = train_test_split(data_disp, labels_disp, test_size=0.3, random_state=42)
print(X_train_disp.shape, X_test_disp.shape)
X_train_disp.head(3)
#part 6
import xgboost as xgb
xgc = xgb.XGBClassifier(n_estimators=500, max_depth=5, base_score=0.5,
objective='binary:logistic', random_state=42)
xgc.fit(X_train, y_train)
#part 7
predictions = xgc.predict(X_test)
predictions[:10]
#part 8
class_labels = list(set(labels))
meu.display_model_performance_metrics(true_labels=y_test, predicted_labels=predictions, classes=class_labels)
remove this function:
def display_confusion_matrix(true_labels, predicted_labels, classes=[1,0]):
total_classes = len(classes)
level_labels = [total_classes*[0], list(range(total_classes))]
cm = metrics.confusion_matrix(y_true=true_labels,
y_pred=predicted_labels, labels=classes)
cm_frame = pd.DataFrame(data=cm,
columns=pd.MultiIndex(levels=[['Predicted:'],
classes],
labels=level_labels),
index=pd.MultiIndex(levels=[['Actual:'], classes],
labels=level_labels))
print(cm_frame)
and it's content
then go the notebook:
at the last line of your code:
meu.display_model_performance_metrics(true_labels=y_test,predicted_labels=predictions, classes=class_labels)
you will notice that the wanted function is "display_model_performance_metrics"
= so we will went back to our model_evaluation_utils
and we will go to the function:
def display_model_performance_metrics(true_labels, predicted_labels, classes=[1,0]):
print('Model Performance metrics:')
print('-'*30)
get_metrics(true_labels=true_labels, predicted_labels=predicted_labels)
print('\nModel Classification report:')
print('-'*30)
display_classification_report(true_labels=true_labels, predicted_labels=predicted_labels,
classes=classes)
print('\nPrediction Confusion Matrix:')
print('-'*30)
display_confusion_matrix(true_labels=true_labels, predicted_labels=predicted_labels,
classes=classes)
and get rid of the last line:
display_confusion_matrix(true_labels=true_labels,predicted_labels=predicted_labels,classes=classes)
to become a little simpler like:
def display_model_performance_metrics(true_labels, predicted_labels, classes=[1,0]):
print('Model Performance metrics:')
print('-'*30)
get_metrics(true_labels=true_labels, predicted_labels=predicted_labels)
print('\nModel Classification report:')
print('-'*30)
display_classification_report(true_labels=true_labels, predicted_labels=predicted_labels,
classes=classes)
print('\nPrediction Confusion Matrix:')
print('-'*30)
and finally if you want to use:
display_confusion_matrix(true_labels=true_labels, predicted_labels=predicted_labels, classes=classes)
you can use it directly in your notebook instead of file.py or python file
that's it:)

Categories

Resources