# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 1.numpy介绍

# +
import numpy as np

score = np.array(
[[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]]
)
# -

score

score.shape

# # 2.numpy的类型

import numpy as np
#创建一个一维数组
a = np.array([1,2,3,4])

a

import numpy as np
b = np.array([1],[2],[3],[4])

a.shape
a.size

a.ndim

b

b.shape

b.ndim

b.size


score


import numpy as np
score_one = np.ones([4,6],dtype=np.int32)
score_one

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

c

# # 3.从以生成数组中生成数组

now = np.array(score)
now

now_deep = np.asarray(score)
now_deep

score[0,0]=100


now

now_deep


# # 4.生成固定范围的数组  
#

import numpy as np
#等差数组  
a = np.linspace(0,100,11)
a

#等比数组  
b=np.logspace(0,100)
b

# +
#生成均值为1.55,标准差为0.8,数据量为1000的正态分布
import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(1.55,0.8,100000)

y = 1000

#创建画布
plt.figure(figsize=(20,8),dpi=100)


# 2）绘制直方图
plt.hist(x, y)

# 3）显示图像
plt.show()




# +
# 生成均匀分布的随机数
x1 = np.random.normal(1.75, 1, 100000000)

# 画图看分布状况
# 1）创建画布
plt.figure(figsize=(20, 10), dpi=100)

# 2）绘制直方图
plt.hist(x1, 1000)

# 3）显示图像
plt.show()

# +
# 生成均匀分布的随机数
#四只股票五天的涨幅 随机生成涨跌幅在某个正态分布内，比如均值0，方差1
stock_random = np.random.normal(0, 1,(4,5))

# 画图看分布状况
# 1）创建画布
plt.figure(figsize=(20, 10), dpi=100)

# 2）绘制直方图
plt.hist(stock_random, 1000)

# 3）显示图像
plt.show()
# -

stock_random

# +
import matplotlib.pyplot as plt

# 生成均匀分布的随机数
x2 = np.random.uniform(-1, 1, 100000000)

# 画图看分布状况
# 1）创建画布
plt.figure(figsize=(10, 10), dpi=100)

# 2）绘制直方图
plt.hist(x=x2, bins=1000)  # x代表要使用的数据，bins表示要划分区间数

# 3）显示图像
plt.show()

# +
import numpy as np  
import matplotlib.mlab as mlab  
import matplotlib.pyplot as plt  


# example data  
mu = 100 # mean of distribution  
sigma = 15 # standard deviation of distribution  
x = mu + sigma * np.random.randn(10000)  

num_bins = 50  
# the histogram of the data  
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='blue', alpha=0.5)  
# add a 'best fit' line  
y = mlab.normpdf(bins, mu, sigma)  
plt.plot(bins, y, 'r--')  
plt.xlabel('Smarts')  
plt.ylabel('Probability')  
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')  

# Tweak spacing to prevent clipping of ylabel  
plt.subplots_adjust(left=0.15)  
plt.show()  
# -



StarcoderdataJupyterScriptsDedupFiltered