大学python笔记_Introduction to Python课程笔记
首先恭喜自己终于通过了DATACAMP的第一个课程Introduction to Python,
课程讲义也上传到了百度云里,链接7天有效,需要的小伙伴们请提前保存。
提取码:0hr3
该课程主要分为四个章节:
Python Basics
Python Lists
Functions and Packages
NumPy
卡的比较久的几个代码主要是在没看清题目,又或者网络不好导致视频没仔细看,直接刷题。
重点如下:python区分大小写
变量不可以数字开头
python数据选取从0开始
[including:excluding]左闭右开,即最左边的区间选取,右边的区间不选取
python 不支持直接对list进行运算,所以需要用到np.array数组对列表数据进行运算。
即标准的列表list[1,2,3]+list[1,2,3]=list[1,2,3,1,2,3],而numpy中的列表可以通过运算符进行数学运算np_list[1,2,3]+np_list[1,2,3]=np_list[2,4,6]
因此多数列表操作使用要调用numpy数组,比如np.mean(),np.median()
2维列表2dnumpyarray,即list of list 中的数据选取规则如下,逗号分隔,:代表全选
data[:,0]标识第一列全选,data[0,:]标识第一行全选
部分通过代码如下:
List操作方法
# string to experiment with: place
place = "poolhouse"
# Use upper() on place: place_up
place_up=place.upper()
# Print out place and place_up
print(place,place_up)
# Print out the number of o's in place
print(place.count('o'))
list.append()一次只能添加一个元素
import math
math.pi即为常数π
也可以只加载包中的一个函数使得运行更快。
from math import pi
一般会用ticks 缩写包名,
如import numpy as np
由于python不支持列的操作,所以一般用numpy包来对列表(数组)进行运算。
python的列表进行+运算会相连
Numpy的列表进行+运算会求和
可以使用np.array(list)来得到一个numpy列表。
# Create list baseball
baseball = [180, 215, 210, 210, 188, 176, 209, 200]
# Import the numpy package as np
import numpy as np
# Create a numpy array from baseball: np_baseball
np_baseball=np.array(baseball)
# Print out type of np_baseball
print(type(np_baseball))
# height is available as a regular list
# Import numpy
import numpy as np
# Create a numpy array from height_in: np_height_in
np_height_in=np.array(height_in)
# Print out np_height_in
print(np_height_in)
# Convert np_height_in to m: np_height_m
np_height_m=np_height_in*0.0254
# Print np_height_m
print(np_height_m)
# height and weight are available as regular lists
# Import numpy
import numpy as np
# Create array from height_in with metric units: np_height_m
np_height_m=np.array(height_in)*0.0254
# Create array from weight_lb with metric units: np_weight_kg
np_weight_kg=np.array(weight_lb)*0.453592
# Calculate the BMI: bmi
bmi=np_weight_kg/(np_height_m**2)
# Print out bmi
print(bmi)
选取列表元素
# height and weight are available as a regular lists
# Import numpy
import numpy as np
# Calculate the BMI: bmi
np_height_m = np.array(height_in) * 0.0254
np_weight_kg = np.array(weight_lb) * 0.453592
bmi = np_weight_kg / np_height_m ** 2
# Create the light array
light=bmi<21
# Print out light
print(light)
# Print out BMIs of all baseball players whose BMI is below 21
print(bmi[light])
# height and weight are available as a regular lists
# Import numpy
import numpy as np
# Store weight and height lists as numpy arrays
np_weight_lb = np.array(weight_lb)
np_height_in = np.array(height_in)
# Print out the weight at index 50
print(weight_lb[50])
# Print out sub-array of np_height_in: index 100 up to and including index 110
print(np_height_in[100:111])
2d numpy arrays 可以看做List of list,最多可以有7维的列表
数据选取类似R语言中的DATAFRAME,都可以通过逗号分隔来选取,但是需要加 :
# Create baseball, a list of lists
baseball = [[180, 78.4],
[215, 102.7],
[210, 98.5],
[188, 75.2]]
# Import numpy
import numpy as np
# Create a 2D numpy array from baseball: np_baseball
np_baseball=np.array(baseball)
# Print out the type of np_baseball
print(type(np_baseball))
# Print out the shape of np_baseball
print(np_baseball.shape)
# baseball is available as a regular list of lists
# Import numpy package
import numpy as np
# Create np_baseball (2 cols)
np_baseball = np.array(baseball)
# Print out the 50th row of np_baseball
print(np_baseball[49,:])
# Select the entire second column of np_baseball: np_weight_lb
np_weight_lb=np_baseball[:,1]
# Print out height of 124th player
print(np_baseball[0:124,1])You managed to get hold of the changes in height, weight and age of all baseball players. It is available as a 2D numpy array, updated. Add np_baseball and updated and print out the result.
You want to convert the units of height and weight to metric (meters and kilograms respectively). As a first step, create a numpy array with three values: 0.0254, 0.453592 and 1. Name this array conversion.
Multiply np_baseball with conversion and print out the result.
# baseball is available as a regular list of lists
# updated is available as 2D numpy array
# Import numpy package
import numpy as np
# Create np_baseball (3 cols)
np_baseball = np.array(baseball)
# Print out addition of np_baseball and updated
print(np_baseball+updated)
# Create numpy array: conversion
conversion=np.array([0.0254,0.453592,1])
# Print out product of np_baseball and conversion
print(np_baseball*conversion)
# np_baseball is available
# Import numpy
import numpy as np
# Create np_height_in from np_baseball
np_height_in=np_baseball[:,0]
# Print out the mean of np_height_in
print(np.mean(np_height_in))
# Print out the median of np_height_in
print(np.median(np_height_in))
# np_baseball is available
# Import numpy
import numpy as np
# Print mean height (first column)
avg = np.mean(np_baseball[:,0])
print("Average: " + str(avg))
# Print median height. Replace 'None'
med = np.median(np_baseball[:,0])
print("Median: " + str(med))
# Print out the standard deviation on height. Replace 'None'
stddev = np.std(np_baseball[:,0])
print("Standard Deviation: " + str(stddev))
# Print out correlation between first and second column. Replace 'None'
corr = np.corrcoef(np_baseball[:,0],np_baseball[:,1])
print("Correlation: " + str(corr))
nparray选取子集
# heights and positions are available as lists
# Import numpy
import numpy as np
# Convert positions and heights to numpy arrays: np_positions, np_heights
np_heights=np.array(heights)
np_positions=np.array(positions)
# Heights of the goalkeepers: gk_heights
gk_heights=np_heights[np_positions=="GK"]
# Heights of the other players: other_heights
other_heights=np_heights[np_positions!="GK"]
# Print out the median height of goalkeepers. Replace 'None'
print("Median height of goalkeepers: " + str(np.median(gk_heights)))
# Print out the median height of other players. Replace 'None'
print("Median height of other players: " + str(np.median(other_heights)))
大学python笔记_Introduction to Python课程笔记相关推荐
- Yann Lecun纽约大学《深度学习》2020课程笔记中文版,干货满满!
关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! [导读]Yann Lecun在纽约大学开设的2020春季<深度学习>课 ...
- 【笔记3-6】CS224N课程笔记 - RNN和语言模型
CS224N(六)Recurrent Neural Networks and Language Models 语言模型 语言模型介绍 n-gram 基于窗口的神经语言模型 RNN RNN Loss a ...
- 【笔记3-7】CS224N课程笔记 - 神经网络机器翻译seq2seq注意力机制
CS224N(七)Neural Machine Translation, Seq2seq and Attention seq2seq神经网络机器翻译 历史方法 seq2seq基础 seq2seq - ...
- 收藏 | Yann Lecun纽约大学《深度学习》2020课程笔记中文版
点上方蓝字计算机视觉联盟获取更多干货 在右上方 ··· 设为星标 ★,与你不见不散 仅作分享,不代表本公众号立场,侵权联系删除 转载于:专知 AI博士笔记系列推荐 周志华<机器学习>手推笔 ...
- Python图形界面开发教程-课程笔记-2022-2-14
目录 1.1 写一个弹窗 1.2了解模板代码的组成 1.3 根据模板代码写一个界面 1.4 窗口关闭事件 1.5 通过字典获取返回值values 1.6 自定义主题 1.7 自定义窗口 1.8 布局和 ...
- 自学Python之Udacity28天入门课程笔记
第一节小乌龟和第一行代码 import turtle fred = turtle.Turtle() fred.color("red") fred.forward(100) fred ...
- 中国大学Mooc -《现代礼仪》课程笔记 (湖南大学袁涤非老师)
一 导论 礼仪的内涵与定义:约定俗成,共同认可的行为规范 礼仪的起源与发展 :简单化,国际化 礼仪的实质和原则:尊重,遵守,适度,自律 礼仪的特征与作用:继承性,差异性,针对性,规范性(握手,稍用力 ...
- 【原】机器学习公开课 目录(课程笔记、测验习题答案、编程作业源码)...持续更新......
之前看过的机器学习课程.本文是相关课程笔记.习题答案.作业源码的电梯. 1 Coursera 斯坦福机器学习课程,Andrew Ng 1.1 说明 课程地址和软件下载 Coursera连接不上(视频无 ...
- 【CS231n】斯坦福大学李飞飞视觉识别课程笔记(二):Python Numpy教程(2)
[CS231n]斯坦福大学李飞飞视觉识别课程笔记 由官方授权的CS231n课程笔记翻译知乎专栏--智能单元,比较详细地翻译了课程笔记,我这里就是参考和总结. [CS231n]斯坦福大学李飞飞视觉识别课 ...
最新文章
- [项目实施失败讨论Case] “凭心而论,在这家公司很敬业的工作了3年多,老板最后给我下的评语,大家都看看吧,千万别和我走同一条路!”(摘自csdn)...
- php执行一条insert插入两条数据其中一条乱码
- linux tar压缩解压命令的详细解释
- python从入门到精通需要多久-python学习从入门到精通要多久
- 加州理工学院公开课:机器学习与数据挖掘_过拟化
- 资源 | 我拿到了斯坦福、UCL、CMU、NYU的offer,关于博士申请你需要知道的一切...
- (day 001 - 进制转换) 405. 数字转换为十六进制数
- steam++加速问题:出现显示443端口被 vmware-hostd(9860)占用的错误。
- 进度图绘制十大注意事项
- 绝对干货!百度文档 用python一键下载
- Markdown表格——在CSDN上画表格
- [笑语天下]风景、照片与评论古今
- 准备结婚的朋友好好看看!这篇文章触动了十几万人!
- [web攻防] weblogic 漏洞复现 CVE-2017-10271CVE-2018-2628CVE-2018-2894
- IB学霸分享学习经验(家长如何助孩子一臂之力)
- Qt植物大战僵尸实现修改阳光和无冷却
- 齐向东透露工信部检测360浏览器
- 《西瓜书》阅读笔记——第一章
- 大卫奥格威-挑选客户的10条准则
- android x5 视频全屏,腾讯X5浏览器内核全屏播放视频相关问题