Python学习问题总结

1.ModuleNotFoundError: No module named 'numpy'

原因:python环境没有安装numpy包

解决方案:通过pip install numpy安装

补充:在cmd环境下已经安装了numpy包,但在pycharm环境中提示No module named 'numpy'

解决方案:在pycharm环境中file->setting->project interpreter中没有numpy包,按下图,安装numpy包

2.安装python包慢

原因:pip安装默认使用的源为https://pypi.python.org/simple,国内网络访问慢

解决方案:修改pip默认安装源为国内源

http://pypi.douban.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
http://pypi.hustunique.com/simple/ 华中理工大学
http://pypi.sdutlinux.org/simple/ 山东理工大学
http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学

https://pypi.tuna.tsinghua.edu.cn/simple 清华

在window下,在C:\Users\用户名  目录下创建pip目录,在pip目录下创建pip.ini配置文件,在配置文件内写入如下内容:

[global]  
index-url=http://mirrors.aliyun.com/pypi/simple/  
[install]  

trusted-host=mirrors.aliyun.com

pycharm环境中,在file->setting->project interpreter中修改pip源配置

Anaconda国内源配置:

在C:\Users\<你的用户名> 下创建配置文件.condarc,写入如下内容

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
show_channel_urls: true

3.%matplotlib inline报错

%matplotlib inline:在jupyter notebook环境中,显示出打印信息

只在notebook环境下运行才需要加%matplotlib inline,正常py代码里会报错

4.去除字符串中标点符号

import re
import string
string = “Hello, how are you!”

print(re.sub(r'[{}]+'.format(string.punctuation),"",string))

print(re.sub("[+\.\!\/_,$%^*(+\"\']+|[+——!,。??、~@#¥%……&*()]+","",string))

5.矩阵相乘

import numpy as np
# 2-D array: 2 x 3
two_dim_matrix_one = np.array([[1, 2, 3], [4, 5, 6]])
# 2-D array: 3 x 2
two_dim_matrix_two = np.array([[1, 2], [3, 4], [5, 6]])
two_multi_res = np.dot(two_dim_matrix_one, two_dim_matrix_two)
print('two_multi_res: %s' %(two_multi_res))

 

6.len使用

 

n_greater_50k = len(data[data['income']=='>50K'].index)

等价于:

n_greater_50k = 0
for info in data['income']:
    if info == '>50K':
        n_greater_50k += 1

7.将二分类数据转换0/1编码

#方法1
labels = {'<=50K':0,'>50K':1}
income = income_raw.map(labels)
#方法2
income = (income_raw=='>50K').astype(int)
#方法3
income = income_raw.apply(lambda x: 0 if x == '<=50K' else 1)

8.将字符串转换为小写

for i in documents:
    lower_case_documents.append(i.lower())

9.运行jupyter notebook ***报错

ValueError: Please install nodejs 5+ and npm before continuing installation. nodejs may be installed using conda or directly from the nodejs website.

安装Nodejs,去https://nodejs.org/en/下载安装8.11.2LTS

10.获取字典中最值对应的键

dict = {'u': -1, 'r': 0, 'd': 0, 'l': 0}
max(dict,key=dict.get) #参数key=不能少
或
max(dict.items(), key=lambda x: x[1])
min(dict, key=lambda x: d[x])

11.判断字符串是否存在子串

string = 'hello world'
if 'world' in string:
if string.find(’world‘) == 5:
if string.index(’world‘) > -1:

12. 获取字典大小

keys = dict.keys()
len(keys)

13. 装饰器和符号@

在代码运行期间动态增加功能的方式,称之为“装饰器”。装饰器本质是高阶函数, 就是将函数作为参数进行相应运作,最后返回一个闭包代替原有函数. 装饰器本质就是将原函数修饰为一个闭包(一个返回函数).

装饰器在python中在函数/对象方法定义前使用@符号调用. 装饰器可以在函数运行前进行一些预处理, 例如检查类型等.

@dec1
@dec2(arg1,arg2)
def test(arg):
    pass

以上代码等于dec1(dec2(arg1,arg2)(test(arg)))

参考:http://gohom.win/2015/10/25/pyDecorator/

14. 读取和写入cvs文件

# 读取cvs文件
with open(filename) as f:
    reader = csv.reader(f)
    # 读取一行,下面的reader中已经没有该行了
    head_row = next(reader)
    for row in reader:
        # 行号从2开始
        print(reader.line_num, row)

# 写入cvs文件
with open('example.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    for row in datas:
        writer.writerow(row)
        
    # 还可以写入多行
    writer.writerows(datas)

15. numpy自动生成数组

# 1. np.arange():通过指定开始值,终值和步长来创建表示等差数列的一维数组,不包含终值
>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(0,1,0.1)
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

# 2. np.linspace():通过指定开始值,终值和元素个数来创建表示等差数列的一维数组,含有参数endpoint布尔值,默认为True表示包含终值,设定为False表示不包含终值。
>>> np.linspace(0,1,10)
array([ 0.    , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
    0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.    ])
>>> np.linspace(0,1,10,endpoint = False)
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

# 3. np.logspace():通过指定开始值,终值和元素个数来创建表示等等比列的一维数组,基数默认为10,可以使用base参数指定基数
>>> np.logspace(0,4,5)
array([ 1.00000000e+00,  1.00000000e+01,  1.00000000e+02,
     1.00000000e+03,  1.00000000e+04])
>>> np.logspace(0,3,5,base = 2)
array([ 1. , 1.68179283, 2.82842712, 4.75682846, 8. ])
起点为2^0 = 1,终点为2^3 = 8,一共按照等比数列生成5个点,这样公比q = 2^(3/4)

# 4. np.zeros(),np.ones(),np.empty(),创建指定的形状和类型数组,np.empty只分配数组所使用的内存,不对数据初始化起作用
# 5. np.full():生成初始化为指定值的数组
>>> np.empty((2,3),np.int32)
array([[ 8078112, 37431728, 8078112],
    [47828800, 47828712,    10]])
>>> np.ones(4)
array([ 1., 1., 1., 1.])
>>> np.ones((2,3))
array([[ 1., 1., 1.],
    [ 1., 1., 1.]])
>>> np.ones(4,dtype = np.bool)
array([ True, True, True, True], dtype=bool)
>>> np.zeros(4,dtype = np.bool)
array([False, False, False, False], dtype=bool)
>>> np.zeros(4)
array([ 0., 0., 0., 0.])
>> np.full(4,np.pi)
array([ 3.14159265, 3.14159265, 3.14159265, 3.14159265])
>>> np.full((2,3),np.pi)
array([[ 3.14159265, 3.14159265, 3.14159265],
    [ 3.14159265, 3.14159265, 3.14159265]])

# 6. np.zeros_like(),np.ones_like():创建于参数形状相同的数组即np.zeros_like(a)与np.zeros(a.shape,dtype = a.type)相同
>>> a = np.arange(10).reshape(2,5)
>>> np.zeros_like(a)
array([[0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]])

# 7. np.fromfunction():从指定的函数中生成数组,第一个参数是函数名称,第二个参数是数组形状
>>> np.fromfunction(lambda a,b:a == b,(3,3))
array([[ True, False, False],
    [False, True, False],
    [False, False, True]], dtype=bool)
>>> np.fromfunction(lambda i:i%7 +1,(10,))
array([ 1., 2., 3., 4., 5., 6., 7., 1., 2., 3.])

# 8. xrange():用法与 range 完全相同,所不同的是生成的不是一个数组,而是一个生成器。
xrange(start, stop, step)
from six.moves import xrange
>> xrange(8)
xrange(8)
>>> list(xrange(8))
[0, 1, 2, 3, 4, 5, 6, 7]
>>> list(xrange(3,5))
[3, 4]
>>> list(xrange(0,6,2))
[0, 2, 4]

16. 生成随机数

# 1. random.random():生成一个0-1之间的随机浮点数

# 2. random.uniform(a, b):生成[a,b]之间的浮点数

# 3. random.randint(a, b):生成[a,b]之间的整数

# 4. random.randrange(a, b, step):在指定的集合[a,b)中,以step为基数随机取一个数.如random.randrange(0, 20, 2),相当于从[0,2,4,6,...,18]中随机取一个

# 5. random.choice(sequence):从特定序列中随机取一个元素,这里的序列可以是字符串,列表,元组等
string = 'nice to meet you'
tup = ('nice', 'to', 'meet', 'you')
lst = ['nice', 'to', 'meet', 'you']
dic = {'a': 1, 'b': 2, 'c': 3}
print random.choice(string)
print random.choice(tup)
print random.choice(lst)

# 6. random.shuffle(lst):将列表中的元素打乱,洗牌

# 7. random.sample(sequence,k):从指定序列中随机抽取不重复定长序列,原始序列不变
string = 'nice to meet you'
tup = ('nice', 'to', 'meet', 'you')
lst = ['nice', 'to', 'meet', 'you']
dic = {'a': 1, 'b': 2, 'c': 3}
print random.sample(string, 2)
print random.sample(tup, 2)
print random.sample(lst, 2)
'''
['m', 'i']
['you', 'nice']
['nice', 'to']
'''

17. 从0-100中随机抽取10个不重复的数字

random_list = np.arange(100).tolist()
random_result = random.sample(random_list ,10)
print(random_result )

18. list和array相互转换

array = np.array(list, dtype = int)
list = array.tolist()

19. 列表排序reverse sort sorted

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

# sort
a = [5,7,6,3,4,1,2]
b = a.sort()
print(a)
print(b)
# [1, 2, 3, 4, 5, 6, 7]
# None

# sorted
a = [5,7,6,3,4,1,2]
b = a.sorted()
print(a)
print(b)
# [5, 7, 6, 3, 4, 1, 2]
# [1, 2, 3, 4, 5, 6, 7]

20 array增加列和删除列

# 增加一列
np.expand_dims(tensor, axis=0)

# 删除一列
dataset=[[1,2,3],[2,3,4],[4,5,6]]
dataset = np.delete(dataset, -1, axis=1)
print(dataset)
#array([[1, 2],
       [2, 3],
       [4, 5]])

# 删除多列
arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
arr = np.delete(arr, [1,2], axis=1)
print(arr)
#array([[ 1,  4],
       [ 5,  8],
       [ 9, 12]])

21. 获取文件名

filename = 'cache/upload_file/test/1.jpg.npy'
basename = os.path.basename(filename).split('.')[0]

22.tqdm显示进度条

with open('valid_data.csv', 'w', newline='') as f:
    #tqdm为python进度条
    writer = csv.writer(f)
    head = ['name','width','length','ratio']
    writer.writerow(head)
    for file in tqdm(valid_files):
        row = []
        img = image.load_img(file)
        row.append(file)
        row.append(img.width)
        row.append(img.height)
        row.append(img.width/img.height*100)
        writer.writerow(row)

23. linspace产生等差数列

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
np.linspace(1, 10, 10)
array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

np.linspace(1, 10, 10, endpoint = False)
array([ 1. ,  1.9,  2.8,  3.7,  4.6,  5.5,  6.4,  7.3,  8.2,  9.1])

np.linspace(1, 10, 10, endpoint = False, retstep= True)
(array([ 1. ,  1.9,  2.8,  3.7,  4.6,  5.5,  6.4,  7.3,  8.2,  9.1]), 0.9)

24. isinstance() 和type()判断一个对象是否是一个已知的类型

  • type() 不会认为子类是一种父类类型,不考虑继承关系。

  • isinstance() 会认为子类是一种父类类型,考虑继承关系

25. 可视化 matplotlib.pyplot

import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(1, 4, i+1, xticks=[], yticks=[])
ax.imshow(filters[i], cmap='gray')
ax.set_title('Filter %s' % str(i+1))

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值