python数据结构练习

发布时间:2024-11-22 13:04

学习Python入门:了解基础语法和数据结构 #生活技巧# #工作学习技巧# #编程语言学习路径#

贝叶斯估计用到的数据结构
Pandas常用到的:索引与切片,unique,value_counts(),reindex,sort_index(可以用于Seires,也可以是DataFrame,但只对index本身,index或columns本身进行排序,而不是其对应的元素进行排序),order(对Series中的值进行排序),sort_index(by=)对DataFrame中的列值进行排序!,相关系数corr,协方差cov
array与DataFrame

x=pd.Series([1,1,1,2,2,3]) print(x.unique()) print(x.value_counts()) y_train=np.array([-1,-1,1,1,-1,-1,-1,1,1,1,1,1,1,1,-1]) print(y_train[0]) # error! print(y_train.unique()) Nunpy没有unique()与value_counts() y=pd.DataFrame(y_train) print(y[0]) print(y[0].value_counts()) frame=pd.DataFrame(np.arange(16).reshape((4,4)),index=['red','blue','black','green'],columns=['a','b','c','d']) #print(frame['a'] #选择列 print(frame['a'].value_counts()) #选择列中元素出现的次数 print(frame.a) #选择列 print('----以上是列') print(frame.ix[2].value_counts()) #返回第3行内容,一行竖着显示而已;同样可以选择行中元素出现的次数 print(frame.ix[[2,3]]) #返回第三行,第四行内容 print('----以上是行') print(frame['a']['red']) #引用二维表的某个元素,先是列标签,再行标签! **~~DataFrame引用元素先列后行~~ ** [1 2 3] 1 3 2 2 3 1 dtype: int64 -1 0 -1 1 -1 2 1 3 1 4 -1 5 -1 6 -1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 -1 Name: 0, dtype: int32 1 9 -1 6 Name: 0, dtype: int64 12 1 4 1 8 1 0 1 Name: a, dtype: int64 red 0 blue 4 black 8 green 12 Name: a, dtype: int32 ----以上是列 11 1 10 1 9 1 8 1 Name: black, dtype: int64 a b c d black 8 9 10 11 green 12 13 14 15 ----以上是行 0 k近邻算法用到的数据结构: Numpy中经常用到:shape,reshape,random,通用函数,聚合函数,索引与切片!

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475

import numpy as np
import pandas as pd
x=np.array([1,2,3,4])
print(x.shape[0]) #4
#print(x.shape[1]) error
y=np.array([[1,2],[2,3],[3,4]])
print(y.shape[0]) #输出个数3
print(y.shape[1]) #输出维度2
print(y[:,1]) #切片,相当于输出第二列
print(y[1])# 等价于y[1,]输出第二行

输出
4
3
2
[2 3 4]

Python自带的数据结构 1234

网址:python数据结构练习 https://www.yuejiaxmz.com/news/view/191125

相关内容

Python中的生活数据分析与个人健康监测.pptx
机器学习之数据预处理(Python 实现)
《数据结构与算法分析
Python数据分析:对饮食与健康数据的分析与可视化
python excel数据分析师职业技能
python数据分析
【Python】Python连接Hadoop数据中遇到的各种坑(汇总)
《数据结构与算法》—— O(3N)=O(N) ?
Python数据分析实战
智能生活垃圾检测与分类系统(UI界面+YOLOv5+训练数据集)

随便看看