简体   繁体   English

RuntimeWarning:exp 预测中遇到溢出 = 1 / (1 + np.exp(-predictions))

[英]RuntimeWarning: overflow encountered in exp predictions = 1 / (1 + np.exp(-predictions))

this is the code I'm trying to implement for the dataset file and as I mentioned before the result just gives a 0 and the error:这是我试图为数据集文件实现的代码,正如我之前提到的,结果只给出了 0 和错误:

RuntimeWarning: overflow encountered in exp predictions = 1 / (1 + np.exp(-predictions)) RuntimeWarning:exp 预测中遇到溢出 = 1 / (1 + np.exp(-predictions))

I tried many solutions for other codes related with this prediction but still the same我为与此预测相关的其他代码尝试了许多解决方案,但仍然相同

`import numpy as np
import pandas as pd

dataset = pd.read_csv('data.csv')

dataset = (dataset - dataset.mean()) / dataset.std()

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(dataset.iloc[:, :-1], dataset.iloc[:, -1], test_size=0.25, random_state=42)

def logisticRegression_model(X, y, learning_rate, num_epochs):
    
    weights = np.zeros(X.shape[1])
    
    for epoch in range(num_epochs):
        
        logisticRegression_update_weights(X, y, weights, learning_rate)
    
    return weights

def logisticRegression_update_weights(X, y, weights, learning_rate):
    
    gradient = logisticRegression_calculate_gradient(X, y, weights)
    
    weights += learning_rate * gradient
    
    return weights

def logisticRegression_calculate_gradient(X, y, weights):
    
    #calculating the predictions
    predictions = logisticRegression_predict(X, weights)
    
    #calculating the errors
    error = y - predictions
    
    gradient = np.dot(X.T, error)

    return gradient

def logisticRegression_predict(X, weights):
    
    predictions = np.dot(X, weights)
    
    predictions = 1 / (1 + np.exp(-predictions))
    
    return predictions

def logisticRegression_accuracy(y_true, y_pred):
    
    accuracy = np.sum(y_true == y_pred) / len(y_true)
    
    return accuracy

def logisticRegression_train(X_train, y_train, learning_rate, num_epochs):
    
    weights = logisticRegression_model(X_train, y_train, learning_rate, num_epochs)
    
    return weights

weights = logisticRegression_train(X_train, y_train, 0.1, 1000)

y_pred_train = logisticRegression_predict(X_train, weights)
y_pred_test = logisticRegression_predict(X_test, weights)

y_pred_train = (y_pred_train > 0.5).astype(int)
y_pred_test = (y_pred_test > 0.5).astype(int)

acc_train = logisticRegression_accuracy(y_train, y_pred_train)
acc_test = logisticRegression_accuracy(y_test, y_pred_test)

print('Train accuracy:', acc_train)
print('Test accuracy:', acc_test)`

This warning occurs because the exponential function exceeds the maximum value accepted for Floating Point (FP) numbers.出现此警告是因为指数 function 超过了浮点 (FP) 数可接受的最大值。 FP numbers have a limited number of bits to store their exponent in scientific notation, so they can eventually overflow. FP 数字在科学记数法中存储指数的位数有限,因此它们最终可能会溢出。

This warning is relatively common, and it has no serious consequences (numpy is smart enough to handle the situation, and realize if the number actually corresponds to inf , nan , 0 , etc.).这个警告比较常见,而且没有严重后果(numpy 足够聪明来处理这种情况,并意识到数字是否实际对应于infnan0等)。

You can even supress the warning message as follows:您甚至可以按如下方式抑制警告消息:

import numpy as np
import warnings
warnings.filterwarnings('ignore')
print(1/np.exp(999999999999))

https://www.statology.org/runtimewarning-overflow-encountered-in-exp/#:~:text=This%20warning%20occurs%20when%20you,provides%20the%20warning%20by%20default . https://www.statology.org/runtimewarning-overflow-encountered-in-exp/#:~:text=This%20warning%20occurs%20when%20you,provides%20the%20warning%20by%20default

Unfortunately, the issue in the OP code is related to another problem (that is not giving the right result).不幸的是,OP 代码中的问题与另一个问题有关(即没有给出正确的结果)。


PS.附言。 If you wrote a code where warnings should not occur at all (because they are related to numerical issues, bugs, etc), you can also transform all numpy warnings into system errors:如果您编写的代码根本不应出现警告(因为它们与数值问题、错误等相关),您还可以将所有 numpy 警告转换为系统错误:

numpy.seterr(all='raise') 

Now the previous code would crash:现在之前的代码会崩溃:

print(1/np.exp(999999999999))
FloatingPointError: overflow encountered in exp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM