生活随笔
收集整理的這篇文章主要介紹了
python数据结构练习
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
貝葉斯估計(jì)用到的數(shù)據(jù)結(jié)構(gòu)
Pandas常用到的:索引與切片,unique,value_counts(),reindex,sort_index(可以用于Seires,也可以是DataFrame,但只對(duì)index本身,index或columns本身進(jìn)行排序,而不是其對(duì)應(yīng)的元素進(jìn)行排序),order(對(duì)Series中的值進(jìn)行排序),sort_index(by=)對(duì)DataFrame中的列值進(jìn)行排序!,相關(guān)系數(shù)corr,協(xié)方差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沒(méi)有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()) #選擇列中元素出現(xiàn)的次數(shù)
print(frame.a) #選擇列
print('----以上是列')
print(frame.ix[2].value_counts()) #返回第3行內(nèi)容,一行豎著顯示而已;同樣可以選擇行中元素出現(xiàn)的次數(shù)
print(frame.ix[[2,3]]) #返回第三行,第四行內(nèi)容
print('----以上是行')
print(frame['a']['red']) #引用二維表的某個(gè)元素,先是列標(biāo)簽,再行標(biāo)簽!**~~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: int321 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: int64a b c d
black 8 9 10 11
green 12 13 14 15
----以上是行
0k近鄰算法用到的數(shù)據(jù)結(jié)構(gòu):
Numpy中經(jīng)常用到:shape,reshape,random,通用函數(shù),聚合函數(shù),索引與切片!
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]) #輸出個(gè)數(shù)3
print(y.shape[1]) #輸出維度2
print(y[:,1]) #切片,相當(dāng)于輸出第二列
print(y[1])# 等價(jià)于y[1,]輸出第二行
輸出
4
3
2
[2 3 4]
Python自帶的數(shù)據(jù)結(jié)構(gòu)
總結(jié)
以上是生活随笔為你收集整理的python数据结构练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。