AI-python

1、sklearn StandardScaler與“ with_std = False或True”之間的差異和“ with_mean = False或True”(https://www.it1352.com/1794584.html

****如果將with_meanwith_std設(shè)置為False,則將平均值μ設(shè)置為0std設(shè)為1,假定列/特征來自正態(tài)高斯分布(均值為0和1 std)。

如果將with_meanwith_std設(shè)置為True,那么您實際上將使用數(shù)據(jù)的真實μσ。這是最常見的方法。

2、python之meshgrid的使用(https://blog.csdn.net/qq_30638831/article/details/84628976)

image.png

4、關(guān)于numpy的astype(bool)和astype(int)等等(https://blog.csdn.net/wuxulong123/article/details/103387222
import numpy as np
a=[[1,2,1],[2,3,5]]
b=[[0,0,0],[2,3,5]]
c=np.array(a).astype(bool)
d=np.array(b).astype(bool)
print(c)
print(d)

tight_layout會自動調(diào)整子圖參數(shù),使之填充整個圖像區(qū)域。

6、Python isalpha()方法(https://www.runoob.com/python/att-string-isalpha.html

Python isalpha() 方法檢測字符串是否只由字母組成。

7、Python List count()方法(https://www.runoob.com/python/att-list-count.html)

count() 方法用于統(tǒng)計某個元素在列表中出現(xiàn)的次數(shù)。

8、from future import print_function用法(https://zhuanlan.zhihu.com/p/28641474
首先我們需要明白該句語句是python2的概念,那么python3對于python2就是future了,也就是說,在python2的環(huán)境下,超前使用python3的print函數(shù)。

舉例如下:
在python2.x的環(huán)境是使用下面語句,則第二句語法檢查通過,第三句語法檢查失敗

1 from future import print_function
2 print('you are good')
3 print 'you are good'

10、np.newaxis作用(https://blog.csdn.net/weixin_42866962/article/details/82811082

np.newaxis的功能:插入新維度

a=np.array([1,2,3,4,5])
aa=a[:,np.newaxis]
print(aa.shape)
print (aa)

輸出:(5, 1)
[[1]
[2]
[3]
[4]
[5]]

11、Mac python matplotlib Glyph xxxxx missing from current font的解決方案

  1. https://blog.csdn.net/fwj_ntu/article/details/105598145
  2. https://blog.csdn.net/qiqiqi98/article/details/106732789
  3. https://blog.csdn.net/weixin_38037405/article/details/107127610

https://blog.csdn.net/Fwuyi/article/details/123084642
plt.rcParams['font.sans-serif'] = ['SimHei'] #運行配置參數(shù)中的字體(font)為黑體(SimHei)

plt.rcParams['axes.unicode_minus'] = False #運行配置參數(shù)總的軸(axes)正常顯示正負號(minus)

12、numpy 學習之 np.c_的用法(https://blog.csdn.net/qq_33728095/article/details/102512600
np.c 中的c 是 column(列)的縮寫,就是按列疊加兩個矩陣,就是把兩個矩陣左右組合,要求行數(shù)相等。

16、Xlim函數(shù)--Matplotlib(https://blog.csdn.net/chongbaikaishi/article/details/108782039)

獲取或設(shè)置x軸數(shù)值顯示范圍
left, right = xlim() # return the current xlim 返回當前x軸邊界
xlim((left, right)) # set the xlim to left, right 設(shè)置x軸邊界

21、python self.class.name 理解(https://blog.csdn.net/aaa958099161/article/details/90177791

獲取類名

22、sklearn中的make_blobs的用法(https://blog.csdn.net/weixin_44177568/article/details/102213508)
data, label = make_blobs(n_features=2, n_samples=100, centers=3, random_state=3, cluster_std=[0.8, 2, 5])

n_features表示每一個樣本有多少特征值
n_samples表示樣本的個數(shù)
centers是聚類中心點的個數(shù),可以理解為label的種類數(shù)
random_state是隨機種子,可以固定生成的數(shù)據(jù)
cluster_std設(shè)置每個類別的方差

23、core_indices[model.core_sample_indices_] = True
https://www.cnblogs.com/xiguapipipipi/p/10109789.html
https://blog.csdn.net/qq_30031221/article/details/116494511

core_indices[model.core_sample_indices_] = True#model.core_sample_indices_:核心點的索引,因為labels_不能區(qū)分核心點還是邊界點,所以需要用這個索引確定核心點。
model.core_sample_indices_ border point位于labels中的下標

24、np.unique( )的用法(https://blog.csdn.net/u012193416/article/details/79672729

該函數(shù)是去除數(shù)組中的重復數(shù)字,并進行排序之后輸出。

25、axis=0 與 axis=1 的區(qū)分(https://blog.csdn.net/guoyang768/article/details/84818774)

1表示橫軸,方向從左到右;0表示縱軸,方向從上到下

26、分群評估指標(二)|調(diào)整互信息與Homogeneity, completeness and V-measure(https://zhuanlan.zhihu.com/p/425253563)

homogeneity : float
score between 0.0 and 1.0. ;1.0 stands for perfectly homogeneous labeling

completeness:float
score between 0.0 and 1.0. ;1.0 stands for perfectly complete labeling

v_measure:float
harmonic mean of the first two

A clustering result satisfies homogeneity: if all of its clusters contain only data points which are members of a single class.

A clustering result satisfies completeness: if all the data points that are members of a given class are elements of the same cluster.

27、聚類︱python實現(xiàn) 六大 分群質(zhì)量評估指標(蘭德系數(shù)、互信息、輪廓系數(shù))(https://blog.csdn.net/sinat_26917383/article/details/70577710
https://blog.csdn.net/howhigh/article/details/73928635

28、numpy.median()(https://blog.csdn.net/qq_42518956/article/details/103987722)
numpy模塊下的median作用為:
計算沿指定軸的均值
返回數(shù)組元素的均值

image.png

29、np.logspace() 對數(shù)等?數(shù)列(https://wenku.baidu.com/view/2f9546020422192e453610661ed9ad51f01d54f6.html)

logspace等?數(shù)列,默認以10為底

image.png

30、numpy.histogramdd(https://www.cjavapy.com/article/1105/
計算某些數(shù)據(jù)的多維直方圖。

返回值 :
H :ndarray
樣本x的多維直方圖。有關(guān)不同的可能語義,請參見normed和weights。

edges :list
D數(shù)組的列表,描述每個維度的面元邊緣。

31、python列表去重的兩種方法(https://blog.csdn.net/CHQC388/article/details/114648761
def test2():
lst = [1,2,5,6,3,5,7,3]
tmp = list(set(lst))
print(tmp) # 順序改變
tmp.sort(key=lst.index)
print(tmp) # 順序不變

32、【numpy】argmax參數(shù)辨析(axis=0,axis=1,axis=-1)(https://blog.csdn.net/weixin_39190382/article/details/105854567)

https://blog.csdn.net/byron123456sfsfsfa/article/details/88923085

argmax:一句話概括,返回最大值的索引。
當axis=0,是在列中比較,選出最大的 行 索引
當axis=1,是在行中比較,選出最大的 列 索引

33、python中endswith()函數(shù)的用法(https://blog.csdn.net/qq_40678222/article/details/83033587
判斷字符串是否以指定字符或子字符串結(jié)尾。

34、np.array()和np.asarray()的聯(lián)系與區(qū)別(https://blog.csdn.net/weixin_40922744/article/details/106737424

從定義中可以看出兩者的主要區(qū)別在于 np.array(默認情況下)將會copy該對象,而 np.asarray除非必要,否則不會copy該對象。

35、np.isnan()是判斷是否是空值(https://blog.csdn.net/tian_jiangnan/article/details/104862085

36、np.argmax(https://blog.csdn.net/CSDNwei/article/details/109183313

格式:np.argmax(a)
注意:返回的是a中元素最大值所對應(yīng)的索引值

37、np.hstack將參數(shù)元組的元素數(shù)組按水平方向進行疊加(https://blog.csdn.net/G66565906/article/details/84142034)

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

38、dtype=np.uint8(https://blog.csdn.net/qq_42191914/article/details/103103460

https://zhidao.baidu.com/question/532862991.html
uint8是8位無符號整型,uint16是16位無符號整型。

https://zhidao.baidu.com/question/519987520.html
uint8是指0~2^8-1 = 255數(shù)據(jù)類型,一般在圖像處理中很常見。

今天踩了一個坑,在opencv-python中,若想為圖像創(chuàng)建一個容器,需要指定dtype=np.uint8,否則雖然你的容器矩陣中是有值的,但是無法正常imshow

image.png

39、eval() 函數(shù)用來執(zhí)行一個字符串表達式,并返回表達式的值。(https://www.runoob.com/python/python-func-eval.html)

n=81
eval("n + 4")
85

40、python中assert的用法(https://blog.csdn.net/qq_37369201/article/details/109195257

def zero(s):
a = int(s)
assert a > 0,"a超出范圍" #這句的意思:如果a確實大于0,程序正常往下運行
return a

zero("-2") #但是如果a是小于0的,程序會拋出AssertionError錯誤,報錯為參數(shù)內(nèi)容“a超出范圍”

41、類型提示(self, nums: List[int]) -> List[int] (https://blog.csdn.net/chengyikang20/article/details/124778296

def greeting(name: str) -> str:
return 'Hello ' + name

greeting 函數(shù)中,參數(shù)name的類型是str,返回類型也是str。子類型也可以當作參數(shù)。

42、查看當前numpy版本(https://blog.csdn.net/cpick/article/details/122503241)

在cmd中依次輸入:

1.python

2.import numpy

3.numpy.version

43、numpy 安裝與卸載(https://blog.csdn.net/weixin_42081389/article/details/98185411/
pip uninstall numpy
pip install numpy==1.16.4

44、os.walk()的詳細理解(https://blog.csdn.net/qq_37344125/article/details/107972463

for root, dirs, files in os.walk(operate_path):
print('root:',root)
print('dirs:',dirs)
print('files:',files)
print('\n')

root:輸出了mm文件夾的的絕對路徑;
dirs:保存了mm文件夾下的所有子文件夾的目錄名(只有一層)
files:則是一個保存了mm文件夾下的所有文件的文件名,并保存到list中

45、PIL庫中g(shù)etpixel()-方法的使用(https://blog.csdn.net/qq_36430012/article/details/114303458)
getpixel()函數(shù)是用來獲取圖像中某一點的像素的RGB顏色值,getpixel的參數(shù)是一個像素點的坐標。對于圖象的不同的模式,getpixel函數(shù)返回的值不同。

46、L,P,RGB,RGBA,CMYK,YCbCr,I, F,不同的圖像模式(https://www.likecs.com/show-204895834.html
模式"L"為灰度圖像,它的每個像素用8個bit位表示,其中0表示黑,255表示白,其它數(shù)字表示不同的灰度。

47、numpy.ndarray 排序(https://www.jb51.net/article/130651.htm
ndarray.sort(axis=-1,kind='quicksort',order=None)

使用方法:a.sort

參數(shù)說明:

axis:排序沿著數(shù)組的方向,0表示按行,1表示按列

kind:排序的算法,提供了快排、混排、堆排

order:不是指的順序,以后用的時候再去分析這個

作用效果:對數(shù)組a排序,排序后直接改變了a

48、pandas從dataframe中提取多列數(shù)據(jù)(https://blog.csdn.net/weixin_44561414/article/details/125673541)

X = df_gps_org[["latitude","longitude"]]

49、Pandas>>按照行、列進行求和(https://blog.csdn.net/panfuyong11/article/details/115349576

x_train[['L']].apply(lambda x:x.sum())
x_train[['溫度']].apply(lambda x:x.sum())
x_train[['PH']].apply(lambda x:x.sum())

50、pandasDataFrame數(shù)據(jù)轉(zhuǎn)為list的方法(https://wenku.baidu.com/view/2605fc37b4360b4c2e3f5727a5e9856a57122649.html)

caohao= all_data['槽號']
print(type(caohao))
caohao_list = caohao.tolist()

51、pandas轉(zhuǎn)numpy(http://t.zoukankan.com/Renyi-Fan-p-13882431.html
1.使用DataFrame中的values方法

df.values
2.使用DataFrame中的as_matrix()方法

df.as_matrix()
3.使用Numpy中的array方法

np.array(df)

52、pandas根據(jù)某列去重(https://blog.csdn.net/qq_43965708/article/details/109892053

53、在matplotlib中創(chuàng)建子圖的多種方式(https://www.dandelioncloud.cn/article/details/1498083567296163841

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(100)

創(chuàng)建圖像布局對象fig

fig = plt.figure(figsize = (12, 6))

221代表創(chuàng)建2行2列一共4個子圖,并從左往右第1個子圖開始繪圖。

ax1 = fig.add_subplot(221)
ax1.plot(x, x)
ax2 = fig.add_subplot(222)
ax2.plot(x, -x)
ax3 = fig.add_subplot(223)
ax3.plot(x, x ** 2)
ax4 = fig.add_subplot(224)
ax4.plot(-x, x ** 2)
plt.show()

54、sklearn-數(shù)據(jù)集劃分(https://blog.csdn.net/qq_36387683/article/details/80468011)

x_train, x_test, y_train, y_test = train_test_split(data, label, test_size = 0.3, random_state = 7)

55、進度條
import time
from tqdm import tqdm

for i in tqdm(range(1000)):
time.sleep(.01)

56、如何將py文件轉(zhuǎn)化為exe(https://blog.csdn.net/m0_54812370/article/details/124493642
https://blog.csdn.net/a789865315/article/details/124259965

57、反編譯,如何將Python打包后的exe還原成.py?(https://blog.csdn.net/Csy79/article/details/125103466)
https://blog.csdn.net/weixin_49764009/article/details/120340153

58、調(diào)用fit_transform()與調(diào)用transform()的區(qū)別(https://blog.csdn.net/data_curd/article/details/112556315
fit_trainfrom方法時trainfrom和fit方法的結(jié)合,其意思是找出x_train的均值和方差,并應(yīng)用到x_train上
而下來調(diào)用trainform就直接用x_train求出來的方差和均值就行了。

fit(): Method calculates the parameters μ and σ and saves them as internal objects.
解釋:簡單來說,就是求得訓練集X的均值,方差,最大值,最小值,這些訓練集X固有的屬性。
transform(): Method using these calculated parameters apply the transformation to a particular dataset.
解釋:在fit的基礎(chǔ)上,進行標準化,降維,歸一化等操作(看具體用的是哪個工具,如PCA,StandardScaler等)。

59、Python—計算方差、標準差(https://blog.csdn.net/weixin_46560950/article/details/104905883
import numpy as np
arr = [1,2,3,4,5,6]

求方差

arr_var = np.var(arr)

求標準差

arr_std = np.std(arr,ddof=1)
print("方差為:%f" % arr_var)
print("標準差為:%f" % arr_std)

60、python怎么去掉換行符_python去除字符串中的換行符(https://blog.csdn.net/weixin_39610759/article/details/109924504)

一、去除空格

strip()

" xyz ".strip() # returns "xyz"

" xyz ".lstrip() # returns "xyz "

" xyz ".rstrip() # returns " xyz"

" x y z ".replace(' ', '') # returns "xyz"

二、替換 replace("space","")

用replace("\n", ""),后邊的串替換掉前邊的

61、Python字符串截取方式(https://blog.csdn.net/luckjump/article/details/119251647

62、如何用Python求眾數(shù)(https://blog.csdn.net/weixin_35757704/article/details/120842651?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6-120842651-blog-108671660.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-6-120842651-blog-108671660.pc_relevant_default&utm_relevant_index=7

import numpy as np
import statistics

my_list = [1, 1, 2, 3, 4, 1, 2, 3, 3, 3, 3, 3]
mode1 = statistics.mode(my_list)
mode2 = np.argmax(np.bincount(my_list))
print(mode1)
print(mode2)

63、ceil() 函數(shù)返回數(shù)字的上入整數(shù)。
import math

math.ceil( x )

64、格式化輸出(https://www.jb51.net/article/240789.htm
print(f"I love {'Geeks'} for "{'Geeks'}!"")

65、使用random.randint函數(shù)可以生成一個范圍內(nèi)的整數(shù),但是會重復(https://blog.csdn.net/u012759006/article/details/108252836

a = np.random.randint(0, 2, 10)

print(a) # [0 0 1 1 0 0 1 0 0 0]

66、Python二維列表的創(chuàng)建、轉(zhuǎn)換以及訪問詳解(https://www.jb51.net/article/246265.htm
追加一維列標來生成二維列標
row1 = [3, 4, 5]
row2 = [1, 5, 9]
row3 = [2, 5, 8]
row4 = [7, 8, 9]
matrix = []
matrix.append(row1)
matrix.append(row2)
matrix.append(row3)
matrix.append(row4)
print(matrix)

67、python中怎么刪除列表中的元素(https://m.php.cn/article/471345.html)

emove: 刪除單個元素,刪除首個符合條件的元素,按值刪除

str=[1,2,3,4,5,2,6]

str.remove(2)

str

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

68、# python判斷目錄是否存在,不存在則創(chuàng)建目錄
import os
wjjname=input("請輸入存放目錄\n") #輸入目標目錄
if os.path.exists(wjjname): #判斷目標目錄是否存在
print("目錄存在")
else:
print("目錄不存在")
print("正在為您創(chuàng)建目錄")
os.mkdir(wjjname) #如果不存在則創(chuàng)建目標目錄
print("目錄創(chuàng)建完成")
input("按回車鍵退出")

69、Python將列表中的元素轉(zhuǎn)化為數(shù)字并排序的示例(http://www.kaotop.com/it/22493.html
numbers = ['2', '4', '1', '3']
numbers = [2, 4, 1, 3]
numbers = list(map(int, numbers))

70、Python中的groupby分組(https://blog.csdn.net/qq_32618817/article/details/80587228

for i in df.groupby(['key1','key2']):
print(i)

輸出:

(('a', 'one'), data1 data2 key1 key2
0 -0.293828 0.571930 a one
4 -1.943001 0.106842 a one)
(('a', 'two'), data1 data2 key1 key2
1 1.872765 1.085445 a two)
(('b', 'one'), data1 data2 key1 key2
2 -0.466504 1.26214 b one)
(('b', 'two'), data1 data2 key1 key2
3 -1.125619 -0.836119 b two)

71、【Python】Json配置文件及簡單的封裝函數(shù)使用(https://blog.csdn.net/AwesomeP/article/details/126832604
import json

def readFileJson():
# 讀取配置文件
with open('global_data.json','r') as f:
data = json.load(f)
return data

72、python讀取文件最后幾行_python讀取文件最后一行兩種方法(https://blog.csdn.net/weixin_39997300/article/details/109879260

2 with open(fname, 'r', encoding='utf-8') as f: #打開文件

3 lines = f.readlines() #讀取所有行

4 first_line = lines[0] #取第一行

5 last_line = lines[-1] #取最后一行

73、Python列表逆序排列(https://www.jb51.net/article/248826.htm

會直接將列表里面的元素倒序排列 不需要創(chuàng)建新的副本儲存結(jié)果
優(yōu)點:1.節(jié)省內(nèi)存
缺點:1.直接修改了源數(shù)據(jù),如果后面使用源數(shù)據(jù)的話不方便,需要再倒序一次(多余的操作)

mylist = [1, 2, 3, 4, 5]
print(mylist)
mylist.reverse()
print(mylist)

74、python文件打包(https://blog.csdn.net/linZinan_/article/details/115573895
https://www.php.cn/faq/415527.html

方法1 pyinstaller -F -w --icon=“窗口文件圖標絕對路徑” 文件名.py 打包為單個exe文件,一般內(nèi)部包含了依賴庫,所以較大

方法2 pyinstaller -D -w --icon=“窗口文件圖標絕對路徑” 文件名.py 打包為一個文件夾,其中exe文件在文件夾內(nèi)部,這樣子單個exe文件就比較小

75、# Pandas 讀取 csv 文件提示:DtypeWarning: Columns (3) have mixed types. Specify dtype option on import or set low_memory=False.

data = pd.read_csv(f, low_memory=False)

76、python讀取csv文件的幾種方式(含實例說明)(https://blog.csdn.net/qq_43160348/article/details/124331781

import pandas as pd

df = pd.read_csv('../data_pro/audito_whole.csv')
print(df)

77、# Python獲取文件夾目錄下文件

os.listdir:參數(shù)為文件夾路徑,可以返回文件夾下的所有子文件夾、文件名稱

不能返回子文件夾下的文件

for file_name in os.listdir(path):
print(file_name)

78、python編寫程序,生成100個0 ~ 10之間的隨機整數(shù),并統(tǒng)計每個元素的出現(xiàn)次數(shù)。(https://blog.csdn.net/jxydwhb/article/details/105418304

import random
i = 1
d = {}
while i<101:
value = random.randint(0,10)
d[value] = d.get(value,0)+1
i = i+1
for i in range(0,11):
print("元素 {}出現(xiàn)的次數(shù):{}".format(i,d[i]))

79、函數(shù)、模塊、包庫關(guān)系
函數(shù)<模塊<包<庫
function < module < package < Libraries

80、

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容