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,通用函数,聚合函数,索引与切片!

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自带的数据结构

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值