1、安装pip: python -m pip install -U pip (打开命令行窗口:Anaconda Prompt)
升级:python -m pip install --upgrade pip (相关用法:https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels)
查询已经安装的包:pip list
如果“pip install 工具包名称”安装不上,那就下载目标软件到安装目录下的pkgs, 然后通过Anaconda Prompt命令窗口进入该路径下,再输入“pip install 工具包名称”命令进行安装。
2、安装nilearn: pip install -U --user nilearn(打开命令行窗口:Anaconda Prompt)
卸载: pip uninstall [options] <package> pip uninstall [options] -r <requirements file>
3、python算pearson's correlation:
import scipy
[r,p] = scipy.stats.pearsonr(x,y):表示求x和y的pearson相关
pd.read_excel('motion_parameters_all.xls')#读取excel文件
4、路径正确的表示以及导入.mat文件:
如果运行时报错:mat4py模块不存在 ----> 在anaconda prompt终端中,输入该命令:pip install mat4py
import mat4py as mt
in_path = 'F:/BrainAging/SDSU/test/Results/mat_in_20.mat'
data_ext = mt.loadmat(ex_path)
5、python 生成随机矩阵:
python中 numpy.empty() 该函数表示生成一个随机矩阵;
或者arr = np.arange(10)
6、查询矩阵维度:numpy.shape(mat)
7'/''//'区别:前者表示浮点除,后者则表示整除
8、构建矩阵:
from numpy import *
matri_data = mat('1 2 3; 2 3 2')

   9、设置colorbar 的颜色:

方法一:

colors = ['white', '#FFA500','#FA8072','#FAA460']
cmap = mpl.colors.ListedColormap(colors)
plt.imshow(X,cmap=cmap)
plt.colorbar()方法二:("hot,autumn, jet【推荐色】
#plt.imshow(X,cmap=plt.cm.hot)
plt.imshow(X,cmap=plt.cm.autumn)
plt.colorbar()

10、获取向量y某个维度的长度:y.shape[m],其中m代表获取第几维度,
11、idx_set = set(range(5))得到的结果:{0, 1, 2, 3, 4, 5}--->相当于matlab中的cell
12、清空变量:reset(快捷键: ctrl+L)
13、选中对应的行 (1)快捷键组合为ctrl键和[键:ctrl+[ 或者(2)快捷键组合为ctrl键和]键:ctrl+]
14、数组的特性:x[1,2]的shape值(2,),意思是一维数组,数组中有2个元素;y[[1],[2]]的shape值是(2,1),意思是一个二维数组,每行有1个元素;z [[1,2]]的shape值是(1,2),意思是一个二维数组,每行有2个元素。
15、生成空的矩阵:mat = None; 生成空的list: list = []
16、两种类型的相互转换函数: 1)array转list:a = a.tolist() ;2)list转array:a =np.array(a)
17、List的基本操作:1)list.append(list1),是将list1作为一个数据项、一个元素,追加在list中;2)list.extend(list1),是将list1与list相连接。
18、找到矩阵中最值,并返回行和列:re = np.where(results == np.min(results[1:80,1:80])),使用x == np.max(x)19、单引号和双引号的区别在于,前者需要转义字符,后者不需要;eg. s1 = ' let\'s go '; s2 = "let's go";
20、1个双引号和三个双引号区别在于,前者换行需要转义字符,后者不需要, 而且三个双引号可以有注释;
eg. s3 = "hello \ s4 = """ hello
world" world """
21、三个双引号和三个单引号区别,在于看字符中是否包含单引号字符,如果有,则用三个双引号。
22、array数组的基本操作:a = np.array([[1, 2], [3, 4]])
>>> np.mean(a) # 将上面二维矩阵的每个元素相加除以元素个数(求平均数)--->2.5
>>> np.mean(a, axis=0) # axis=0,计算每一列的均值 ----> array([ 2., 3.])
>>> np.mean(a, axis=1) # 计算每一行的均值 ----> array([ 1.5, 3.5])
23、取绝对值:abs()
24、如何导入一个项目:
关于python的集成开发环境有很多种,比如PyCharm,Spyder等。楼主在初学python时使用的是Spyder,在打开项目时遇到一点小问题,记录一下解决方法。。
首先在想试用Spyder新建项目时,习惯性的选择了工具栏中的“File”,紧接着就可以看到有一个“New file...”,但显然,这样只能创建一个文件,而不是想要的项目。
之后仔细查看,会发现在同样在工具栏,有独立的一个“Projects”,就可以创建项目了...
接下来关于打开项目,在使用Scrapy框架创建爬虫后,发现不能顺利打开项目,会遇到“XXX is not a Spyder project!"
对比使用Spyder创建的项目以及自己没有使用Spyder创建的项目,会发现在第一级目录下,Spyder创建的项目多一个.spyproject文件夹,打开后是四个配置文件,想必问题就出在这里了。将这个文件夹放置于自己没有使用Spyder创建的项目的一级目录下,就可以顺利打开自己的项目了。

25、python 里 np.array 的shape ( ,)与( ,1)的区别

>>> import numpy as np
>>> x = np.array([1, 2])
>>> y = np.array([[1],[2]])
>>> z = np.array([[1,2]])
>>> print(x.shape)
(2,)
>>> print(y.shape)
(2, 1)
>>> print(z.shape)
(1, 2)
x[1,2]的shape值(2,),意思是一维数组,数组中有2个元素
y[[1],[2]]的shape值是(2,1),意思是一个二维数组,每行有1个元素
z [[1,2]]的shape值是(1,2),意思是一个二维数组,每行有2个元素

26、mean, (std)用法:
import numpy as np
num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]])
now2 = np.mat(num1)
now2
matrix([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
np.mean(now2) # 对所有元素求均值
3.5
np.mean(now2,0) # 压缩行,对各列求均值
matrix([[ 2.5, 3.5, 4.5]])
np.mean(now2,1) # 压缩列,对各行求均值
matrix([[ 2.],
[ 3.],
[ 4.],
[ 5.]])
27、类比maltab中函数: 1)python中的 np.tile()相当于 np.repmat() 2)data(j,:) 等价 y_score[[j],:]28、返回格式:return (sel_fea, fea_num)29、生成随机数:np.random.randn(n_samples)30、将类别二值化:y = label_binarize(y, classes=[0, 1, 2])31、生成随机噪声: # Add noisy features to make the problem harder
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
50、在这里我们介绍两个拼接数组的方法:np.vstack():在竖直方向上堆叠; np.hstack():在水平方向上平铺33、spyder中让生成的图像单独在窗口中显示:
1)生成图像在窗口中单独显示的命令:%matplotlib空格qt 在python3的版本中是qt5
2)生成图像在命令行中单独显示的命令:%matplotlib空格inline 34、矩阵的转置:data = data.T35、python将nan, inf转为特定的数字: import numpy as np
a = np.array([[np.nan, np.nan, 1, 2], [np.inf, np.inf, 3, 4], [1, 1, 1, 1], [2, 2, 2, 2]])
where_are_nan = np.isnan(a)
where_are_inf = np.isinf(a)
a[where_are_nan] = 0
a[where_are_inf] = 036、将list中某一个元素赋值给变量:a = list[5]
37、round使用:np.round([3.00,4.00,5.2]) ----> [3,4,5]
38、修改变量的数据类型:a.astype(np.int)---->将数组a里面数据类型修改为整数型
39、读出路径的上级,上上级目录:

​  #当前文件的路径​

​  pwd ​​​​=​​​ ​​os.getcwd()​

​  #当前文件的父路径​

​  father_path​​​​=​​​​os.path.abspath(os.path.dirname(pwd)​​​​+​​​​os.path.sep​​​​+​​​​"."​​​​)​

​  #当前文件的前两级目录​

​  grader_father​​​​=​​​​os.path.abspath(os.path.dirname(pwd)​​​​+​​​​os.path.sep​​​​+​​​​".."​​​​)​

40、python中np.where类似于Matlab中find函数: idx = np.where(a > 2)41、元组转数组:index = np.array(indx) %indx是元组类型数据42、数组取特定index的值:
1)y[0:59,](此处y的数据类型是一维数组,float,(107,))43、通过shuffle打乱数据:from sklearn.utils import shuffle;df = shuffle(df);
44、通过np.delete删除矩阵特定的行(axis = 0),列(axis = 1):X = np.delete(X,(0,1,2,3,4,5,6,7,8,9,10),axis = 0)
45、非零元素个数: print(len(a.nonzero(Y)[0]))
46、非零元素的值:print(a[a.nonzero(Y)[0]])
47、用python怎么读取mat文件的三维矩阵

​  # coding=utf-8​

​  import​​​ ​​scipy.io as sio​

​  import​​​ ​​numpy as np​

​  data ​​​​=​​​ ​​sio.loadmat(r​​​​'C:\Users\xiligey\Desktop\C3.mat'​​​​)  ​​​​# 把这个路径改成你的mat路径即可​

​  print​​​​(​​​​'scipy读取三维矩阵的初步结果: \n%s\n'​​​ ​​%​​​ ​​data)​

​  result ​​​​=​​​ ​​data[​​​​'C'​​​​]​

​  print​​​​(​​​​'提取出其中的三维数组: \n%s'​​​ ​​%​​​ ​​result)​

48、在python中,读取mat文件,并存为.npy,代码如下:

#方法一
import mat4py as mt
data_path = 'E:/structural_network/res_NetPro_hcc/NBS/Edge_connect_P.mat'
data_FC_ext = mt.loadmat(data_path)
for k_ex, v_ex in data_FC_ext.items():
c_ex = k_ex
correlation_matrix = np.mat(v_ex)
np.save('correlation_matrix.npy',correlation_matrix)
#方法二
from scipy.io import loadmat
coords = loadmat('E:/structural_network/coords_MNI_AAL90.mat')
coords = coords['coords_MNI_AAL90'
np.save('coords.npy',coords)

49、在python中,读取npy文件,并存为.mat文件,代码如下:

import numpy as np
import scipy.io as sio
mat = np.load('F:/coupling_FC_SC_ASD/child_adol/toConsesusNet_stru/adol_SCN_asd_group_binary.npy')
sio.savemat('adol_SCN_asd_group_binary.mat',{'adol_SCN_asd_group_binary':mat})

50、在python中,读取Excel文件,代码如下:

path_H_2 = 'E:/project//H_reg_site/H_2_reg_site.xlsx'
H_1_reg = pd.read_excel(io=path_H_1, sheet_index=0, header=0)

51、在python中,读取比较大的.mat文件,代码如下:

     data = h5py.File(path,'r')

52、在python中,通过Anaconda Prompt窗口运行python的代码:

学习笔记1—python基础_双引号

52、在python中,删除数据中某行或者某列:scale = np.delete(scale,[9,10],axis = 1) # axis =0 删除9,10行,axis = 1 删除9,10列,

53、python中 list 与数组的互相转换:(1) list转array np.array(a);(2)array 转list a.tolist()

54、python中 numpy.shape用法:(1) y.shape[0];(2) np.shape(y)[0] (注意:y为数组,0表示第一维度)

55、python中 两个向量拼接用法:(1) 行拼接:np.vstack(());(2) 列拼接:np.hstack(()), 比如:X_test_new = np.hstack((fea_1_test,fea_2_test,fea_3_test))。

56、python中 计时器代码:import time; time_start = time.time(), time_end = time.time(); time_c = time_end - time_start; print('time cost', time_c,'s')。

57、python中 独立样本t检验和配对t检验:from scipy.stats import ttest_rel, ttest_ind; ttest, pval = ttest_rel(X_train_asd,X_train_td) ;ttest, pval = ttest_ind(X_train_asd,X_train_td,equal_var = 'false')。


意在交流学习,欢迎点赞评论🙏, 如有谬误,请联系指正。转载请注明出处。