a = sorted(d.items(), key=lambda x: x[1], reverse=True)
print(a)
输出
[('a',1),('b',23),('c',56)]
list不同元素计数(数组则转list计数)
list=[1,1,2,2,3]
print(list)
set1=set(list)
print(set1)
print(len(set1))
输出
[1, 1, 2, 2, 3]
{1, 2, 3}
3
将字符串型的list,tuple,dict转变成原有的类型
import ast
ast.literal_eval("[1,2,3,4]")
centroids = random.sample(data,num_clust) # data为列表,num_clust为采样数量
(1)
f['date'] = pd.to_datetime(f['date'].values, utc=True, unit='s').tz_convert(
"Asia/Shanghai").to_period("s")
f['date'] = f['date'].apply(lambda x: x.strftime('%Y%m%d-%H:%M:%S')) # 日期格式化
(2)
f['time'] = pd.to_datetime(f['time'].values, unit='s')
f['time'] = f['time'].apply(lambda x: x.strftime('%Y-%m-%d'))
curr_time = datetime.datetime.strptime(extract_date[0][43:], '%Y%m%d%H%M')
list_01 = pd.DataFrame(data=list01_tolastrecord[1::2]) # ::代表每隔一个值取一次
list_01[(list_01[0] <= gap)&(list_01[0] > gap-1)][0].count()
k-means 参数设置
https://segmentfault.com/a/1190000010863236
1)concat
data_seq = pd.DataFrame(data=result_list)
classlab = pd.DataFrame(data=lable)
to_write = pd.concat([data_seq,classlab],axis=1) #axis=1 按列合并
#
2)merge(有一定要求的合并,如按key值合并到一起)
[https://blog.csdn.net/Asher117/article/details/84725199]
f.iloc[:,1] # 取第一列
f.iloc[:,:100] # 取前100列
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['simhei']
mpl.rcParams['axes.unicode_minus']
fig, axes = plt.subplots(2, 1)
fig.set_size_inches(5.5, 8.5)
plt.subplots_adjust(hspace=0.4) # 设置上下间隔 wspace为左右
axes[0].bar(list(range(len(abnomal_mean_time))), sort_abnomallable)
axes[0].set_xlabel('所属类别')
axes[0].set_ylabel('时间/天')
axes[0].set_title('每次房颤持续平均时长')
axes[1].bar(list(range(len(nomal_mean_time))), nomal_mean_time)
axes[1].set_xlabel('所属类别')
axes[1].set_ylabel('时间/天')
axes[1].set_title('每次恢复正常持续平均时长')
plt.show()
用于模型评估,预测值与真实值得对比
from sklearn import metrics
cm_test = metrics.confusion_matrix(y_test, preds)