python資料結構練習

2021-09-24 06:21:05 字數 2332 閱讀 4451

貝葉斯估計用到的資料結構

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

-10 -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

----以上是行

0k近鄰演算法用到的資料結構:

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

32[2 3 4]

python自帶的資料結構

Python資料結構練習

1.給定列表l,如 2,5,3,8,10,1 對其進行公升序排序並輸出。list 2,5,8,10,1 print list list.sort print list 2.給定字串s,如 123456 將其逆序輸出,使用切片 str 123456 print str 1 3.給定字典dict,如,分...

資料結構練習

include include define size 20 typedef struct list 重新命名結構體 list t 建立 list t creat list int size list length 0 空表初始化,實際長度為0 return list 返回堆區申請的首位址 判空 i...

Python基礎練習 資料結構大彙總

day 2 依舊是記錄在學習過程中容易混淆的點 列表 簡單資料型別 容器資料型別 字串 2.獲取列表中的元素 例子 淺拷貝與深拷貝 list1 123 456 789 213 list2 list1 list3 list1 print list2 123,456,789,213 print list...