Python 基本函式應用

2021-10-07 13:32:43 字數 3543 閱讀 5527

1、使用int() 獲得數值輸入

使用函式input()時,python將使用者的輸入解讀為字串,例如

age=input("how old are you ")

>>21

age>>21#字串21

函式int()讓python將輸入視為數值

age=input("how old are you?")

age=int(age)

>>18

age>>18#數字18

2、list用法含義

list() 方法用於將元組轉換為列表。

注意:元組與列表是非常類似的,區別在於元組的元素值不能修改,元組是放在括號中,列表是放於方括號中。 

list(tup)#tup是元組
3、np.hstack()

np.hstack將引數元組的元素陣列按水平方向進行疊加,行與行拼在一起

import numpy as np

arr1 = np.array([[1,3], [2,4] ])

arr2 = np.array([[1,4], [2,6] ])

res = np.hstack((arr1, arr2))

print (res)

#輸出[[1 3 1 4]

[2 4 2 6]]

import numpy as np

arr1 = [1,2,3]

arr2 = [4,5]

arr3 = [6,7]

arr = np.hstack((arr1, arr2,arr3))

print (arr)

#輸出[1 2 3 4 5 6 7]

4、計算協方差矩陣

import numpy

from sklearn.preprocessing import standardscaler

x_std=standardscaler.fit_transform(x)

cov_mat = np.cov(x_std.t)#x_std為預處理過的資料集

5、矩陣之間的點乘.dot()

y = x_std.dot(matrix_w)#x_std與matrix_w均為矩陣
6、求協方差矩陣的特徵值與特徵向量

cov_mat = np.cov(x_std.t)

eig_vals, eig_vecs = np.linalg.eig(cov_mat)#計算特徵值和特徵向量

print('eigenvectors \n%s' %eig_vecs)#特徵向量

print('\neigenvalues \n%s' %eig_vals)#特徵值

7、排序裡key=lambda _: _[0]的理解

sorted(data, key=lambda _: _[0]),若資料是列表或元組裡的資料仍是列表與元組,那麼在對內進行排序時需要指定排序的基準元素是哪個

例:key=lambda _: _[0]指以元組內各列表的第乙個為準進行排序

>>>data = ([1, 4, 3], [3, 2, 5], [5, 1, 2], [4, 3, 1], [2, 5, 3])

>>>sorted(data, key=lambda _: _[0])

[[1, 4, 3], [2, 5, 3], [3, 2, 5], [4, 3, 1], [5, 1, 2]]

>>>sorted(data, key=lambda _: _[1])

[[5, 1, 2], [3, 2, 5], [4, 3, 1], [1, 4, 3], [2, 5, 3]]

>>>sorted(data, key=lambda _: _[2])

[[4, 3, 1], [5, 1, 2], [1, 4, 3], [2, 5, 3], [3, 2, 5]]

8、time.sleep(3) #睡眠的意思,暫停3秒執行下方的**

import time

time.sleep(3)#睡眠的意思,暫停3秒執行下方的**

start_time = time.time()#獲取**執行到這裡的時間

9、python的引數有預設引數、可變引數和關鍵字引數等

*args是可變引數,args接收的是乙個tuple(元組),**kw是關鍵字引數,kw接收的是乙個dict(字典)

10、index_col(0):指定讀入資料的第一列為索引,index_col(1):指定資料的第二列為索引

11、dropna.(thresh=n,axis=1)

thresh=n,保留至少有 n 個非 na 數的行, 

dropna 在使用 axis 時,預設 axis=0 表示按行,只要行有nan就去除此行,     

axis=1 表示按列,只要列有nan就去除此列

12、print(loans_2007.iloc[0])#列印資料loans_2007的第一行

print("該資料共有列",loans_2007.shape[1])#列印出該資料共有多少個列

print("該資料共有行",loans_2007.shape[0])#列印出該資料共有多少個行

print(loans_2007['loan_status'].value_counts())#統計資料中,列loan_status中各種屬性值出現的次數

13、loans_2007 = pd.read_csv('loanstats3a.csv', skiprows=1)#skiprows=1把第資料的二行當做表頭的各列名(屬性名)

14、orig_columns = loans_2007.columns#展現出所有的列,即資料的特證名

15、pandas裡的unique()函式與nunique()函式的區別

不同點:

unique()函式是以陣列形式返回列的所有唯一值

nunique()函式則是返回所有唯一值的個數

16、np.zeros(),np.ones()

生成n維零矩陣,n維一矩陣

np.zeros((3,2))

>>>

array[[0,0],

[0,0]

[0,0]]

17、np.linespace()

生成指定間隔的一列數字

18、np.random.randn()

rand函式根據給定維度生成[0,1)之間的資料,包含0,不包含1,返回值為指定維度的array

np.random.rand(4,2)

>>>

array([[ 0.02173903, 0.44376568],

[ 0.25309942, 0.85259262],

[ 0.56465709, 0.95135013],

[ 0.14145746, 0.55389458]])

C 呼叫python指令碼函式基本應用指南

c 呼叫python指令碼函式 如下 py initialize 初始化 python interpreter pyobject pname pystring fromstring test1 python指令碼檔名 pyobject pmodule pyimport import pname im...

函式的基本應用

def func 定義函式,函式的命名規則遵循 變數名的命名規則,首字母不可以大寫 塊func 呼叫函式 ef func a,b,c 括號內是形參 塊func c,b,a 括號內是實參 誰呼叫,值就給誰 return 返回值 不寫時 函式預設返回none,或者寫了,不寫返回值,也返回none 返回乙...

fabric role 基本函式 組合函式 應用

usr bin env python coding utf 8 from fabric.api import 定義機器列表 m110 root 192.168.0.110 22 m228 root 192.168.0.228 22 定義機器密碼 env.passwords 定義機器分組 env.ro...