预测 = model.predict(email_features_array)

问题描述 投票:0回答:1
  prediction = model.predict(email_features_array)
                 ^^^^^^^^^^^^^
AttributeError: 'numpy.ndarray' object has no attribute 'predict'

我已经构建并训练了 ML 模型,我只想通过 Flask 使用它,但每当它要进入模式时。预测它都会给我这个问题

这是我的代码:

这些是我的进口品

import pickle
import re
import string
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
import numpy as np  # Import NumPy

加载预训练模型


model = pickle.load(open('c:/Users/7rbe2/OneDrive/سطح المكتب/Main projects/Grad project/Phishward/PhishWarden/app/Python/logistic_regression_model.pkl', 'rb'))

# Initialize CountVectorizer
cv = CountVectorizer()

这里有清洁功能

def remove_special_characters(word):
    return word.translate(str.maketrans('', '', string.punctuation))

def remove_stop_words(words):
    stop_words = set(stopwords.words('english'))
    return [word for word in words if word not in stop_words]

def remove_hyperlink(word):
    return re.sub(r"http\S+", "", word)

def fit_count_vectorizer(text):
    # Clean and tokenize the text
    cleaned_text = remove_special_characters(text)
    cleaned_text = remove_hyperlink(cleaned_text)
    tokens = word_tokenize(cleaned_text)
    tokens = remove_stop_words(tokens)
    cleaned_text = ' '.join(tokens)
    return cleaned_text  # Return the cleaned text

这是问题发生的预测

def predict():
    email_text ="hello world"
    cleaned_text = fit_count_vectorizer(email_text)
    cv.fit([cleaned_text])  # Fit CountVectorizer on the cleaned text
    email_features_array = cv.transform([cleaned_text])  # Use transform instead of fit_transform
    # Assume `model` is already defined and trained
    prediction = model.predict(email_features_array)

    # Apply the pre-trained model to predict the probability of the email being phishing
    probability = model.predict_proba(email_features_array)
    if prediction[0] == 1:
        result = 'Phishing'
        probability_score = probability[0][1] * 100
        print(result, probability_score)
    else:
        result = 'Legitimate'
        probability_score = probability[0][0] * 100
        print(result, probability_score)


# Example usage
predict()

我检查了我的 ML 模型的类型并得到了这个:

<class 'numpy.ndarray'>

,我不知道我能做什么来解决这个问题,我几乎尝试了所有可能的方法

python machine-learning flask model artificial-intelligence
1个回答
0
投票

model
变量未在代码中的任何位置定义,并且肯定不在调用它的
predict()
函数中。

其实有一条评论:

# Assume `model` is already defined and trained

大概它在全球范围内(不良做法),但在问题中被省略。

所以看起来应该是:

  1. predict()
    函数内创建。 或
  2. 作为参数传递到同一函数中。

顺便说一句,代码还缺少

cv
,人们可能会认为它来自
opencv
模块。无论如何,如果没有定义这个代码,代码将无法工作......

© www.soinside.com 2019 - 2024. All rights reserved.