Numpy Exercises

这是一组关于Numpy的练习,涵盖了矩阵运算、求解线性系统、计算范数、幂迭代、奇异值及最近邻算法等主题。
摘要由CSDN通过智能技术生成

Exercise 9.1: Matrix operations

import numpy
from scipy.linalg import toeplitz

A = []
for i in range(0, 200):
    x = numpy.random.normal(size=500)
    A.append(x)
A = numpy.array(A)
B = toeplitz([i for i in range(0, 500)])
print(A + A)
print(numpy.dot(A, A.T))
print(numpy.dot(A.T, A))
print(numpy.dot(A, B))

def calculate(A, B, I, namuda):
    return numpy.dot(A, B - namuda * I)

Exercise 9.2: Solving a linear system

import numpy
from scipy.linalg import toeplitz

A = []
for i in range(0, 200):
    x = numpy.random.normal(size=500)
    A.append(x)
A = numpy.mat(A)
B = numpy.mat(toeplitz([i for i in range(0, 500)]))
b = numpy.array([i for i in range(0, 500)])
x = numpy.dot(b, B.I)
print(x)

Exercise 9.3: Norm

import numpy
from scipy.linalg import toeplitz, svdvals

A = []
for i in range(0, 200):
    x = numpy.random.normal(size=500)
    A.append(x)
A = numpy.mat(A)
B = numpy.mat(toeplitz([i for i in range(0, 500)]))
Frobenius_norm = numpy.linalg.norm(A, 'fro')
infinity_norm = numpy.linalg.norm(B, numpy.inf)
value = svdvals(B)
largest_singular_value = max(value)
smallest_singular_value = min(value)

print(Frobenius_norm)
print(infinity_norm)
print(largest_singular_value)
print(smallest_singular_value)

Exercise 9.4: Power iteration

import numpy
import time

Z = []
for i in range(0, 200):
    x = numpy.random.normal(size=200)
    Z.append(x)
Z = numpy.mat(Z)

def max_abs(xn):
    return abs(xn).max()

time.clock()
x = numpy.array([1 for i in range(0, 200)])
tol = 0.1
xn = numpy.dot(Z, x.reshape(200, 1))
xn = xn.reshape(1, 200)
xp = x
while max_abs(xn - xp)> tol:
    xp = xn
    y = max_abs(xn)
    xn = xn / y
    xn = numpy.dot(Z, xn.reshape(200, 1))
    xn = xn.reshape(1, 200)
eigenvalue = 1 / max_abs(xn)
print(eigenvalue)
print(time.clock(), 'seconds')

Exercise 9.5: Singular values

import numpy
import random
from scipy.linalg import svdvals

def random_num():
    if random.randint(1, 10) < 6:
        return 1
    else:
        return 0

C = []
for i in range(0, 200):
    x = []
    for j in range(0, 200):
        x.append(random_num())
    C.append(x)
C = numpy.mat(C)
largest_singular_value = max(svdvals(C))
print(largest_singular_value)

关系:largest_singular_value = n * p

Exercise 9.6: Nearest neighbor

import numpy

def NearestNeighber(z, A):
    tmp = []
    for i in range(0, len(A)):
        tmp.append(abs(A[i] - z))
    return A[numpy.argmin(tmp)]

Programming is an eminently learnable skill that gives you unrivalled problem-solving power you can apply in all areas of life. It’s also a fun, creative activity that provides insight into how we control the devices that influence virtually every aspect of our lives. How to Program teaches you one of the world’s most accessible and powerful computer languages, Python. Learning a new language opens a wealth of opportunities. But there’s one language family that provides benefits like no other: the languages of computer programming. Now widely taught in schools—even in elementary schools—programming is an eminently learnable skill that gives you unrivalled problem-solving power you can apply in all areas of life. Programming is also a fun, creative activity that imparts deep insights into how we control the devices that influence virtually every aspect of our lives. Writing computer code has truly gone mainstream in recent years. Simple, general-purpose computer languages that resemble English can be readily used by anyone, thanks to fundamental building blocks that allow even complete beginners to write short pieces of working code, while also taking the mystery and complexity out of more complicated scripts. Remarkable advances in hardware and in user interfaces mean that skills that were once highly technical, complicated, and difficult to learn are today within the reach of everyone who is willing to engage with a computer. And now a pathbreaking guide is available with How to Program: Computer Science Concepts and Python Exercises. These 24 engaging and information-rich half-hour lessons use one of the world’s most accessible, popular, and powerful computer languages, Python 3, as a gateway to the universe of programming. Taught by Professor John Keyser of the Department of Computer Science and Engineering at Texas A&M University, one of the top-ranked computer science programs in the country, this unique video course offers the following advantages: From the very first lesson, Professor Keyser plunges you into Python coding and the concepts of computer science, with a friendly and accessible style that has won him numerous teaching awards. The Python computer language (named after the comedy troupe Monty Python) is ideal for beginners, with code based on ordinary English words and the flexibility to create many useful and creative programs. The course covers fundamental ideas with clarity and depth, teaching you programming from the most basic commands to the techniques that help you develop ambitious pieces of software. Professor Keyser focuses on practical problem-solving, presenting dozens of real-life examples and exercises, walking you through solutions, and helping you practice and build your skills. Following some of the lessons, Professor Keyser leads you through supplementary problems that reinforce key programming strategies. In addition, the guidebook that accompanies the course features dozens of additional drills and practice exercises, always with answers, together with a reference section that includes definitions of computer science terms, important Python commands, and other useful information. No matter what level of experience and skill you have with computers, you can rest assured that this course will suit your needs from the first step: walking you through how to install Python 3 and the programming editor PyCharm, both of which are available free online.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值