numpy
import numpy as np
test_1=np.array([92, 94, 88, 91, 87])
my_list = [1, 2, 3, 4, 5, 6]
my_array = np.array(my_list)
test_2 = np.genfromtxt('test_2.csv', delimiter=',')
# With a list
l = [1, 2, 3, 4, 5]
l_plus_3 = []
for i in range(len(l)):
l_plus_3.append(l[i] + 3)
# With an array
a = np.array(l)
a_plus_3 = a + 3
import numpy as np
test_1 = np.array([92, 94, 88, 91, 87])
test_2 = np.array([79, 100, 86, 93, 91])
test_3 = np.array([87, 85, 72, 90, 92])
test_3_fixed = test_3 + 2
total_grade=test_1+test_2+test_3_fixed
final_grade=total_grade/3
print(final_grade)
import numpy as np
porridge = np.array([79, 65, 50, 63, 56, 90, 85, 98, 79, 51])
cold = porridge[porridge < 60]
hot = porridge[porridge > 80]
just_right = porridge[(porridge > 60) & (porridge < 80)]
print(cold)
print(hot)
print(just_right)
a =
np.array([[92, 94, 88, 91, 87],
[79, 100, 86, 93, 91],
[87, 85, 72, 90, 92]])
a[2,1]
#第一列
a[:,0]
#第一行
a[0,:]
4.function
total_mean=np.mean(allergy_trials)
print(total_mean)
trial_mean=np.mean(allergy_trials,axis=1)
print(trial_mean)
patient_mean=np.mean(allergy_trials,axis=0)
print(patient_mean)
loc,
scale,size
) 随机返回一组正态分布的数组例子:
np.random.randn(5,3)
[[-0.11579098 1.16659392 0.30404131]
[ 0.62924988 -0.60270385 -1.04879158]
[-0.16612736 0.2497032 -0.59060274]
[ 0.91963005 0.67421963 0.6451157 ]
[ 0.58471986 -0.91288115 1.97703323]]
experiments=np.random.binomial(200,0.1,5000)
prob=np.mean(experiments<20)
print(prob)
import codecademylib
import numpy as np
from matplotlib import pyplot as plt
survey_responses = ['Ceballos', 'Kerrigan', 'Ceballos', 'Ceballos', 'Ceballos','Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Ceballos',
'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Ceballos', 'Ceballos', 'Ceballos', 'Ceballos',
'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Ceballos', 'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Ceballos',
'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Ceballos', 'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Kerrigan', 'Kerrigan', 'Ceballos', 'Ceballos', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Kerrigan', 'Ceballos']
total_ceballos=survey_responses.count('Ceballos')
print(total_ceballos)
percentage_ceballos=100*total_ceballos/len(survey_responses)
print(percentage_ceballos)
possible_surveys=np.random.binomial(70,0.54,size=10000)/70.
plt.hist(possible_surveys,range=(0,1),
bins=20)
plt.show()
ceballos_loss_surveys=np.mean(possible_surveys<0.5)
print(ceballos_loss_surveys)
large_survey=np.random.binomial(7000,0.54,10000)/7000.
ceballos_loss_new=np.mean(large_survey<0.5)
print(ceballos_loss_new)
从一组样本冲随机抽取size个成为新的样本