周次

W2

W3

W4

W2


Anaconda Powershell Prompt (Anaconda3)

→ 輸入 cd D:(轉化路徑為D槽)

→ 輸入 jupyter lab

https://en.wikibooks.org/wiki/A_Beginner's_Python_Tutorial

https://www.w3schools.com/python/python_arrays.asp

print("Hello, World")

#Ch1
#1.1
20 + 80
6 - 5
2 * 5
5 ** 2

print('1 + 2 is an addition')

print('One kilobyte is 2^10 bytes, or', 2 ** 10,  'bytes')

#1.2
23 / 3
23 % 3

#1.3
1 + 2 * 3
(1 + 2) * 3

#1.4
# I am a comment. Fear my warth!
print("food is very nice") #eat me
# print("food is very nice")

#Ch2
word1 = "Good"
word2 = "moring"
word3 = "to see you!"
print(word1, word2)
sentence = word1 + " " + word2 + " " + word3
print(sentence)

text = "abcdefghij"
len(text)

print(text[4])

print(text[:4])

print(text[4:])

print(text[4:8])

print(text[-2])

print(text[-4:])

print(text[::2])

#Ch3
a = 0
while a < 10:
    a = a + 1
    print(a)
-----

x = 10
while x != 0:
    print(x)
    x = x - 1
    print("Wow, we've counted x down, and now it equals", x)
print ("And now the loop has ended.")
-----

a = 1
if a > 5:
    print("a is greater than 5")
else:
    print("a is less than 5")
-----

z = 4
if z > 70:
    print("Something is very wrong")
elif z < 7:
    print("This is normal")
-----

def hello():
    print("hello")
    return 1234

print(hello())
-----

W3


import numpy as np

oneDArray = np.array([1, 2, 3, 4, 5])
print(oneDArray)
print(oneDArray.ndim)
print(oneDArray[2])

twoDArray = np.array([[1, 2, 3],[4, 5, 6]])
print(twoDArray)
print(twoDArray.ndim)
print(twoDArray[1])
print(twoDArray[1, 0])

threeDArray = np.array([[[1, 2, 3], [-1, -2, -3]],[[4, 5, 6], [4, 5, 6]]])
print(threeDArray)
print(threeDArray.ndim)
print(threeDArray[0])
print(threeDArray[0, 1])
print(threeDArray[0, 1, 0])

#Array Dimension
print(twoDArray.shape)

#Numpy Arrays
for i in oneDArray:
    print(i)

for i in twoDArray:
    print(i)
for i in twoDArray:
    for j in i:
        print(j)

for i in threeDArray:
    print(i)
for i in threeDArray:
    for j in i:
        print(j)
for i in threeDArray:
    for j in i:
        for k in j:
            print(k)

#.zeros()
zerosArray = np.zeros(10) #create an array with 10 zeros
print(zerosArray)
zerosArray = zerosArray.astype(int) #convert the elements of the list from float to int
print(zerosArray)

#.ones()
onesArray = np.ones(10) #create an array with 10 zeros
print(onesArray)
onesArray = onesArray.astype(int) #convert the elements of the list from float to int
print(onesArray)

#.full()
preFillArray = np.full(10, 5) #create an array with 10 elements where each element is 5
print(preFillArray)

#Scalar Operations
#Addition
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix)
print(matrix + 2)
pythonList = [[1, 2, 3], [4, 5, 6]]
print(pythonList + 2)

#Substraction
print(matrix - 2)
pythonList = [[1, 2, 3], [4, 5, 6]]
print(pythonList - 2)

#Multiplication
print(matrix * 2)
pythonList = [[1, 2, 3], [4, 5, 6]]
print(pythonList * 2)

#Division
print(matrix / 2)
print(matrix // 2)
pythonList = [[1, 2, 3], [4, 5, 6]]
print(pythonList / 2)

#Power
print(matrix ** 2)
pythonList = [[1, 2, 3], [4, 5, 6]]
print(pythonList ** 2)

#Transpose
print(matrix.T)
pythonList = [[1, 2, 3], [4, 5, 6]]
print(pythonList.T)

#Element-wise Operation
#Addition
matrix1 = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix1)
matrix2 = np.array([[-1, -2, -3], [-4, -5, -6]])
print(matrix2)
print(matrix1 + matrix2)
pythonList1 = [[1, 2, 3], [4, 5, 6]]
pythonList2 = [[-1, -2, -3], [-4, -5, -6]]
print(pythonList1 + pythonList2)

#Substraction
print(matrix1 - matrix2)
print(pythonList1 - pythonList2)

#Multiplication
print(matrix1 * matrix2)
print(pythonList1 * pythonList2)

#Division
print(matrix1 / matrix2)
print(matrix1 // matrix2)
print(pythonList1 / pythonList2)

#Matrix Multiplication
matrix1 = np.array([[1, 2, 3], [4, 5, 6], [0, 0, 0]])
print(matrix1)
matrix2 = np.array([[1, 2, 3], [4, 5, 6], [0, 0, 0]])
print(matrix2)
np.matmul(matrix1, matrix2)

#Satistics
matrix1 = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix1)
np.min(matrix1)
np.max(matrix1)
np.sum(matrix1)
np.mean(matrix1)
np.std(matrix1)
np.median(matrix1)
import pandas as pd

myList = [['Apple', 'Red'],
          ['Banana', 'Yellow'], 
          ['Orange', 'Orange']]
myDataFrame = pd.DataFrame(myList, columns = ['Fruits', 'Colors'])
myDataFrame

myList = np.array([[0, 1],
                  [2, 3],
                  [4, 5]])
myDataFrame = pd.DataFrame(myList, columns = ['Evens', 'Odds'])
myDataFrame

myDictionary = {'Fruit':['Apple', 'Banana', 'Orange'], 'Color':['Red', 'Yellow', 'Orange']}
myDataFrame = pd.DataFrame(myDictionary)
myDataFrame
df = pd.read_csv('cereals.csv')
df

df.set_index('name')

df.head(7)

df.tail(7)

df.describe()

df[1:4]

df[['name', 'rating']]

#Boolean List
df
thirdRow = [False, False, True, False, False]
df[thirdRow]

#Filtering Rows
condition = df['calories'] > 70
df[condition]
df[df['calories']] > 70

df[(df['calories'] > 70) & (df['protein'] < 4)]

df[(df['calories'] > 70) | (df['protein'] > 3)]

#loc
#Indexing
df
df.loc[0, 'name']
#Slicing
df
df.loc[0:4, 'name':'protein']
#Indexing & Slicing
df.loc[[5, 8], 'name':'protein']

#iloc
df.iloc[9, 2]
#Indexing
df.iloc[[9], [2]]
#Slicing
df.iloc[0:5, 0:3]