如果我计划使用交叉验证 (KFold),我是否仍应将数据集拆分为训练数据和测试数据并仅在训练集上执行训练(包括交叉验证)?或者简历会为我做一切吗?例如
选项1
X_train, X_test, y_train, y_test = train_test_split(X,y)
clf = GridSearchCV(... cv=5)
clf.fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)
选项2
clf = GridSearchCV(... cv=5)
clf.fit(X y)
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的数据帧(df):
PointID Time geojson
---- ---- ----
36F 2016-04-01T03:52:30 {'type': 'Point', 'coordinates': [3.961389, 43.123]}
36G 2016-04-01T03:52:50 {'type': 'Point', 'coordinates': [3.543234, 43.789]}
Run Code Online (Sandbox Code Playgroud)
geojson 列包含 geoJSON 格式的数据(本质上是一个 Python 字典)。
我想以 geoJSON 格式创建一个新列,其中包括时间坐标。换句话说,我想将时间信息注入到 geoJSON 信息中。
对于单个值,我可以成功执行以下操作:
oldjson = df.iloc[0]['geojson']
newjson = [df['coordinates'][0], df['coordinates'][1], df.iloc[0]['time'] ]
Run Code Online (Sandbox Code Playgroud)
对于单个参数,我成功地将 dataFrame.apply 与 lambda 结合使用(感谢 SO:相关问题
但是现在,我有两个参数,我想在整个 DataFrame 上使用它。由于我对 .apply 语法和 lambda 没有信心,我不知道这是否可能。我想做这样的事情:
def inject_time(geojson, time):
"""
Injects Time dimension into geoJSON coordinates. Expects a dict in geojson POINT format.
"""
geojson['coordinates'] = [geojson['coordinates'][0], geojson['coordinates'][1], time]
return …Run Code Online (Sandbox Code Playgroud) 我从数据库加载一个DataFrame,并有一个dict列,如下所示:
id some_value ... coordinates
15 34.7 {'type': 'Point', 'coordinates': [-3.2, 37.0]}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用pd.to_csv()将我的DataFrame保存到磁盘然后重新读取它时,包含坐标的列不是dict,而是字符串:
id some_value ... coordinates
15 34.7 "{'type': 'Point', 'coordinates': [-3.2, 37.0]}"
Run Code Online (Sandbox Code Playgroud)
我如何告诉Pandas将此专栏作为dict读取,或者如何将此列转换回dict?
SVM:http://scikit-learn.org/stable/modules/svm.html#classification
SGD:http://scikit-learn.org/stable/modules/sgd.html#classification
似乎对我的眼睛做的几乎一样,因为他们写的是"SGD实现了一个线性模型".有人可以解释它们之间的差异吗?
所以我用 scikit-learns 支持向量分类器(svm.SVC)结合流水线和网格搜索构建了一个小例子。经过拟合和评估,我得到了一条看起来很有趣的 ROC 曲线:它只弯曲一次。
我想我会在这里得到更多的曲线形状。谁能解释这种行为?最小的工作示例代码:
# Imports
import sklearn as skl
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn import preprocessing
from sklearn import svm
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn import metrics
from tempfile import mkdtemp
from shutil import rmtree
from sklearn.externals.joblib import Memory
def plot_roc(y_test, y_pred):
fpr, tpr, thresholds = skl.metrics.roc_curve(y_test, y_pred, pos_label=1)
roc_auc = skl.metrics.auc(fpr, tpr)
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve …Run Code Online (Sandbox Code Playgroud)