python笔记

Numpy

切片

选择均匀间隔的元素/选择奇数偶数列

x = numpy.array([[ 1,  2,  3,  4,  5],
[ 6,  7,  8,  9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[6,  7,  8,  9, 10]])
#间隔三行取行数组
a=x[::4]
#a= [[1	2 3	4 5]
# [6 7 8 9 10]]


#间隔三列取列数组
b=a[:,::4]
# b = [[1	5]
#      [6	10]]

忽略0

a = numpy.array([0,1,2,0,2,3,4])
b = a[numpy.nonzero(a)]

where & mask

mask = np.where( (array_xya[:,1]> array_xya[:,1].max()-200 ) & (array_xya[:,0]>0.6) )
array = array_xya[mask]
#数组长度:array.shape[0]

np.array_split()与np.split()

np.array_split()可以进行不均等的划分,而np.split()只能进行均等划分。

import numpy as np
x = np.arange(8.0)

print np.array_split(x,3)
#输出:不均等划分结果
    [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]
print np.split(x,3)
#报错:一旦不均等就会报错
    'array split does not result in an equal division')
    ValueError: array split does not result in an equal division

求导,曲率计算

dx= np.gradient(x)
dy = np.gradient(y)
d2x = np.gradient(dx)
d2y = np.gradient(dy)
curvature = (np.abs(dx * d2y - d2x * dy) / (dx * dx + dy * dy)**1.5)*1000

文件读取

rasterio

import rasterio
Raster= "C:\\Users\\a\\Downloads\\2015.tif"	#多波段tif
with rasterio.open(Raster) as src:
     array = src.read()
array1 = array[0,::]	#第一个波段

读取文件夹下的子文件夹里的文件

import os
def get_filelist(dir):
    Filelist = []
    for home, dirs, files in os.walk(input_folder):
        for filename in files:
            # 文件名列表,包含完整路径
            Filelist.append(os.path.join(home, filename))
            # # 文件名列表,只包含文件名
            # Filelist.append( filename)
    return Filelist

if __name__ == "__main__": 
    input_folder = '....'
    file_list = get_filelist(input_folder)

平滑+拟合

loess平滑

f =0.35
yest = lowess_ag(x, y, f=f, iter=4)#拟合的y值

高斯拟合

H, A, x0, sigma = gauss_fit(x, y) #拟合的公式
# GaussLine = sigma*2.58+x0   
GaussLine = sigma*1.96+x0

Matplotlib

matplotlib图库

plt.scatter(array[:,1],array[:,0],color='red', marker='.',label='original values')
if line!=-9999:
    plt.plot([line,line],[0,0.8],color = 'blue')
plt.xlabel('dem')
plt.ylabel('ndvi')
plt.title('line')
plt.legend(loc=3) 
plt.show()    
以下是一份Python学习笔记: ### 全局变量 定义在函数体外部的变量,函数体内外都生效。示例代码如下: ```python # 定义变量money,值为50,并打印出来 money = 50 print("钱包还有:", money) # 买了一个冰淇淋花费10元 money = money - 10 print("买了一个冰淇淋花费10元,还剩余:", money, "元") ``` ### 变量赋值与命名规则 Python是动态类型语言,变量无需预先声明类型,直接赋值即可。变量名必须是字母、数字或下划线的组合,且不能以数字开头。示例: ```python # 整数变量 age = 25 # 字符串变量 name = "Alice" ``` ### 数据类型 - **数字类型(Number)** - **int**:整数(有符号),存放整数,如 -1, 10, 0 等。 - **float**:浮点数(有符号),存放小数,如 -3.13, 6.66。 - **complex**:复数,如 4 + 3j,以 j 结尾表示复数。 - **bool**:布尔,True 是真,False 是假,本质上是一个数字,True 记作 1,False 记作 0。 - **字符串(String)**:用引号引起来的数据都是字符串。 - **列表(List)**:有序的可变序列,Python 中使用最频繁的数据类型,可有序记录一堆数据。 - **元组(Tuple)**:有序的不可变序列,可有序记录一堆不可变的 Python 数据集合。 - **集合(Set)**:无序不重复集合,可无序记录一堆不重复的 Python 数据集合。 - **字典(Dictionary)**:无序 Key - Value 集合,可无序记录一堆 Key - Value 型的 Python 数据集合。 查看类型可使用 `type(被查看类型的数据)`,示例: ```python num = 10 print(type(num)) # 查看 num 的类型 ``` ### 迭代器 在 Python 中,迭代器(Iterator)是一种逐个访问数据集合的机制,它通过 `__iter__()` 和 `__next__()` 方法实现对数据的按需读取。迭代器的核心特性包括惰性计算、内存高效和统一的遍历接口,在处理大规模数据(如深度学习训练数据)时尤为重要 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prophet.Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值