我对这一切都很陌生。可能存在配置问题。
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
import seaborn as sns
from numpy import matrix
logisticreg = LogisticRegression()
logisticreg.fit(xtrain, ytrain)
退货
File ...\Python\Python312\Lib\site-packages\sklearn\linear_model\_logistic.py:1303, in LogisticRegression.fit(self, X, y, sample_weight)
1300 else:
1301 n_threads = 1
-> 1303 fold_coefs_ = Parallel(n_jobs=self.n_jobs, verbose=self.verbose, prefer=prefer)(
1304 path_func(
1305 X,
1306 y,
1307 pos_class=class_,
1308 Cs=[C_],
...
-> 1871 if isinstance(a, np.matrix):
1872 return asarray(a).ravel(order=order)
1873 else:
AttributeError: module 'numpy' has no attribute 'matrix'
运行 numpy 版本 1.26.2
尝试运行示例
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
# y = 1 * x_0 + 2 * x_1 + 3`
y = np.dot(X, np.array([1, 2])) + 3
reg = LinearRegression().fit(X, y)
reg.score(X, y)
reg.coef_
reg.intercept_
reg.predict(np.array([[3, 5]]`))
同样的错误
我想我明白问题出在哪里了。我传入的数据对象格式不正确。纠正这个似乎可以解决问题。