函数/库/框架/function/library
————————————————
关于删除数据集中含有NaN、inf、-inf 等异常值,有很多种方法,在stackoverflow的这个问题下,高票回答中使用的方法
df[~df.isin([np.nan, np.inf, -np.inf]).any(1)]
————————————————
用 pandas 读取 csv 浮点数字
data = pd.read_csv('15topic.csv',
dtype={'High': np.float64, 'Low': np.float64})
————————————————
xgboost 安装方法
Python Package Introduction — xgboost 2.1.0-dev documentation
————————————————
sklearn 包里面的 cross_validation这个包早就不在使用了,划分到了model_selection这个包中。
————————————————
把 random forest 输出位 .c 程序
————————————————
python 读取 matlab 存储的 .mat 文件
from scipy.io import loadmat
Data = loadmat(‘filename.mat’)
读出来的 Data 是个 dictionary 数据结构
Data.keys()
读取 Data 里存储的数据名称, label
Data[“label”] 显示 该 label 的数据
————————————————
python的list变量保存为txt文本
python的一些中间变量,如list变量保存为txt文件:
file = open('file_name.txt','w');
file.write(str(list_variable));
file.close();
原文链接:https://blog.csdn.net/qq1491599481/java/article/details/80017493
————————————————
打开python数组的一列
print(array[ : , 1])
▽▼▽▼▽▼ ▽▼▽▼▽▼
python 2 生成的 .pkl 文档,有可能不能被 python 3 打开,gate 服务器上安装有 python2,现在python2 中读取 pkl 文档,让后存储为 txt 或者 csv 文件,可由 matlab 打开
- import cPickle as pickle
f = open('path')
info = pickle.load(f)
print info #show file - ▽▼▽▼▽▼ ▽▼▽▼▽▼
或者这样打开 python3打开pkl文件-CSDN博客 - import pickle
- unpickled_filename = pickle.load(open(“filename.pkl", 'rb'), encoding='utf-8')
python2: import numpy; numpy.savetxt(‘filename.csv’, variable);
>>> for i in range(len(info[:,10])):
... if info[i,10]=='non_disrupted':
... info[i,10]=1
... else:
... info[i,10]=0
...
>>> numpy.savetxt('TRAIN_DENSITY_LIMIT.txt',info)
>>> f=open('TRAIN_DENSITY_LIMIT.pkl')
>>> info=pickle.load(f)
>>> for i in range(len(info[:,10])):
... if info[i,10]=='non_disrupted':
... info[i,10]=1
...
>>> numpy.savetxt('TRAIN_DENSITY_LIMIT.txt',info)
python 面向对象编程,类 解读 博客
python 中的 size,shape,len, count
python中的size,shape,len,count_python size-CSDN博客
Python Tuple(元组) len()方法
torch.stack python 3.x - Pytorch - Stack dimension must be exactly the same? - Stack Overflow
import torch
a = torch.randn(1, 42, 1, 1)
b = torch.randn(1, 42, 1, 1)
ab = torch.stack((a, b), 0)
print(ab.shape)
# torch.Size([2, 1, 42, 1, 1])
ab = torch.cat((a, b), 0)
print(ab.shape)
# torch.Size([2, 42, 1, 1])
aab = torch.cat((a, ab), 0)
print(aab.shape)
# torch.Size([3, 42, 1, 1])
python 数据集基础:Python3 数据结构 | 菜鸟教程
squeeze : This helps you get rid of useless one dimension arrays like using [7,8,9] instead of [[[7,8,9]]] or [[1,2,3],[4,5,6]] instead of [[[[1,2,3],[4,5,6]]]]
np.random.seed(0) makes the random numbers predictable
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
With the seed reset (every time), the same set of numbers will appear every time.
If the random seed is not reset, different numbers appear with every invocation:
>>> numpy.random.rand(4)
array([ 0.42, 0.65, 0.44, 0.89])
>>> numpy.random.rand(4)
array([ 0.96, 0.38, 0.79, 0.53])
3 Ways to Write Text to a File in Python - Python and R Tips
在python程序里打开文档的时候,路径要写全
/Users/huwenhui/Desktop, 不要写成 ~/Desktop
否则 python 会无法识别
——————————————————————————————————————————————————————————————————————————————————
Pytorch
更新后,data 的用法有改变,变为以下,
参考pytorch 中torch.utils.data.TensorDataset()函数的更新-CSDN博客
import torch
import torch.utils.data as Data
x = torch.linspace(1,10,10)
y = torch.linspace(10,1,10)
torch_dataset = Data.TensorDataset(x ,y)
老用法为
torch_dataset = Data.TensorDataset (datatensor = x, target_tensor = y)
conda install tensorflow
终端:conda install spyder, 可以把spyder安装到anaconda环境中去,然后打开spyder,会跳出下面的窗口
终端:conda install tensorflow 安装成功
python: A = [[1 2 3 4] [5 6 7 8]]
A.reshape(-1) = [1 2 3 4 5 6 7 8]
matlab的神经网络是2阶算法,有时候比python的结果更好一些
mode = hybrid CNN+RNN
mode = simple RNN(LSTM)
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
—————————————————————————————————————————————————————————————————————————————————————
软件包安装
Warning: Variable 'XX_samples' cannot be saved to a MAT-file whose version
is older than 7.3.
To save this variable, use the -v7.3 switch.
Basic elements of python: functions, tuples, lists, mutability, recursion, dictionaries, objec-oriented programming
Some packages: Numpy, matplotlib, Scipy, tqdm, PyTorch
open pickle file
python - How to read pickle file? - Stack Overflow
with open('filename', 'rb') as f:
x = pickle.load(f)
Jupyter notebook 中 pythonNotes.py 是记笔记的文档
$ pip install 安装包(e.g. numpy, matplotlib)
$ pip uninstall 安装包
$ pip3 install 安装包
$ pip3 uninstall 安装包
找到某个字母在一个字符串中的位置index,可以用find函数实现, e.g.:
import string
str = string.ascii_lowercase
index_a = str.find(‘a’)
index_a = 0
如何把两个 list 变成一个词典, 特殊函数 zip, dict
>>> keys = ['a', 'b', 'c']
>>> values = [1, 2, 3]
>>> dictionary = dict(zip(keys, values))
>>> print(dictionary)
{'a': 1, 'b': 2, 'c': 3}