Last active
February 2, 2019 16:57
-
-
Save conquistadorjd/4b72e67fdf4a6627f422786bce2928eb to your computer and use it in GitHub Desktop.
python-08-numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
a=np.identity(3,dtype=np.float64) | |
print(a) | |
# [[ 1. 0. 0.] | |
# [ 0. 1. 0.] | |
# [ 0. 0. 1.]] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.ndarray'> | |
print(type(a[0][0])) | |
# <type 'numpy.float64'> | |
a=np.eye(3, 5, k=0,dtype=np.float64) | |
print(a) | |
# [[ 1. 0. 0. 0. 0.] | |
# [ 0. 1. 0. 0. 0.] | |
# [ 0. 0. 1. 0. 0.]] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.ndarray'> | |
print(type(a[0][0])) | |
# <type 'numpy.float64'> | |
a=np.ones((3, 5),dtype=np.float64) | |
print(a) | |
# [[ 1. 1. 1. 1. 1.] | |
# [ 1. 1. 1. 1. 1.] | |
# [ 1. 1. 1. 1. 1.]] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.ndarray'> | |
print(type(a[0][0])) | |
# <type 'numpy.float64'> | |
a=np.ones_like(np.array([[0,1,0,2,5],[1,2,3,4,4]]),dtype=np.float64) | |
print(a) | |
# [[1 1 1 1 1] | |
# [1 1 1 1 1]] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.ndarray'> | |
print(type(a[0][0])) | |
# <type 'numpy.float64'> | |
a=np.full((3, 5),22,dtype=np.float64) | |
print(a) | |
# [[ 22. 22. 22. 22. 22.] | |
# [ 22. 22. 22. 22. 22.] | |
# [ 22. 22. 22. 22. 22.]] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.ndarray'> | |
print(type(a[0][0])) | |
# <type 'numpy.float64'> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################################ | |
# name: numpy_data_linear.py | |
# desc: Genarate test data having linear relationship | |
# date: 2019-02-02 | |
# Author: conquistadorjd | |
################################################################################################ | |
import numpy as np | |
from matplotlib import pyplot as plt | |
print('*** Program Started ***') | |
n = 50 | |
x = np.arange(-n/2,n/2,1,dtype=np.float64) | |
r = np.random.uniform(10,10,(n,)) | |
y =np.sqrt(r*r - x*x) | |
print('x',x, type(x[0])) | |
print('y',y, type(y[0])) | |
plt.scatter(x,y,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Linear Relation") | |
plt.scatter(x,-y,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Linear Relation") | |
plt.grid(color='black', linestyle='--', linewidth=0.5,markevery=int) | |
# plt.xlim( -15, 15 ) # set the xlim to xmin, xmax | |
# plt.ylim( -15, 15 ) # set the ylim to ymin, ymax | |
plt.legend(loc=2) | |
plt.axis('scaled') | |
plt.show() | |
plt.savefig('scarrerplot-01.png') | |
print('*** Program ended ***') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################################ | |
# name: numpy_data_linear.py | |
# desc: Genarate test data having linear relationship | |
# date: 2019-02-02 | |
# Author: conquistadorjd | |
################################################################################################ | |
import numpy as np | |
from matplotlib import pyplot as plt | |
print('*** Program Started ***') | |
n = 50 | |
x = np.arange(-n/2,n/2,1,dtype=np.float64) | |
m = np.random.uniform(0.3,0.5,(n,)) | |
b = np.random.uniform(5,10,(n,)) | |
y = x*m +b | |
print('x',x, type(x[0])) | |
print('y',y, type(y[0])) | |
plt.scatter(x,y,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Linear Relation") | |
plt.grid(color='black', linestyle='--', linewidth=0.5,markevery=int) | |
plt.legend(loc=2) | |
plt.axis('scaled') | |
plt.show() | |
plt.savefig('numpy_data_linear.jpeg') | |
print('*** Program ended ***') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
######################### Random values in a given shape. | |
a=np.random.rand(2,3) | |
print(a) | |
# [[ 0.7524278 0.21176809 0.73990734] | |
# [ 0.28341776 0.11559792 0.15859365]] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.ndarray'> | |
print(type(a[0][0])) | |
# <type 'numpy.float64'> | |
####################### Return a sample from the standard normal distribution | |
a=np.random.randn(5) | |
print(a) | |
# [ 1.0000366 0.0906066 -0.05027158 -0.14745128 1.35046138] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.float64'> | |
#######################Return a sample from the standard normal distribution | |
a=np.random.randn(5,4) | |
print(a) | |
# [[ 1.48864593 -0.75508993 1.57585151 -0.02507804] | |
# [-1.11795072 0.16357727 0.76753395 0.02291213] | |
# [-1.39439533 0.66704929 -0.01020978 0.12887067] | |
# [-0.19386682 0.70650588 0.71049381 -0.40089744] | |
# [-0.6845585 0.35872981 0.18581329 -0.51889034]] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.float64'> | |
print(type(a[0][0])) | |
# <type 'numpy.float64'> | |
###############Return random integers from low (inclusive) to high (exclusive). | |
a=np.random.randint(2,14,size=5) | |
print(a) | |
# [12 9 7 3 9] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.int64'> | |
###############Return random floats in the half-open interval [0.0, 1.0). | |
a=np.random.random_sample(5) | |
print(a) | |
# [-0.20534297 0.4333096 0.94111548 -0.61324519 0.8843922 ] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.float64'> | |
###############Draw samples from a binomial distribution. | |
n,p=10,0.5 # number of trials, probability of each trial | |
a=np.random.binomial(n,p,100) | |
print(a) | |
# [5 2 5 6 5 3 6 8 5 7 3 4 8 4 9 5 5 6 3 5 7 6 6 2 6 5 6 6 5 3 6 5 6 6 4 6 2 | |
# 7 5 6 7 6 3 3 3 8 8 3 2 5 7 6 4 2 5 7 6 4 5 6 5 5 5 7 4 2 8 3 5 3 6 5 4 4 | |
# 3 3 5 7 7 4 4 6 4 5 6 7 5 5 6 6 4 7 4 4 3 2 6 6 7 3] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.int64'> | |
############### Draw samples from a uniform distribution. | |
a=np.random.uniform(5,15,(3,)) | |
print(a) | |
# [ 13.81416285 5.82087405 13.24553233] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.float64'> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################################ | |
# name: numpy_data_random_01.py | |
# desc: Genarate test data having linear relationship | |
# date: 2019-02-02 | |
# Author: conquistadorjd | |
################################################################################################ | |
import numpy as np | |
from matplotlib import pyplot as plt | |
print('*** Program Started ***') | |
n = 50 | |
x = np.arange(-n/2,n/2,1,dtype=np.float64) | |
y = a=np.random.uniform(-15,15,(n,)) | |
print('x',x) | |
print('y',y) | |
plt.scatter(x,y,s=None, marker='o',color='g',edgecolors='g',alpha=0.9,label="Random numbers") | |
plt.grid(color='black', linestyle='--', linewidth=0.5,markevery=int) | |
plt.legend(loc=2) | |
plt.axis('scaled') | |
plt.show() | |
plt.savefig('numpy_data_random_01.jpeg') | |
print('*** Program ended ***') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
a = np.array([1, 2, 3]) | |
print(a) | |
#[1 2 3] | |
print(type(a)) | |
#<type 'numpy.ndarray'> | |
b = np.array([[1, 2], [3, 4]]) | |
print(b) | |
#[[1 2] | |
# [3 4]] | |
################################################################# | |
c = np.arange(3) | |
print(c) | |
# [0 1 2] | |
print(type(c)) | |
# <type 'numpy.ndarray'> | |
print(type(c[0])) | |
# <type 'numpy.int64'> | |
d = np.arange(3.0) | |
print(d) | |
# [ 0. 1. 2.] | |
print(type(d)) | |
# <type 'numpy.ndarray'> | |
print(type(d[0])) | |
# <type 'numpy.float64'> | |
e = np.arange(5 ,10, 1,dtype=np.float64) | |
print(e) | |
# [ 5. 6. 7. 8. 9.] | |
print(type(e)) | |
# <type 'numpy.ndarray'> | |
print(type(e[0])) | |
# <type 'numpy.float64'> | |
################################################################## | |
a=np.linspace(2, 13, num=5,dtype=np.float64) | |
print(a) | |
# [ 2. 4.75 7.5 10.25 13. ] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.float64'> | |
a=np.logspace(1, 2, num=5,dtype=np.float64) | |
print(a) | |
# [ 10. 17.7827941 31.6227766 56.23413252 100. ] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.float64'> | |
a=np.geomspace(1, 2, num=5,dtype=np.float64) | |
print(a) | |
# [ 1. 1.18920712 1.41421356 1.68179283 2. ] | |
print(type(a)) | |
# <type 'numpy.ndarray'> | |
print(type(a[0])) | |
# <type 'numpy.float64'> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment