我有一个包含 1400 个 obs 和 19 列的数据集。Target 变量的值为 1(我最感兴趣的值)和 0。类别的分布显示不平衡 (70:30)。
使用下面的代码我得到了奇怪的值(全 1)。我不知道这是由于数据过度拟合/不平衡问题还是由于特征选择问题(我使用皮尔逊相关性,因为所有值都是数字/布尔值)。我认为接下来的步骤是错误的。
import numpy as np
import math
import sklearn.metrics as metrics
from sklearn.metrics import f1_score
y = df['Label']
X = df.drop('Label',axis=1)
def create_cv(X,y):
if type(X)!=np.ndarray:
X=X.values
y=y.values
test_size=1/5
proportion_of_true=y[y==1].shape[0]/y.shape[0]
num_test_samples=math.ceil(y.shape[0]*test_size)
num_test_true_labels=math.floor(num_test_samples*proportion_of_true)
num_test_false_labels=math.floor(num_test_samples-num_test_true_labels)
y_test=np.concatenate([y[y==0][:num_test_false_labels],y[y==1][:num_test_true_labels]])
y_train=np.concatenate([y[y==0][num_test_false_labels:],y[y==1][num_test_true_labels:]])
X_test=np.concatenate([X[y==0][:num_test_false_labels] ,X[y==1][:num_test_true_labels]],axis=0)
X_train=np.concatenate([X[y==0][num_test_false_labels:],X[y==1][num_test_true_labels:]],axis=0)
return X_train,X_test,y_train,y_test
X_train,X_test,y_train,y_test=create_cv(X,y)
X_train,X_crossv,y_train,y_crossv=create_cv(X_train,y_train)
tree = DecisionTreeClassifier(max_depth = 5)
tree.fit(X_train, y_train)
y_predict_test = tree.predict(X_test)
print(classification_report(y_test, y_predict_test))
f1_score(y_test, y_predict_test)
Run Code Online (Sandbox Code Playgroud)
输出:
precision recall f1-score support
0 1.00 1.00 1.00 24
1 1.00 1.00 …Run Code Online (Sandbox Code Playgroud) python machine-learning resampling scikit-learn cross-validation
有没有一种有效的方法来使用零阶保持重新采样numpy数组?理想情况下,像numpy.interp那样的签名?
我知道scipy.interpolate.interp1d,但我确信有一个矢量化替代方案可用于处理这样的情况.
这是设置代码:
import pandas
from datetime import datetime
a_values = [1728, 1635, 1733]
a_index = [datetime(2011, 10, 31), datetime(2012, 1, 31), datetime(2012, 4, 30)]
a = pandas.Series(data=a_values, index=a_index)
aa_values = [6419, 5989, 6006]
aa_index = [datetime(2011, 9, 30), datetime(2011, 12, 31), datetime(2012, 3, 31)]
aa = pandas.Series(data=aa_values, index=aa_index)
apol_values = [1100, 1179, 969]
apol_index = [datetime(2011, 8, 31), datetime(2011, 11, 30), datetime(2012, 2, 29)]
apol = pandas.Series(data=apol_values, index=apol_index)
Run Code Online (Sandbox Code Playgroud)
以下是表中数据的样子(未显示APOL的第3个值):

目标是将数据与日历季度标记对齐,以便可以比较3个数据集.只是浏览下面的日期,2012年3月,2011年12月和2011年9月似乎是合理的对齐标记.
这是fill_method ='ffill'的输出:
In [6]: a.resample('Q', fill_method='ffill')
Out[6]:
2011-12-31 1728
2012-03-31 1635 …Run Code Online (Sandbox Code Playgroud) 我正在将多个时间序列表读入pandas dataFrame,并将它们与常见的pandas datetime索引连接在一起.记录时间序列的数据记录器不是100%准确,这使得重新采样非常烦人,因为根据时间是略高于还是低于采样间隔,它将创建NaN并开始使我的系列看起来像一条虚线.这是我的代码
def loaddata(filepaths):
t1 = time.clock()
for i in range(len(filepaths)):
xl = pd.ExcelFile(filepaths[i])
df = xl.parse(xl.sheet_names[0], header=0, index_col=2, skiprows=[0,2,3,4], parse_dates=True)
df = df.dropna(axis=1, how='all')
df = df.drop(['Decimal Year Day', 'Decimal Year Day.1', 'RECORD'], axis=1)
if i == 0:
dfs = df
else:
dfs = concat([dfs, df], axis=1)
t2 = time.clock()
print "Files loaded into dataframe in %s seconds" %(t2-t1)
files = ["London Lysimeters corrected 5min.xlsx", "London Water Balance 5min.xlsx"]
data = loaddata(files)
Run Code Online (Sandbox Code Playgroud)
这是一个关于索引的想法:
data.index
class'pandas.tseries.index.DatetimeIndex'> [2012-08-27 12:05:00.000002,...,2013-07-12 15:10:00.000004]时长:91910,频率:无,时区:无 …
我有一个已导入MATLAB的wav文件,其采样率为44.1 kHz.我正在尝试将此音频文件重新采样为22.05 kHz,然后将其恢复为44.1 kHz.但是,我对如何在MATLAB中使用resample函数感到困惑,如果这甚至是我应该用来执行此操作的函数.任何帮助将不胜感激.谢谢!
我真的很喜欢使用插入符号至少在建模的早期阶段,特别是因为它非常容易使用重采样方法.然而,我正在开发一个模型,其中训练集通过半监督自我训练添加了相当数量的案例,并且我的交叉验证结果因此而真正偏离.我对此的解决方案是使用验证集来测量模型性能,但我看不到直接在插入符中使用验证集的方法 - 我是否遗漏了某些东西或者这只是不支持?我知道我可以编写自己的包装器去做插入符号通常用于m的插件,但是如果有一个解决方法而不必这样做会非常好.
以下是我遇到的一个简单例子:
> library(caret)
> set.seed(1)
>
> #training/validation sets
> i <- sample(150,50)
> train <- iris[-i,]
> valid <- iris[i,]
>
> #make my model
> tc <- trainControl(method="cv")
> model.rf <- train(Species ~ ., data=train,method="rf",trControl=tc)
>
> #model parameters are selected using CV results...
> model.rf
100 samples
4 predictors
3 classes: 'setosa', 'versicolor', 'virginica'
No pre-processing
Resampling: Cross-Validation (10 fold)
Summary of sample sizes: 90, 90, 90, 89, 90, 92, ...
Resampling results across …Run Code Online (Sandbox Code Playgroud) 我正在使用重新采样数据框,它可以在几小时,几分钟内工作,但不会重新采样少于秒.程序即使在很短的时间内也会挂起.我错过了什么吗?
我试过0.000001S,U等......到目前为止没有任何工作.
我的时间格式:2015-08-29 19:30:47.015506
你可以看到varable sf代表重新采样频率.
grph = df.set_index('Date and Time').resample(sf, len).astype(int)
Run Code Online (Sandbox Code Playgroud)
如何在微秒粒度上重新采样数据帧?
我有一个dataframe t_unit,这是一个pd.read_csv()函数的结果。
datetime B18_LR_T B18_B1_T
24/03/2016 09:00 21.274 21.179
24/03/2016 10:00 19.987 19.868
24/03/2016 11:00 21.632 21.417
24/03/2016 12:00 26.285 24.779
24/03/2016 13:00 26.897 24.779
Run Code Online (Sandbox Code Playgroud)
我正在重新采样数据帧以使用代码计算第5个和第5个百分点:
keys_actual = list(t_unit.columns.values)
for key in keys_actual:
ts_wk = t_unit[key].resample('W-MON')
ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p', inplace=True)
ts_wk_95p = ts_wk.apply(lambda x: x.quantile(0.95)).round(decimals=1).rename(key+'_95p', inplace=True)
Run Code Online (Sandbox Code Playgroud)
一切正常,但是当我通过的方式向数据框添加一列时pd.concat:
datetime B18_LR_T B18_B1_T ext_T
24/03/2016 09:00 21.274 21.179 6.9
24/03/2016 10:00 19.987 19.868 7.5
24/03/2016 11:00 21.632 21.417 9.1
24/03/2016 12:00 26.285 24.779 9.9
24/03/2016 13:00 …Run Code Online (Sandbox Code Playgroud) 给定一个数据帧:
Index A B
2016-01-01 5 10
2016-01-01 1 2
2016-01-02 1 1
Run Code Online (Sandbox Code Playgroud)
是否可以使用sum重新采样DataFrame,并在DataFrame的末尾添加一列,其中包括bin中的观察计数,结果如下:
Index A B Count
2016-01-01 6 12 2
2016-01-02 1 1 1
Run Code Online (Sandbox Code Playgroud) 我想解决以下问题。我必须在一个非常大的集合中进行采样,数量级为 10^20,并提取一个样本而没有重复大小约为 10%-20% 的样本。鉴于集合的大小,我认为像 Fisher-Yates 这样的算法是不可行的。
我在想像随机路径树这样的东西可能适用于 O(n log n) 并且不能更快地完成,但我想问一下是否已经实现了这样的东西。
感谢您的时间!
resampling ×10
pandas ×5
python ×5
python-2.7 ×2
time-series ×2
algorithm ×1
audio ×1
dataframe ×1
datetime ×1
math ×1
matlab ×1
numpy ×1
quantile ×1
r ×1
r-caret ×1
sampling ×1
scikit-learn ×1
scipy ×1
sorting ×1