当我试图用seaborn保存我的情节时,像这样:
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
from pylab import savefig
array = [[100,0],
[33,67]]
df_cm = pd.DataFrame(array)
svm = sn.heatmap(df_cm, annot=True,cmap='coolwarm', linecolor='white', linewidths=1)
svm.savefig('svm_conf.png', dpi=400)
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
AttributeError Traceback (most recent call last)
<ipython-input-71-5c0ae9cda020> in <module>()
----> 1 svm.savefig('svm_conf.png', dpi=400)
AttributeError: 'AxesSubplot' object has no attribute 'savefig'
Run Code Online (Sandbox Code Playgroud)
我以前用相同的代码保存了一些箱图,但这一次,它不起作用.
尝试实施 XGBoost 以确定最重要的变量时,我对数组有一些错误。
我的完整代码如下
from numpy import loadtxt
from numpy import sort
import pandas as pd
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.feature_selection import SelectFromModel
df = pd.read_csv('data.txt')
array=df.values
X= array[:,0:330]
Y = array[:,330]
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=7)
model = XGBClassifier()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
predictions = [round(value) for value in y_pred]
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
TypeError: type str doesn't define __round__ method
Run Code Online (Sandbox Code Playgroud)
我能做什么?
我尝试运行这段代码:
from cdlib import algorithms
import networkx as nx
G = nx.karate_club_graph()
coms = algorithms.louvain(G, resolution=1., randomize=False)
Run Code Online (Sandbox Code Playgroud)
但错误仍然相同。我已尝试 AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'给出的所有选项
但它不起作用。
另外,我在 Google Colab 工作,并且安装了 cdlib。
当我绘制功能重要性图时,会出现混乱的图。我有7000多个变量。我了解内置功能只会选择最重要的功能,尽管最终图形不可读。这是完整的代码:
import numpy as np
import pandas as pd
df = pd.read_csv('ricerice.csv')
array=df.values
X = array[:,0:7803]
Y = array[:,7804]
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
seed=0
test_size=0.30
X_train, X_test, y_train, y_test = train_test_split(X,Y,test_size=test_size, random_state=seed)
from xgboost import XGBClassifier
model = XGBClassifier()
model.fit(X, Y)
import matplotlib.pyplot as plt
from matplotlib import pyplot
from xgboost import plot_importance
fig1=plt.gcf()
plot_importance(model)
plt.draw()
fig1.savefig('xgboost.png', figsize=(50, 40), dpi=1000)
Run Code Online (Sandbox Code Playgroud)
python machine-learning matplotlib feature-selection xgboost
当我尝试使用 Pipeline 和 param_grid 运行 RandomForestClassifier 时:
pipeline = Pipeline([("scaler" , StandardScaler()),
("rf",RandomForestClassifier())])
from sklearn.model_selection import GridSearchCV
param_grid = {
'max_depth': [4, 5, 10],
'max_features': [2, 3],
'min_samples_leaf': [3, 4, 5],
'n_estimators': [100, 200, 300]
}
# initialize
grid_pipeline = GridSearchCV(pipeline,param_grid,n_jobs=-1, verbose=1, cv=3, scoring='f1')
# fit
grid_pipeline.fit(X_train,y_train)
grid_pipeline.best_params_
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ValueError: Invalid parameter max_depth for estimator Pipeline(memory=None,
steps=[('scaler',
StandardScaler(copy=True, with_mean=True, with_std=True)),
('rf',
RandomForestClassifier(bootstrap=True, ccp_alpha=0.0,
class_weight=None, criterion='gini',
max_depth=None, max_features='auto',
max_leaf_nodes=None, max_samples=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0,
n_estimators=100, n_jobs=None,
oob_score=False, random_state=None,
verbose=0, warm_start=False))], …Run Code Online (Sandbox Code Playgroud) python ×4
matplotlib ×2
xgboost ×2
arrays ×1
graph ×1
gridsearchcv ×1
networkx ×1
numpy ×1
plot ×1
scikit-learn ×1
seaborn ×1
typeerror ×1