Python中基础使用及Numpy、Scipy、Matplotlib 使用教程

2023-11-13

本文主要根据 斯坦福CS231n课程的Python 教程进行整理,原文地址为http://cs231n.github.io/python-numpy-tutorial/,官方Python指南网址https://www.python.org/doc/

Python是本身是一个通用的编程语言,但其具有一些库(numpy,scipy,matplotlib)用于科学运算,原文的Python的版本是3.5。

本文先进行Python的基本介绍(数据、容器、函数、类)然后再介绍Numpy库、SciPy库以及MatPlotlib库的常用方法。

Python基本数据类型

  1. 整型 int ,浮点型 float (注:Python没有i++这种语句,乘方用x**i)
  2. 布尔型 booleans (注:Python 使用 and 、or、not替代C语言的&&、||、!;t!=f表示t和f的异或)
  3. 字符串型 strings (注:Python中字符串可以用单引号或双引号表示,一些常用的函数如大小写、去除空格、字符串替换、空格位置调整等,去掉空格s.strip()字符串替换s.replace('l','ell')具体使用什么函数可以现查)

Python容器

  • 列表 list,与数组相同但是可变大小(注:下标从0开始算,与maltab从1开始算不同)
xs = [3, 1, 2]    # Create a list
print(xs, xs[2])  # Prints "[3, 1, 2] 2" 注意小标从0开始
print(xs[-1])     # 负数下标从后往前数; prints "2"
xs[2] = 'foo'     # 列表的元素可以类型不同
print(xs)         # Prints "[3, 1, 'foo']"
xs.append('bar')  # 增加元素
print(xs)         # Prints "[3, 1, 'foo', 'bar']"
x = xs.pop()      # 删除最后一个元素
print(x, xs)      # Prints "bar [3, 1, 'foo']"

切片:list使用中的一个重要方法(注:切片的下标左边界包含,右边界不包含,与matlab两边界都包含不同)

nums = list(range(5))     # 利用list()函数生成列表
print(nums)               # Prints "[0, 1, 2, 3, 4]"
print(nums[2:4])          # 切片下标2到(4-1); prints "[2, 3]"
print(nums[2:])           # 切片下标2到最后; prints "[2, 3, 4]"
print(nums[:2])           # 切片开始到下标(2-1); prints "[0, 1]"
print(nums[:])            # 切片全部; prints "[0, 1, 2, 3, 4]"
print(nums[:-1])          # 切片开始到(-1-1); prints "[0, 1, 2, 3]"
nums[2:4] = [8, 9]        # 将下标2,3的值替换成8,9
print(nums)               # Prints "[0, 1, 8, 9, 4]"

 列表元素循环(与matlab相同)

animals = ['cat', 'dog', 'monkey']
for animal in animals:
    print(animal)
# 三行分别打印 cat dog monkey

也可以利用内建函数enumerate(Lists)取得下标(从0开始)和元素

animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
    print('#%d: %s' % (idx + 1, animal))
# Prints "#1: cat", "#2: dog", "#3: monkey", each on its own line

列表解析:一种生成新的列表的方式,举两个例子

nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print(squares)   # Prints [0, 1, 4, 9, 16]
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print(even_squares)  # Prints "[0, 4, 16]"
  • 字典 dictionaries,存储键值对(key,value)
d = {'cat': 'cute', 'dog': 'furry'}  # Create a new dictionary with some data
print(d['cat'])       # Get an entry from a dictionary; prints "cute"
print('cat' in d)     # Check if a dictionary has a given key; prints "True"
d['fish'] = 'wet'     # Set an entry in a dictionary
print(d['fish'])      # Prints "wet"
# print(d['monkey'])  # KeyError: 'monkey' not a key of d
print(d.get('monkey', 'N/A'))  # Get an element with a default; prints "N/A"
print(d.get('fish', 'N/A'))    # Get an element with a default; prints "wet"
del d['fish']         # Remove an element from a dictionary
print(d.get('fish', 'N/A')) # "fish" is no longer a key; prints "N/A"

字典循环

d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
    legs = d[animal]
    print('A %s has %d legs' % (animal, legs))
# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8 legs"

也可以利用内建函数d.items()取得键和值

d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.items():
    print('A %s has %d legs' % (animal, legs))
# Prints "A person has 2 legs", "A cat has 4 legs", "A spider has 8 legs"

字典解析,利用表达式生成字典

nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print(even_num_to_square)  # Prints "{0: 0, 2: 4, 4: 16}"
  • 集合sets(注:集合中元素无序且不重复)
animals = {'cat', 'dog'}
print('cat' in animals)   # Check if an element is in a set; prints "True"
print('fish' in animals)  # prints "False"
animals.add('fish')       # Add an element to a set
print('fish' in animals)  # Prints "True"
print(len(animals))       # Number of elements in a set; prints "3"
animals.add('cat')        # Adding an element that is already in the set does nothing
print(len(animals))       # Prints "3"
animals.remove('cat')     # Remove an element from a set
print(len(animals))       # Prints "2"

集合循环,与数组循环语法相同,但是注意其无序性。

animals = {'cat', 'dog', 'fish'}
for idx, animal in enumerate(animals):
    print('#%d: %s' % (idx + 1, animal))
# Prints "#1: fish", "#2: dog", "#3: cat"

集合解析:利用解析表达式生成集合

from math import sqrt
nums = {int(sqrt(x)) for x in range(30)}
print(nums)  # Prints "{0, 1, 2, 3, 4, 5}"
  • 元组 tuple,与数组类似,但其实不可变的列表,因此可以作为字典dictionary中的键key或集合set的元素(注:列表由于可变无法成为dictionary的key和set的元素)
d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys
t = (5, 6)        # Create a tuple
print(type(t))    # Prints "<class 'tuple'>"
print(d[t])       # Prints "5"
print(d[(1, 2)])  # Prints "1"

Python函数

利用关键字def,返回使用return

def sign(x):
    if x > 0:
        return 'positive'
    elif x < 0:
        return 'negative'
    else:
        return 'zero'

for x in [-1, 0, 1]:
    print(sign(x))
# Prints "negative", "zero", "positive"

参数可以使可选的的例子

def hello(name, loud=False):
    if loud:
        print('HELLO, %s!' % name.upper())
    else:
        print('Hello, %s' % name)

hello('Bob') # Prints "Hello, Bob"
hello('Fred', loud=True)  # Prints "HELLO, FRED!"

Python类

定义类的Python语法的格式如下

class Greeter(object):

    # Constructor
    def __init__(self, name):
        self.name = name  # Create an instance variable

    # Instance method
    def greet(self, loud=False):
        if loud:
            print('HELLO, %s!' % self.name.upper())
        else:
            print('Hello, %s' % self.name)

g = Greeter('Fred')  # 构造Greeter类的实例
g.greet()            # 调用类函数;prints "Hello, Fred"
g.greet(loud=True)   # 调用类函数; prints "HELLO, FRED!"

Numpy

numpy库用于python科学计算,与matlab有相似之处,其核心是对数组arrays的操作。

数组arrays

数组array是numpy提供的容器,能够通过列表list生成,利用方括号取元素(注:下标从0开始)

import numpy as np

a = np.array([1, 2, 3])   # Create a rank 1 array
print(type(a))            # Prints "<class 'numpy.ndarray'>"
print(a.shape)            # Prints "(3,)"
print(a[0], a[1], a[2])   # Prints "1 2 3"
a[0] = 5                  # Change an element of the array
print(a)                  # Prints "[5, 2, 3]"

b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array
print(b.shape)                     # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0])   # Prints "1 2 4"

与matlab类似,numpy提供了一些生成特殊数组的函数

import numpy as np

a = np.zeros((2,2))   # Create an array of all zeros
print(a)              # Prints "[[ 0.  0.]
                      #          [ 0.  0.]]"

b = np.ones((1,2))    # Create an array of all ones
print(b)              # Prints "[[ 1.  1.]]"

c = np.full((2,2), 7)  # Create a constant array
print(c)               # Prints "[[ 7.  7.]
                       #          [ 7.  7.]]"

d = np.eye(2)         # Create a 2x2 identity matrix
print(d)              # Prints "[[ 1.  0.]
                      #          [ 0.  1.]]"

e = np.random.random((2,2))  # Create an array filled with random values
print(e)                     # Might print "[[ 0.91940167  0.08143941]
                             #               [ 0.68744134  0.87236687]]"

数组切片,利用方括号内的逗号和冒号对每个维度进行索引,基本与matlab相同(注:左包括右不包括)

import numpy as np

# Create the following rank 2 array with shape (3, 4)
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
#  [6 7]]
b = a[:2, 1:3]

# A slice of an array is a view into the same data, so modifying it
# will modify the original array.
print(a[0, 1])   # Prints "2"
b[0, 0] = 77     # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1])   # Prints "77"

与matlab不同的是,切片时使用单个数字索引和使用冒号的索引的结果维度是不同的,例如

import numpy as np

# Create the following rank 2 array with shape (3, 4)
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# Two ways of accessing the data in the middle row of the array.
# Mixing integer indexing with slices yields an array of lower rank,
# while using only slices yields an array of the same rank as the
# original array:
row_r1 = a[1, :]    # Rank 1 view of the second row of a
row_r2 = a[1:2, :]  # Rank 2 view of the second row of a
print(row_r1, row_r1.shape)  # Prints "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape)  # Prints "[[5 6 7 8]] (1, 4)"

# We can make the same distinction when accessing columns of an array:
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print(col_r1, col_r1.shape)  # Prints "[ 2  6 10] (3,)"
print(col_r2, col_r2.shape)  # Prints "[[ 2]
                             #          [ 6]
                             #          [10]] (3, 1)"

这里有一个较为奇怪的整数索引的方式,但这种表达方便创建数组。

import numpy as np

a = np.array([[1,2], [3, 4], [5, 6]])

# An example of integer array indexing.
# The returned array will have shape (3,) and
print(a[[0, 1, 2], [0, 1, 0]])  # Prints "[1 4 5]"

# The above example of integer array indexing is equivalent to this:
print(np.array([a[0, 0], a[1, 1], a[2, 0]]))  # Prints "[1 4 5]"

# When using integer array indexing, you can reuse the same
# element from the source array:
print(a[[0, 0], [1, 1]])  # Prints "[2 2]"

# Equivalent to the previous integer array indexing example
print(np.array([a[0, 1], a[0, 1]]))  # Prints "[2 2]"
# Create a new array from which we will select elements
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])

print(a)  # prints "array([[ 1,  2,  3],
          #                [ 4,  5,  6],
          #                [ 7,  8,  9],
          #                [10, 11, 12]])"

# Create an array of indices
b = np.array([0, 2, 0, 1])

# Select one element from each row of a using the indices in b
print(a[np.arange(4), b])  # Prints "[ 1  6  7 11]"

# Mutate one element from each row of a using the indices in b
a[np.arange(4), b] += 10

print(a)  # prints "array([[11,  2,  3],
          #                [ 4,  5, 16],
          #                [17,  8,  9],
          #                [10, 21, 12]])

利用布尔表达式进行索引

import numpy as np

a = np.array([[1,2], [3, 4], [5, 6]])

bool_idx = (a > 2)   # Find the elements of a that are bigger than 2;
                     # this returns a numpy array of Booleans of the same
                     # shape as a, where each slot of bool_idx tells
                     # whether that element of a is > 2.

print(bool_idx)      # Prints "[[False False]
                     #          [ True  True]
                     #          [ True  True]]"

# We use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the True values
# of bool_idx
print(a[bool_idx])  # Prints "[3 4 5 6]"

# We can do all of the above in a single concise statement:
print(a[a > 2])     # Prints "[3 4 5 6]"

关于数组的索引就只介绍一小部分,当有需要时请查看官方文档深入学习。https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

数组的元素类型可以根据需求进行定义

import numpy as np

x = np.array([1, 2])   # Let numpy choose the datatype
print(x.dtype)         # Prints "int64"

x = np.array([1.0, 2.0])   # Let numpy choose the datatype
print(x.dtype)             # Prints "float64"

x = np.array([1, 2], dtype=np.int64)   # Force a particular datatype
print(x.dtype)                         # Prints "int64"

array的数学运算,(注意与matlab不同,乘除法是每一个位置的元素乘除法,不是矩阵运算)

import numpy as np

x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)

# Elementwise sum; both produce the array
# [[ 6.0  8.0]
#  [10.0 12.0]]
print(x + y)
print(np.add(x, y))

# Elementwise difference; both produce the array
# [[-4.0 -4.0]
#  [-4.0 -4.0]]
print(x - y)
print(np.subtract(x, y))

# Elementwise product; both produce the array
# [[ 5.0 12.0]
#  [21.0 32.0]]
print(x * y)
print(np.multiply(x, y))

# Elementwise division; both produce the array
# [[ 0.2         0.33333333]
#  [ 0.42857143  0.5       ]]
print(x / y)
print(np.divide(x, y))

# Elementwise square root; produces the array
# [[ 1.          1.41421356]
#  [ 1.73205081  2.        ]]
print(np.sqrt(x))

矩阵乘法采用dot

import numpy as np

x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

v = np.array([9,10])
w = np.array([11, 12])

# Inner product of vectors; both produce 219
print(v.dot(w))
print(np.dot(v, w))

# Matrix / vector product; both produce the rank 1 array [29 67]
print(x.dot(v))
print(np.dot(x, v))

# Matrix / matrix product; both produce the rank 2 array
# [[19 22]
#  [43 50]]
print(x.dot(y))
print(np.dot(x, y))

一些有用的矩阵运算方法,例如sum

import numpy as np

x = np.array([[1,2],[3,4]])

print(np.sum(x))  # Compute sum of all elements; prints "10"
print(np.sum(x, axis=0))  # Compute sum of each column; prints "[4 6]"
print(np.sum(x, axis=1))  # Compute sum of each row; prints "[3 7]"

当需要使用某些函数时,进行搜索https://docs.scipy.org/doc/numpy/reference/routines.math.html

矩阵转置

import numpy as np

x = np.array([[1,2], [3,4]])
print(x)    # Prints "[[1 2]
            #          [3 4]]"
print(x.T)  # Prints "[[1 3]
            #          [2 4]]"

# Note that taking the transpose of a rank 1 array does nothing:
v = np.array([1,2,3])
print(v)    # Prints "[1 2 3]"
print(v.T)  # Prints "[1 2 3]"

广播机制:numpy中一个重要而有用的机制,可以将小的矩阵进行扩展后与大矩阵进行运算,省for循环,提高运算速度。

import numpy as np

# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x)   # Create an empty matrix with the same shape as x

# Add the vector v to each row of the matrix x with an explicit loop
for i in range(4):
    y[i, :] = x[i, :] + v

# Now y is the following
# [[ 2  2  4]
#  [ 5  5  7]
#  [ 8  8 10]
#  [11 11 13]]
print(y)
import numpy as np

# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1))   # Stack 4 copies of v on top of each other
print(vv)                 # Prints "[[1 0 1]
                          #          [1 0 1]
                          #          [1 0 1]
                          #          [1 0 1]]"
y = x + vv  # Add x and vv elementwise
print(y)  # Prints "[[ 2  2  4
          #          [ 5  5  7]
          #          [ 8  8 10]
          #          [11 11 13]]"

还能更加简洁

import numpy as np

# We will add the vector v to each row of the matrix x,
# storing the result in the matrix y
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = x + v  # Add v to each row of x using broadcasting
print(y)  # Prints "[[ 2  2  4]
          #          [ 5  5  7]
          #          [ 8  8 10]
          #          [11 11 13]]"

我们整理一些广播broadcasting的规则:

1、两个数组的维数不相同,低维度的扩展维度

2、两个数组在某个维度上size相同或小的size是1

3、广播相当于沿着size为1的地方进行复制

下面用例子来展示一下广播

import numpy as np

# Compute outer product of vectors
v = np.array([1,2,3])  # v has shape (3,)
w = np.array([4,5])    # w has shape (2,)
# To compute an outer product, we first reshape v to be a column
# vector of shape (3, 1); we can then broadcast it against w to yield
# an output of shape (3, 2), which is the outer product of v and w:
# [[ 4  5]
#  [ 8 10]
#  [12 15]]
print(np.reshape(v, (3, 1)) * w)

# Add a vector to each row of a matrix
x = np.array([[1,2,3], [4,5,6]])
# x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3),
# giving the following matrix:
# [[2 4 6]
#  [5 7 9]]
print(x + v)

# Add a vector to each column of a matrix
# x has shape (2, 3) and w has shape (2,).
# If we transpose x then it has shape (3, 2) and can be broadcast
# against w to yield a result of shape (3, 2); transposing this result
# yields the final result of shape (2, 3) which is the matrix x with
# the vector w added to each column. Gives the following matrix:
# [[ 5  6  7]
#  [ 9 10 11]]
print((x.T + w).T)
# Another solution is to reshape w to be a column vector of shape (2, 1);
# we can then broadcast it directly against x to produce the same
# output.
print(x + np.reshape(w, (2, 1)))

# Multiply a matrix by a constant:
# x has shape (2, 3). Numpy treats scalars as arrays of shape ();
# these can be broadcast together to shape (2, 3), producing the
# following array:
# [[ 2  4  6]
#  [ 8 10 12]]
print(x * 2)

更多关于Numpy的内容,请看官方文件http://docs.scipy.org/doc/numpy/reference/

SciPy

SciPy库基于Numpy库,提供了许多操作数组array的函数,官方文件http://docs.scipy.org/doc/scipy/reference/index.html

图片操作

from scipy.misc import imread, imsave, imresize

# Read an JPEG image into a numpy array
img = imread('assets/cat.jpg')
print(img.dtype, img.shape)  # Prints "uint8 (400, 248, 3)"

# We can tint the image by scaling each of the color channels
# by a different scalar constant. The image has shape (400, 248, 3);
# we multiply it by the array [1, 0.95, 0.9] of shape (3,);
# numpy broadcasting means that this leaves the red channel unchanged,
# and multiplies the green and blue channels by 0.95 and 0.9
# respectively.
img_tinted = img * [1, 0.95, 0.9]

# Resize the tinted image to be 300 by 300 pixels.
img_tinted = imresize(img_tinted, (300, 300))

# Write the tinted image back to disk
imsave('assets/cat_tinted.jpg', img_tinted)

读写matlab文件

scipy.io.loadmat 与scipy.io.savemat,用到请看http://docs.scipy.org/doc/scipy/reference/io.html

’计算点间距离

import numpy as np
from scipy.spatial.distance import pdist, squareform

# Create the following array where each row is a point in 2D space:
# [[0 1]
#  [1 0]
#  [2 0]]
x = np.array([[0, 1], [1, 0], [2, 0]])
print(x)

# Compute the Euclidean distance between all rows of x.
# d[i, j] is the Euclidean distance between x[i, :] and x[j, :],
# and d is the following array:
# [[ 0.          1.41421356  2.23606798]
#  [ 1.41421356  0.          1.        ]
#  [ 2.23606798  1.          0.        ]]
d = squareform(pdist(x, 'euclidean'))
print(d)

Matplotlib

matplotlib.pyplot的作图模块,与matlab中的使用类似

import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)

# Plot the points using matplotlib
plt.plot(x, y)
plt.show()  # You must call plt.show() to make graphics appear.

多条曲线、图标题、图例、坐标等

import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()

与matlab相同,利用subplot分区域作图

import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)

# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')

# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')

# Show the figure.
plt.show()

显示图像

import numpy as np
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt

img = imread('assets/cat.jpg')
img_tinted = img * [1, 0.95, 0.9]

# Show the original image
plt.subplot(1, 2, 1)
plt.imshow(img)

# Show the tinted image
plt.subplot(1, 2, 2)

# A slight gotcha with imshow is that it might give strange results
# if presented with data that is not uint8. To work around this, we
# explicitly cast the image to uint8 before displaying it.
plt.imshow(np.uint8(img_tinted))
plt.show()

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python中基础使用及Numpy、Scipy、Matplotlib 使用教程 的相关文章

  • 如何将 typeshed 与 mypy 一起使用?

    我克隆了typeshed https github com python typeshed但我不知道如何告诉 mypy 使用它包含的类型提示 我在 mypy help 中没有看到任何选项 mypy 存储库确实包含对 typeshed 存储库
  • Django表单中的隐藏字段不在cleaned_data中

    我有这个表格 class CollaboratorForm forms Form user forms CharField label Username max length 100 canvas forms IntegerField wi
  • 获取父类名? [复制]

    这个问题在这里已经有答案了 class A object def get class self return self class class B A def init self A init self b B print b get cl
  • Django CollectStatic 启动大文件上传时管道损坏

    我正在尝试使用collectstatic将静态文件上传到我的S3存储桶 但我收到一个700k javascript文件的管道损坏错误 这就是错误 Copying Users wedonia work asociados server aso
  • 使用 cx_oracle 返回 MERGE 中受影响的行数

    如何在 CX Oracle 中执行 MERGE INTO sql 命令来获取受影响的行数 当我在cx oracle 上执行MERGE SQL 时 我得到的cursor rowcount 为 1 有没有办法获取受合并影响的行数 由于 cx o
  • 如何在 dash/plotly 中使用 iframe? (Python/HTML)

    我正在创建一个仪表板 我想使用这个交互式地图 网站链接 https www ons gov uk peoplepopulationandcommunity healthandsocialcare causesofdeath articles
  • os.walk 无需深入研究下面的目录

    我该如何限制os walk只返回我提供的目录中的文件 def dir list self dir name whitelist outputList for root dirs files in os walk dir name for f
  • 在Python中将月份和年份的列合并为季度和年份的列

    我有一个数据框 df Month 1 8 Year 2015 2020 df pd DataFrame data df df 想要将其转变为新列 期望的输出 df Month 1 8 Year 2015 2020 Quarter Q1201
  • 将 gtk.DrawingArea 保存到文件

    我想使用 PIL 将 gtk DrawingArea 对象内容保存到 jpeg 文件 我特别想添加这个脚本 http pygstdocs berlios de pygst tutorial webcam viewer html制作照片的可能
  • 构建wheel失败/“错误:INCLUDE环境变量为空”

    我正在使用 Python 2 7 11 并尝试 pip install 模块 但是其中一些模块失败了 我收到的消息是 无法为 X 构建轮子 和 错误 包含环境变量为空 我尝试安装 Scrapy LXML 和 Twisted 但都失败了 我尝
  • numpy.polyval() 的反函数

    我想知道 np polyval 是否有一个方便的反函数 我在其中给出 y 值并求解 x 我知道我可以做到这一点的一种方法是 import numpy as np Set up the question p np array 1 1 10 y
  • 如何循环遍历列表中除最后一项之外的所有项? [复制]

    这个问题在这里已经有答案了 Using a for循环 如何循环遍历列表中除最后一项之外的所有项 我想遍历一个列表 检查每个项目与后面的项目 我可以在不使用索引的情况下做到这一点吗 for x in y 1 If y是一个生成器 那么上面的
  • Python - 从一定范围内随机采样,同时避免某些值

    我一直在阅读有关random sample 函数在random模块 但没有看到任何可以解决我的问题的东西 我知道使用random sample range 1 100 5 会给我来自 人群 的 5 个独特样本 我想得到一个随机数range
  • Python httplib 和 POST

    我目前正在使用别人编写的一段代码 它用httplib向服务器发出请求 它以正确的格式提供所有数据 例如消息正文 标头值等 问题是 每次尝试发送 POST 请求时 数据都在那里 我可以在客户端看到它 但没有任何内容到达服务器 我已经阅读了库规
  • 从Python列表中挑选出具有特定索引的项目

    我确信在 Python 中有一种很好的方法可以做到这一点 但我对这门语言还很陌生 所以如果这是一个简单的方法 请原谅我 我有一个列表 我想从该列表中挑选某些值 我想要挑选的值是列表中索引在另一个列表中指定的值 例如 indexes 2 4
  • Python for 循环前瞻

    我有一个 python for 循环 其中我需要向前查看一项以查看在处理之前是否需要执行某项操作 for line in file if the start of the next line 0 perform pre processing
  • ValueError:序列太大;不能大于 32

    我写了这段代码 from Crypto Cipher import AES import numpy as np import cv2 base64 BLOCK SIZE 16 PADDING pad lambda s s BLOCK SI
  • pandas groupby 中两个系列的最大值和最小值

    是否可以从 groupby 中的两个系列中获取最小值和最大值 例如下面的情况 分组时c 我怎样才能得到最小值和最大值a and b同时 df pd DataFrame a 10 20 3 40 55 b 5 14 8 50 60 c x x
  • Python pandas:向我的数据框中添加一列来计算变量

    我有一个像这样的数据框 gt org group org1 1 org2 1 org3 2 org4 3 org5 3 org6 3 我想将列 count 添加到 gt 数据帧以计算组的成员数量 预期结果如下 org group count
  • Mac 无法安装 Tensorflow

    我检查了我的 pip3 和 python3 版本 tensorflow MacBook Pro de Hector 2 tensorflow hectoresteban pip3 V pip 10 0 1 from Users hector

随机推荐

  • PR/AE/FCPX比较好用的插件有哪些?

    Beauty Box 磨皮润肤美容插件 Digital Anarchy比较出众的一款视频磨皮美白降噪插件 支持系统 windows Mac 软件版本 PR AE CS6 2023 Davinci Resolve 达芬奇11以上 FCPX 1
  • 机器学习二:支持向量机

    支持向量机 1 介绍 2 对偶问题 3 非线性数据 3 1 核函数与核技巧 3 1 1 数学解释 3 1 2 几种常用的核函数 4 SVM 响应离群点 4 1 软间隔 4 2 正则化 4 3 参数调整 4 3 1 SVM C Paramet
  • OpenGL学习书籍推荐

    1 opengl 红宝书 2 Nehe的Opengl教程 网上的文章 能形成一个完整系列的就是 Nehe的 有点老 不过不影响学习理论 3 知乎上的这个帖子也提供了不少思路 https www zhihu com question 2416
  • 汉堡王什么汉堡好吃_汉堡王9款汉堡测评,牛肉和鸡肉你喜欢哪个?

    从第一次吃汉堡王到现在已经好久了 数了一下 他们家的汉堡我已经吃了9种了 虽然还没有全部吃过一遍 今天就来盘点一下汉堡王的汉堡吧 小皇堡 第一次吃的就是小皇堡 当时不是很能吃得惯 所以从那时起就有点不太敢尝试皇堡系列 后来真香了 里面有西红
  • [中奖]第九届“泰迪杯”挑战赛A题

    问题概述 题目1如下 赛题有2个点 分别是 确定数据指标 即确定哪些特征是决定财务造假与否的关键特征 预测造假公司 训练模型 然后跑测试数据即可 预处理 首先使用missingno2 对全局数据进行观测 看一看缺失值等情况 然后删去无用的特
  • retrofit应用详解与源码解析--源码解析

    本文出自门心叼龙的博客 属于原创类容 未经允许 不得转载 本专栏的同步视频教程已经发布到CSDN学院 https edu csdn net course detail 30408 上一篇文章我们通过12个小案例 给大家演示了retrofit
  • Ado.net批量插入数据

    采用的是SqlBulkCopy方法 数据库是sql server 示例代码地址 https gitee com Alexander360 LearnAdoNet SqlBulkCopy批量插入的方法如下 包括list转datatable方法
  • Python书写的格式规范

    Python书写的格式规范 1 英文版Python书写格式 2 中文版Python书写规范
  • C++之const类成员变量,const成员函数,const指针

    https www cnblogs com cthon p 9178701 html 结合下面这个链接观看更佳 讲常量指针和指向常量的指针的 https www cnblogs com lihuidashen p 4378884 html
  • CnOCR 使用教程

    目录 一 简介 二 使用教程 三 效果展示 一 简介 CnOCR 是 Python 3 下的文字识别 Optical Character Recognition 简称OCR 工具包 支持简体中文 繁体中文 部分模型 英文和数字的常见字符识别
  • Non-parseable settings C:\Users\xxxx.m2\settings.xml:错误 maven项目下载jar是空包解决方案

    在用maven项目下载我们要引入相对应的jar时候控制台报错 Non parseable settings C Users xxxx m2 settings xml expected START TA G or END TAG not TE
  • [python2.7版本] pip安装包 或者 pip升级pip版本出现此错误

    File tmp pip build vD3Ntt pip setup py line 7 def read rel path str gt str SyntaxError invalid syntax Command python set
  • 左手系和右手系转换最最最简便方法

    左手系和右手系转换最最最简便方法就是交换Y Z轴 只需一个矩阵变换 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 不需翻转Z轴 不需转置矩阵 不需修改缠绕方向 既可用于转换模型坐标 又可用于世界矩阵 视矩阵 实在是模型转换
  • 测试用例设计白皮书--边界值分析方法

    测试用例设计白皮书 边界值分析方法Author Vince 来源 http blog csdn net vincetest 一 方法简介1 定义 边界值分析法就是对输入或输出的边界值进行测试的一种黑盒测试方法 通常边界值分析法是作为对等价类
  • mysql日期查询

    今天 select from 表名 where to days 时间字段名 to days now 昨天 SELECT FROM 表名 WHERE TO DAYS NOW TO DAYS 时间字段名 lt 1 近7天 SELECT FROM
  • 开源服务器日志实时查看系统,开源日志管理系统

    开源日志管理系统 内容精选 换一换 鲲鹏工程师培训及认证为客户提供鲲鹏认证伙伴基于open系开源内核构建的商业软件培训 包含商业软件介绍 特性描述 操作使用 开发指导等内容 来自 其他 MindX DL Sample的系统架构如图1所示 各
  • nodejs typescript express mongodb 搭建简易服务器

    安装依赖 npm i express mongoose ts node typescript nodemon types express types node express session types express session 新建
  • java编译遇到的错误: 无法从静态上下文中引用非静态 变量 this

    记住这句话 静态方法不能引用非静态变量 我遇到的是因为将Student 放到了Test类当中去了 解决的办法 1 Student类写到Test外边去 2 Student定义为静态类 package cn sxt oo1 public cla
  • 设计模式----状态模式UML和实现代码

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 一 什么是状态模式 状态模式 State 定义 当一个对象的内在状态改变时允许改变其行为 这个对象看起来像是改变了其类 状态模式主要解决的是当控制一个对象状态的条件表达式过
  • Python中基础使用及Numpy、Scipy、Matplotlib 使用教程

    本文主要根据 斯坦福CS231n课程的Python 教程进行整理 原文地址为http cs231n github io python numpy tutorial 官方Python指南网址https www python org doc P