I am working on the file "06.List_it_out.ipynb".
The first task is:
Indexing and Slicing : How to access parts of a list
Can you write Python code for it?

all_info = ["Sherlock", True, 42]
all_info[0]

all_info[1]

more_info = ["Watson", "Hooley", all_info]
more_info[-1]

more_info[-1][0]

a_list = [1,2,3,4,5,6,7,8,9,10]
a_list[0]

a_list[2:4]

a_list[1:]

a_list[-3:]

a_list[3:6]

a_list[0:3]


I am working on the file "06.List_it_out.ipynb".
The first task is:
That's it. As simple as that.
So why do we have sets, as opposed to just using lists?
Sets are really fast when it comes to checking for membership. Here's how:
Can you write Python code for it?


I am working on the file "06.List_it_out.ipynb".
The first task is:
Tuples and Numbers
Can you write Python code for it?

monthly_high = (115.20, 113.60, 117.15, 120.90, 118.25)

print("Monthly high is", max(monthly_high))
print("Monthly low is", min(monthly_high))
print("Range:", max(monthly_high)-min(monthly_high))


I am working on the file "06.Model Evaluation.ipynb".
The first task is:
<b>Classification reports</b> - a text report with important classification metrics (e.g. precision, recall)
Can you write Python code for it?

from sklearn.metrics import classification_report

# Using the test and prediction sets from above
print(classification_report(y_test, y_pred, target_names = iris.target_names))

# Another example with some toy data

y_test = ['cat', 'dog', 'mouse', 'mouse', 'cat', 'cat']
y_pred = ['mouse', 'dog', 'cat', 'mouse', 'cat', 'mouse']

# How did our predictor do?
print(classification_report(y_test, ___, target_names = ___)) # <-- fill in the blanks


I am working on the file "06._Scipy.ipynb".
The first task is:
A biblioteca SciPy implementa uma vasta gama de algoritmos em computação científica, divididos em vários módulos:

scipy.cluster - K-médias, vector quantization
scipy.constants - constantes físicas e matemáticas
scipy.fftpack - transformada de Fourier
scipy.integrate - integração
scipy.interpolate - interpolação
scipy.io - IO, permite a leitura de arquivos .mat (Matlab)
scipy.linalg - álgebra linear
scipy.ndimage - processamento de sinais $n$-dimensionais
scipy.odr - regressão
scipy.optimize - otimização
BFGS, método de Newton, raízes de funções, simulated annealing,...

scipy.signal - processamento de sinais
scipy.sparse - matrizes esparsas
scipy.spatial - algoritmos e estruturas de dados espaciais
KD-Trees, Voronoi, Delaunay e fecho convexo

scipy.special - funções matemáticas diversas
Bessel, funções elípticas, hipergeométricas, parabólicas,...

scipy.stats - estatística

SciPy pode ser vista como uma biblioteca equivalente à GSL (GNU Scientific Library C/C++) ou a várias toolboxes de MATLAB. SciPy faz uso da NumPy para produzir implementações eficientes.
O pesquisador deveria preferir implementações oferecidas pela SciPy, como argumentado por Haenel et al. nas Python scientific lecture notes:

Antes de implementr uma rotina, é importante checar se o processamento desejado já não se encontra implementado na SciPy. Como programadores, cientistas geralmente tendem a reinventar a roda, o que leva a código incorreto, não-otimizado e difícil de ser compartilhado e mantido. Em contraste, as rotinas da SciPy são otimizadas e testadas, e deveriam ser utilizadas sempre que possível.

Para ilustrar uso e potencial da biblioteca, nas próximas seções serão apresentadas algumas rotinas encontradas em três dos módulos da SciPy: linalg para álgebra linear, optimize para otimização contínua e stats para estatística.
Álgebra Linear - scipy.linalg
O módulo scipy.linalg fornece operações básicas de álgebra linear e baseia-se em bibliotecas nativas eficientes como BLAS e LAPACK. No exemplo abaixo, o determinante de uma...
Can you write Python code for it?

from scipy import linalg
A = array([[1, 2],
           [3, 4]])
linalg.det(A)


I am working on the file "06._Scipy.ipynb".
The first task is:
A mediana obviamente coincide com o percentil 50:
Can you write Python code for it?


I am working on the file "06._Scipy.ipynb".
The first task is:
A partir desses dados, gostaríamos de estimar funções para prever a temperatura ao longo do ano. O formato se "sino" dos gráficos acima sugere que uma função Gaussiana poderia produzir um bom fitting:
Can you write Python code for it?

import scipy.optimize as opt

def f(x, a, b, c, d):
    return a * exp(-(x-b)**2/(2*c**2)) + d

theta_min, theta_min_cov = opt.curve_fit(f, month, tmin, [10., 6, 2, -60])
theta_min

theta_max, theta_max_cov = opt.curve_fit(f, month, tmax, [10., 6, 2, -60])
theta_max

X = linspace(0, 11, 25)
a, b, c, d = theta_max
plot(month, tmax, 'ro', linewidth=3, alpha=0.3, label=u'máximas')
plot(X, [f(x, a, b, c, d) for x in X], 'r-', alpha=0.7, label=r'$f_{\max}$')

a, b, c, d = theta_min
plot(month, tmin, 'bo', linewidth=3, alpha=0.3, label=u'mínimas')
plot(X, [f(x, a, b, c, d) for x in X], 'b-', alpha=0.7, label=r'$f_{\min}$')

ylabel(r'Celsius')
xlabel(u'Mês')
xlim(0,11)
ylim(-80,60)
legend(bbox_to_anchor=(1.34,1))
title(r'Extremos de temperatura no Alasca')


I am working on the file "06._Scipy.ipynb".
The first task is:
Como esperado, $a$ e $b$ diferem segundo o teste (note o baixíssimo p-valor):
Can you write Python code for it?


I am working on the file "06._Scipy.ipynb".
The first task is:
Estatística - scipy.stats
Funções de densidade de probabilidade
Can you write Python code for it?


I am working on the file "06._Scipy.ipynb".
The first task is:
Fitting
Uma atividade frequente em análise estatística é estimar os parâmetros de uma distribuição a partir de dados observados. Os métodos fit das várias distribuições encontradas em stats são capazes de realizar a estimação dos parâmetros por máxima verossimilhança (maximum likelihood estimate - MLE). Como exemplo, considere os dados de comprimento da sépala para Iris setosa:
Can you write Python code for it?

iris_data = loadtxt('./data/iris.data.txt', usecols=(0,1,2,3))
iris_class = loadtxt('./data/iris.data.txt', dtype='string')[:,4]

setosa = iris_data[iris_class == 'Iris-setosa']


I am working on the file "06._Scipy.ipynb".
The first task is:
Inversão de matrizes
Can you write Python code for it?

A = array([[1, 2],
              [3, 4]])
Ainv = linalg.inv(A)
dot(A, Ainv)


I am working on the file "06._Scipy.ipynb".
The first task is:
Note como o resultado acima é muito próximo da nossa função verdadeira, $f(x) = x^2 + 0.2 x + 4 \cos(x)$.
Can you write Python code for it?

X = linspace(-3, 3, num=200)
plot(X, f(X), 'r-', label=r'$f(x)$')
plot(Xdata, Ydata, 'rx', label=r'Dados')
theta1, theta2, theta3 = theta
Y_model = [model(x, theta1, theta2, theta3) for x in X]
plot(X, Y_model, 'b--', label=r'Fitting')
xlim((-3,3))
title(u'Fitting')
legend()


I am working on the file "06._Scipy.ipynb".
The first task is:
O exemplo abaixo ilustra a estimação para a distribuição normal:
Can you write Python code for it?

sep_len = setosa[:,0]
mu, sig = stats.norm.fit(sep_len)
mu, sig

subplot(2,1,1)
n, pan, binstches = hist(sep_len, 10)

subplot(2,1,2)
X = arange(3, 7, 0.05)
plot(X, [stats.norm.pdf(x, mu, sig) for x in X])
xlabel(u'Comprimento da sépala')
ylabel(r'$N(%.2f, %.2f)$' % (mu, sig))


I am working on the file "06._Scipy.ipynb".
The first task is:
O módulo stats fornece diversas funções de densidade de probabilidade (pdf). Abaixo, são computadas as probabilidades para todo ponto $x$ em um certo domínio $X$, tomando-se uma distribuição normal com média igual a 5 e desvio padrão igual a 1,5.:
Can you write Python code for it?

X = arange(-2, 12, 0.05)
plot(X, [stats.norm.pdf(x, 5., 1.5) for x in X])
xlabel('x')
ylabel(r'$N(5, 1.5)$')


I am working on the file "06._Scipy.ipynb".
The first task is:
O resultado obtido é satisfatório, mas há um porém: o modelo escolhido é perfeito, isto é, ele tem a mesma forma paramétrica da verdadeira função $f(x)$. E se o modelo proposto fosse menos preciso? Abaixo, temos o resultado obtido por um modelo polinomial $\theta_1 x^2 + \theta_2 x + \theta_3$:
Can you write Python code for it?

def model2(x, theta1, theta2, theta3):
    return theta1 * x**2 + theta2 * x + theta3

palpite = [1,1,1]
theta, theta_cov = curve_fit(model2, Xdata, Ydata, palpite)
theta

X = linspace(-3, 3, num=200)
plot(X, f(X), 'r-', label=r'$f(x)$')
plot(Xdata, Ydata, 'rx', label=r'Dados')
theta1, theta2, theta3 = theta
Y_model = [model2(x, theta1, theta2, theta3) for x in X]
plot(X, Y_model, 'b--', label=r'Fitting')
xlim((-3,3))
title(u'Fitting (modelo polinomial)')
legend()


I am working on the file "06._Scipy.ipynb".
The first task is:
O teste corretamente informa que não podemos descartar a hipótese nula para $a$ e $c$:
Can you write Python code for it?


I am working on the file "06._Scipy.ipynb".
The first task is:
Qualquer percentil pode ser recuperado:
Can you write Python code for it?

stats.scoreatpercentile(sep_len, 90)

stats.scoreatpercentile(sep_len, 15)


I am working on the file "06._Scipy.ipynb".
The first task is:
Restrições envolvendo propriedades de matrizes são tratados como exceções. Python é uma linguagem que suporta exceções, que podem ser capturadas em tempo de execução de tratadas de acordo¹. No exemplo abaixo, uma exceção, ValueError, é gerada ao tentarmos computar o determinante de uma matrix que não é quadrada:
¹ Ver Errors and Exceptions para detalhes.
Can you write Python code for it?


I am working on the file "06._Scipy.ipynb".
The first task is:
Se o sistema $\mathrm{\tt A} \mathbf{x} = 0$ tem solução, então o menor valor singular é zero:
Can you write Python code for it?


I am working on the file "06._Scipy.ipynb".
The first task is:
Testes estatísticos
O teste t-Student toma duas amostras independentes e verifica a hipótese de que a média das duas populações (assumindo a mesma variância ) são iguais (hipótese nula - null-hypothesis). O teste avalia se a média esperada difere significativamente entre as duas amostras. Implementado na rotina ttest_ind, o teste também computa o p-valor. Se o valor do p-valor for menor que um limiar (por exemplo, 1%), rejeita-se a hipótese de médias iguais.
Considere como exemplo três amostras, $a$, $b$ e $c$. As amostras $a$ e $c$ são produzidas a partir de uma distribuição normal $\mathcal{N}(0, 1.)$ enquanto que $b$ é oriunda de uma distribuição $\mathcal{N}(1, 1)$. Note que todas as distribuições tem a mesma variâncias, mas a distribuição de $b$ difere das outras duas em relação à média:
Can you write Python code for it?

a = random.normal(0, 1, size=500)
b = random.normal(1, 1, size=100)
c = random.normal(0, 1, size=100)

n, pan, binstches = hist(a, 25, label='a')
n, pan, binstches = hist(b, 25, alpha=0.75, label='b')
n, pan, binstches = hist(c, 25, alpha=0.5, label='c')
legend()


I am working on the file "06._Scipy.ipynb".
The first task is:
Um problema com este método é que dependendo do ponto inicial o processo de otimização pode ficar "preso" em um mínimo local. No exemplo anterior, o algoritmo foi inicializado com $x = 0$ (o segundo argumento de fmin_bfgs). Eis o resultado se a inicialização fosse $x = 1$:
Can you write Python code for it?

xmin = fmin_bfgs(f, 1, disp=0)
print xmin
show_opt_result(x, f, xmin, u'Método quasi-Newton BFGS')


I am working on the file "06._Scipy.ipynb".
The first task is:
Uma alternativa para obter o mínimo de uma função é utilizar o método BFGS (Broyden–Fletcher–Goldfarb–Shanno), um método iterativo de otimização:
Can you write Python code for it?

from scipy.optimize import fmin_bfgs

xmin = fmin_bfgs(f, 0)
xmin

def show_opt_result(x, f, xmin, opt_title=None):

    plot(x, f(x), 'r-')
    xlim((-3,3))
    scatter([xmin],[f(xmin)], 10, color='red')
    annotate('%f' % xmin,
            xy=(xmin, f(xmin)), fontsize=12, xycoords='data',
            xytext=(-90, -50), textcoords='offset points',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
    if opt_title != None:
        title(opt_title)

show_opt_result(x, f, xmin, u'Método quasi-Newton BFGS')


I am working on the file "06/.ipynb_checkpoints/06-Homework-Devulapalli-Forecast_graded-checkpoint.ipynb".
The first task is:
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
Can you write Python code for it?

data['daily'].keys()
mooncover=data['daily']['data'][0]['moonPhase']
if mooncover == 0:
    print("Today is a new moon day. The mooncover is",mooncover)
if mooncover > 0 and mooncover <0.25:
    print("The moon is in waxing crescent phase. The mooncover is",mooncover)
if mooncover == 0.25:
    print("Today is a first quarter moon. The mooncover is",mooncover)
if mooncover > 0.25 and mooncover <0.5:
    print("The moon is in waxing gibbous phase. The mooncover is",mooncover)
if mooncover == 0.5:
    print("Today is a full moon day. The mooncover is",mooncover)
if mooncover > 0.5 and mooncover<0.75:
    print("The moon is in waning gibbous phase. The mooncover is",mooncover)
if mooncover == 0.75:
    print("Today is a last quarter moon. The mooncover is",mooncover)
if mooncover >0.75:
    print("The moon is in waning crescent phase. The mooncover is",mooncover)


I am working on the file "06/.ipynb_checkpoints/06-Homework-Devulapalli-Forecast_graded-checkpoint.ipynb".
The first task is:
4) What's the difference between the high and low temperatures for today?
Can you write Python code for it?

low=data['daily']['data'][0]['temperatureMin']
print("The low temperature is",low)
high=data['daily']['data'][0]['temperatureMax']
print("The high temperature is",high)
print("There is a difference of",high-low,"degrees between the high and low temperatures today.")


I am working on the file "06/.ipynb_checkpoints/06-Homework-Devulapalli-Forecast_graded-checkpoint.ipynb".
The first task is:
7) What was the temperature in Central Park on Christmas Day, 1980? How about 1990? 2000?
Tip: You'll need to use UNIX time, which is the number of seconds since January 1, 1970. Google can help you convert a normal date!
Tip: You'll want to use Forecast.io's "time machine" API at https://developer.forecast.io/docs/v2
Can you write Python code for it?

import requests
timestamp='346550400'
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/40.7829,-73.9654,"+timestamp
cpresponse=requests.get(url)
cpdata=cpresponse.json()
print("The temperature at midnight of Christmas in 1980 was",cpdata['currently']['temperature'],"degrees F")

import requests
timestamp='662083200'
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/40.7829,-73.9654,"+timestamp
cpresponse=requests.get(url)
cpdata=cpresponse.json()
print("The temperature at midnight of Christmas in 1990 was",cpdata['currently']['temperature'],"degrees F")

import requests
timestamp='977702400'
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/40.7829,-73.9654,"+timestamp
cpresponse=requests.get(url)
cpdata=cpresponse.json()
print("The temperature at midnight of Christmas in 2000 was",cpdata['currently']['temperature'],"degrees F")


I am working on the file "06/.ipynb_checkpoints/Homework_06_Paolo_Rivas_Legua-checkpoint.ipynb".
The first task is:
2) What's the current wind speed? How much warmer does it feel than it actually is?
Can you write Python code for it?

weather_Lima['currently'].keys()

Lima_windspeed = weather_Lima['currently']['windSpeed']
print("The wind in Lima is currently blowing at", Lima_windspeed, "mph")

weather = weather_Lima['currently']
Temperature = int(weather['apparentTemperature']) - int(weather['temperature'])
if Temperature == 0:
    print("The temperature feels exactly as expected:", weather['temperature'], "degrees Fahrenheit")
elif Temperature > 0:
    print("It feels", Temperature, "degrees Fahrenheit warmer than the actual temperature:", weather['temperature'], "degrees Fahrenheit")
else:
    print("It feels", Temperature, "degrees Fahrenheit colder than the actual temperature:", weather['temperature'], "degrees Fahrenheit")


I am working on the file "06/.ipynb_checkpoints/Homework_06_Paolo_Rivas_Legua-checkpoint.ipynb".
The first task is:
I have chosen Lima-Peru, the city I was born.
Can you write Python code for it?

print(weather_Lima['timezone'])

print("Longitude:", weather_Lima['longitude'], "Latitude:", weather_Lima['latitude'])


I am working on the file "06/06-Homework-Devulapalli-Forecast_graded.ipynb".
The first task is:
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
Can you write Python code for it?


I am working on the file "06/06-Homework-Devulapalli-Forecast_graded.ipynb".
The first task is:
4) What's the difference between the high and low temperatures for today?
Can you write Python code for it?


I am working on the file "06/06-Homework-Devulapalli-Forecast_graded.ipynb".
The first task is:
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
Can you write Python code for it?

x=0
hotlimit=86
warmlimit=68
for date in data['daily']['data']:
    if data['daily']['data'][x]['temperatureMax'] > hotlimit:
        print("The high for day",x,"of the mext week is",data['daily']['data'][x]['temperatureMax'],"Fahrenheit")
        print("It's a hot day. ")
    if data['daily']['data'][x]['temperatureMax'] < hotlimit and data['daily']['data'][x]['temperatureMax'] > warmlimit :
        print("The high for day",x,"of the mext week is",data['daily']['data'][x]['temperatureMax'],"Fahrenheit")
        print("It's a warm day. ")
    if data['daily']['data'][x]['temperatureMax'] < warmlimit:
        print("The high for day",x,"of the mext week is",data['daily']['data'][x]['temperatureMax'],"Fahrenheit")
        print("It's a cold day. ")
    x=x+1


I am working on the file "06/06-Homework-Devulapalli-Forecast_graded.ipynb".
The first task is:
6) What's the weather looking like for the rest of today in Miami, Florida? I'd like to know the temperature for every hour, and if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
Can you write Python code for it?

import requests
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/25.7617,-80.1918"
miamiresponse=requests.get(url)
miamidata=miamiresponse.json()
miamidata.keys()

noofhoursinaday=0
miamidata['hourly']['data']
for count in miamidata['hourly']['data']:
    if miamidata['hourly']['data'][noofhoursinaday]['cloudCover'] > 0.5:
        print("The temperature for hour",noofhoursinaday+1,"is",miamidata['hourly']['data'][noofhoursinaday]['temperature'],"degrees F and cloudy.")
    else:
        print("The temperature for hour",noofhoursinaday+1,"is",miamidata['hourly']['data'][noofhoursinaday]['temperature'],"degrees F.")
    noofhoursinaday=noofhoursinaday+1

    if noofhoursinaday>23:
        break


I am working on the file "06/06-Homework-Devulapalli-Forecast_graded.ipynb".
The first task is:
7) What was the temperature in Central Park on Christmas Day, 1980? How about 1990? 2000?
Tip: You'll need to use UNIX time, which is the number of seconds since January 1, 1970. Google can help you convert a normal date!
Tip: You'll want to use Forecast.io's "time machine" API at https://developer.forecast.io/docs/v2
Can you write Python code for it?


I am working on the file "06/Dark Sky Forecast_Homework_6_Graded.ipynb".
The first task is:
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
Can you write Python code for it?

type(weather_data['daily'])

weather_data['daily'].keys()

type(weather_data['daily']['data'])

for phase in weather_data['daily']['data']:
    moon_forecast_tomorrow = phase['moonPhase']
    break
#print(phase['moonPhase'])
if moon_forecast_tomorrow == 0:
    print("Tomorrow is New Moon.")
elif moon_forecast_tomorrow > .75:
    print("Tomorrow the Moon is in a Waning Crescent phase.")
elif moon_forecast_tomorrow == .75:
    print("Tomorrow is last quarter Moon.")
elif moon_forecast_tomorrow > .5:
    print("Tommorrow the Moon is in a Waning Gibbous phase.")
elif moon_forecast_tomorrow == .5:
    print("Tommorrow is Full Moon.")
elif moon_forecast_tomorrow > .25:
    print("Tommorrow the Moon is a Waxing Gibbous phase.")
elif moon_forecast_tomorrow == .25:
    print("Tommorrow is first Quarter Moon.")
elif moon_forecast_tomorrow > 0:
    print("Tommorrow the Moon is in a Waxing Crescent phase. This is the first phase after New Moon.")


I am working on the file "06/Dark Sky Forecast_Homework_6_Graded.ipynb".
The first task is:
4) What's the difference between the high and low temperatures for today?
Can you write Python code for it?

print(weather_data['currently'])

response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/47.4079,9.4647,1465420000")
hist_weather_data = response.json()

hist_weather_data.keys()

hist_weather_data['daily'].keys()

print(hist_weather_data['daily']['data'])

for n in hist_weather_data['daily']['data']:
    Min = n['temperatureMin']
    Max = n['temperatureMax']

Min_Max = Max - Min
Min_Max_Celsius = ((Max - 32) * 5 / 9) - ((Min - 32) * 5 / 9)
print("The diffrence between the high and low temperatures on Wedesday, June 8, in Trogen, Switzerland, was", round(Min_Max), "Fahrenheit", "or", round(Min_Max_Celsius), "Celsius.")


I am working on the file "06/Dark Sky Forecast_Homework_6_Graded.ipynb".
The first task is:
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
Can you write Python code for it?

response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/40.7128,-74.0059")
weather_data = response.json()
weather_data.keys()

weather_data['daily'].keys()

print(weather_data['daily']['data'][0])

Forecast = weather_data['daily']['data']
for max_temp in Forecast:
    Convert_Celsius = (max_temp['temperatureMax'] - 32) * 5 / 9
    if Convert_Celsius > 30:
        Temperature = "hot"
    elif Convert_Celsius > 20:
        Temperature = "warm"
    else:
        Temperature = "cold"
    print("The maximum temperature in New York for next week are", max_temp['temperatureMax'], "Fahrenheit or", round(Convert_Celsius), "Celsius. That is", Temperature)

 import datetime
print(datetime.datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d'))

import time
import datetime
Forecast = weather_data['daily']['data']
for max_temp in Forecast:
    Convert_Celsius = (max_temp['temperatureMax'] - 32) * 5 / 9
    time1 = max_temp['time']
    if Convert_Celsius > 30:
        Temperature = "hot"
    elif Convert_Celsius > 20:
        Temperature = "comfortably warm."
    else:
        Temperature = "cold"
    #print("On", time.strftime('%A, %b %d', time.localtime(time1)), "the maximum temperature will be", max_temp['temperatureMax'], "Fahrenheit or", round(Convert_Celsius), "Celsius. That is", Temperature)
    print("On", time.strftime('%A', time.localtime(time1)), "the maximum temperature will be", max_temp['temperatureMax'], "Fahrenheit or", round(Convert_Celsius), "Celsius. That is", Temperature)

import datetime
print(datetime.datetime.fromtimestamp(int(time)).strftime('%Y-%m-%d'))


I am working on the file "06/Dark Sky Forecast_Homework_6_Graded.ipynb".
The first task is:
6) What's the weather looking like for the rest of today in Miami, Florida? I'd like to know the temperature for every hour, and if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
Can you write Python code for it?

response = requests.get("https://api.forecast.io/forecast/e554f37a8164ce189acd210d00a452e0/25.7617,-80.1918")
Florida = response.json()
Florida.keys()

Florida['hourly'].keys()

#print(Florida['hourly']['data'])

import time
import datetime
Hourly = Florida['hourly']['data']
Hourly = Hourly
for weather in Hourly:
    time = weather['time']
    stop_time = datetime.datetime.fromtimestamp(int(time)).strftime('%H')
    if stop_time == '01':
        break
    if weather['cloudCover'] > 0.5:
        cloudy = "and cloudy"
    else:
        cloudy = "not cloudy"
    print(datetime.datetime.fromtimestamp(int(time)).strftime('%H:%M'), "{", weather['temperature'], "°F}",cloudy)


I am working on the file "06/Dark Sky Forecast_Homework_6_Skinner.ipynb".
The first task is:
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
Can you write Python code for it?


I am working on the file "06/Dark Sky Forecast_Homework_6_Skinner.ipynb".
The first task is:
4) What's the difference between the high and low temperatures for today?
Can you write Python code for it?


I am working on the file "06/Dark Sky Forecast_Homework_6_Skinner.ipynb".
The first task is:
6) What's the weather looking like for the rest of today in Miami, Florida? I'd like to know the temperature for every hour, and if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
Can you write Python code for it?


I am working on the file "06/Forecastio_Graded.ipynb".
The first task is:
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
Can you write Python code for it?

# the moon is not currently printing
print("Currently", data['daily']['data'][0]['moonPhase'], "of the moon is showing.")


I am working on the file "06/Forecastio_Graded.ipynb".
The first task is:
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
Can you write Python code for it?

dailies = data['daily']['data']
import time

for day in dailies:

    date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(day['temperatureMaxTime']))

    if(day['temperatureMax']) > 95:

        print(date, "will be a hot day of",day['temperatureMax'], "degrees" )
    else:
        print(date, "will be a warm day of", day['temperatureMax'], "degrees")

#print(dailies)


I am working on the file "06/Homework_06_Paolo_Rivas_Legua.ipynb".
The first task is:
2) What's the current wind speed? How much warmer does it feel than it actually is?
Can you write Python code for it?


I am working on the file "06/Homework_06_Paolo_Rivas_Legua.ipynb".
The first task is:
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
Can you write Python code for it?

weather_Lima['daily'].keys()

weather_Lima['daily']['data']

for moon in weather_Lima['daily']['data']:
    moon_forecast = moon['moonPhase']
    break
if moon_forecast == 0:
    print("Tomorrow is New Moon.")
elif moon_forecast > .75:
    print("Tomorrow the Moon is in a Waning Crescent phase.")
elif moon_forecast == .75:
    print("Tomorrow is last quarter Moon.")
elif moon_forecast > .5:
    print("Tommorrow the Moon is in a Waning Gibbous phase.")
elif moon_forecast == .5:
    print("Tommorrow is Full Moon.")
elif moon_forecast > .25:
    print("Tommorrow the Moon is a Waxing Gibbous phase.")
elif moon_forecast == .25:
    print("Tommorrow is first Quarter Moon.")
elif moon_forecast > 0:
        print("Tommorrow the Moon is in a Waxing Crescent phase. This is the first phase after New Moon.")


I am working on the file "06/Homework_06_Paolo_Rivas_Legua.ipynb".
The first task is:
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
Can you write Python code for it?

response = requests.get("https://api.forecast.io/forecast/5afc9217d7eea82824254c951b1b57f4/-12.0561,-77.0268")
daily_weather_Lima = response.json()
daily_weather_Lima.keys()

week_report = daily_weather_Lima['daily']['data'][1:] #For the following 7 days

for day in week_report:
    Celsius = (day['temperatureMax'] - 32) * 5 / 9
    if Celsius > 28:
        Temperature = "hot"
    elif Celsius > 18:
        Temperature = "warm"
    else:
        Temperature = "cold"
    print("The maximum temperature in Lima for this day is", round(Celsius), "degrees. This is", Temperature)


I am working on the file "06/Homework_6_Emelike.ipynb".
The first task is:
2) What's the current wind speed? How much warmer does it feel than it actually is?
Can you write Python code for it?

diff = current['apparentTemperature'] - current['temperature']
print('The current windspeed is', current['windSpeed'],"miles per hour.")
print('It feels', diff, 'degrees warmer than it actually is.')


I am working on the file "06/Homework_6_Emelike.ipynb".
The first task is:
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
Can you write Python code for it?

daily = data['daily']['data']
today = daily[0]
today_moonphase = today['moonPhase']

if today_moonphase < .25:
    moonphase = 'new moon'
elif today_moonphase < 0.5:
    moonphase = 'first quarter moon'
elif today_moonphase < 0.75:
    moonphase = 'full moon'
else:
    moonphase = 'last quarter moon'

print('Reported moon phase from Dark Sky forecast is', today_moonphase)
print('So, currently, the', moonphase, 'is visible.')


I am working on the file "06/Homework_6_Emelike.ipynb".
The first task is:
4) What's the difference between the high and low temperatures for today?
Can you write Python code for it?

highlow_tempdiff = today['temperatureMax'] - today['temperatureMin']
print('The difference between the high and low temperatures for today is', highlow_tempdiff, 'degrees.')


I am working on the file "06/Homework_6_Emelike.ipynb".
The first task is:
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
Can you write Python code for it?

for item in daily:
    daily_hightemp = item['temperatureMax']
    if daily_hightemp < 70:
        tempdescription = 'COLD'
    elif daily_hightemp < 85:
        tempdescription = 'WARM'
    else:
        tempdescription = 'HOT'

    print('The high temp is', daily_hightemp, 'so it is', tempdescription)


I am working on the file "06/Homework_6_Emelike.ipynb".
The first task is:
6) What's the weather looking like for the rest of today in Miami, Florida? I'd like to know the temperature for every hour, and if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
Can you write Python code for it?

response = requests.get('https://api.forecast.io/forecast/a197f06e1906b1a937ad31d4378b8939/25.7617,-80.1918')
data = response.json()

hourly = data['hourly']['data']

for item in hourly:
    if item['cloudCover'] < .5:
        print('It is', item['temperature'], 'degrees and cloudy')
    else:
        print('It is', item['temperature'], 'degrees')


I am working on the file "06/darksky-foundations-homework.ipynb".
The first task is:
2) What's the current wind speed? How much warmer does it feel than it actually is?
Can you write Python code for it?

#Where is the wind speed? Maybe it's in 'currently' updates.
current = data['currently']
type(current)
current.keys()

#We need windspeed, temperature and apparent temperature
print("The current wind speed is", current['windSpeed'])

current_temp = current['temperature']
apparent_temp = current['apparentTemperature']
cold_temp = current_temp - apparent_temp
warm_temp = apparent_temp - current_temp

if current_temp - apparent_temp == 0:
    print("It feels as warm as it actually is.")
if current_temp - apparent_temp > 0:
    print("It currently feels", cold_temp, "degrees colder")
if current_temp - apparent_temp < 0:
    print("It currently feels", warm_temp, "degrees colder")


I am working on the file "06/darksky-foundations-homework.ipynb".
The first task is:
3) The first daily forecast is the forecast for today.
or the place you decided on up above, how much of the moon is currently visible?
From the documentation: moonPhase (only defined on daily data points): A number representing the fractional part of the lunation number of the given day: a value of 0 corresponds to a new moon, 0.25 to a first quarter moon, 0.5 to a full moon, and 0.75 to a last quarter moon. (The ranges in between these represent waxing crescent, waxing gibbous, waning gibbous, and waning crescent moons, respectively.)
Can you write Python code for it?

#We are looking for moonphase, which is only available in daily and represent weather that will
#occur over the entire day
daily = data['daily']
#print(daily)
daily.keys()
#print(daily['data'])
daily_info = daily['data']

#Looks like a list of dictionaries
#We have to look for the moonphase and the time

for info in daily_info:
    #print(info['moonPhase'], info['time'])
    if info['time'] == 1465358400:
        print(info['moonPhase'], "of the moon is currently visible")

#from datetime import datetime
#datetime.fromtimestamp(1465444800)

#okay I gotta find the human timings to see how much of the moon I can see now
#okay so these are daily timings, so I just need the first one


I am working on the file "06/darksky-foundations-homework.ipynb".
The first task is:
5) Loop through the daily forecast, printing out the next week's worth of predictions.
I'd like to know the high temperature for each day, and whether it's hot, warm, or cold,
based on what temperatures you think are hot, warm or cold.
Can you write Python code for it?

#Here's what we need: daily forecasts for the next week,
#temperatureMax, my definition of hot and cold temperatures

for heat in daily_info:
    #print(heat['temperatureMax'], heat['time'])
    from datetime import datetime
    datetime = datetime.fromtimestamp(heat['time'])
    print("The high temperature for", datetime, "is", heat['temperatureMax'], "degrees")
    if heat['temperatureMax'] >= 80:
        print("Damn it's gonna be hot")
    if 70 <= heat['temperatureMax'] <= 79:
        print("I guess I'll wear a t-shirt because it's going to be warm")
    if heat['temperatureMax'] <= 69:
        print("Damn I hope they don't turn on the ac because it's going to be cold")


I am working on the file "06/darksky-foundations-homework.ipynb".
The first task is:
6) What's the weather looking like for the rest of today in Miami, Florida?
I'd like to know the temperature for every hour, and if it's going to have cloud cover of
more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
Miami Coordinates: 25.7617° N, 80.1918° W
From the documentation: cloudCover: A numerical value between 0 and 1 (inclusive) representing the percentage of sky occluded by clouds.
Can you write Python code for it?

response = requests.get('https://api.forecast.io/forecast/2720ff846cb70aeec085c402d31c3ea2/25.7617,-80.1918')

miami = response.json()

#What I need: hourly, icon

miami.keys()
miami_hourly = miami['hourly']
miami_hourly.keys()
#print(miami_hourly['data'])
miami_hourly_data = miami_hourly['data']

for temp in miami_hourly_data:
    #print(temp['time'], temp['cloudCover'], temp['temperature'])
    from datetime import datetime
    todaytime = datetime.fromtimestamp(temp['time'])
    if temp['time'] < 1465444800 and temp['cloudCover'] <= 0.5:
        print("The temperature at", todaytime, "is", temp['temperature'], "degrees")
    if  temp['time'] < 1465444800 and temp['cloudCover'] > 0.5:
        print("The temperature at", todaytime, "is", temp['temperature'], "degrees and it is cloudy")


I am working on the file "06/darksky-foundations-homework.ipynb".
The first task is:
7) What was the temperature in Central Park on Christmas Day, 1980? How about 1990? 2000?
Tip: You'll need to use UNIX time, which is the number of seconds since January 1, 1970. Google can help you convert a normal date!
Tip: You'll want to use Forecast.io's "time machine" API at https://developer.forecast.io/docs/v2
Can you write Python code for it?

response = requests.get('https://api.forecast.io/forecast/2720ff846cb70aeec085c402d31c3ea2/40.7829,-73.9654,346550400')
xmas_1980 = response.json()

response = requests.get('https://api.forecast.io/forecast/2720ff846cb70aeec085c402d31c3ea2/40.7829,-73.9654,662083200')
xmas_1990 = response.json()

response = requests.get('https://api.forecast.io/forecast/2720ff846cb70aeec085c402d31c3ea2/40.7829,-73.9654,662083200')
xmas_2000 = response.json()

#I need the temperature from current because it's not in daily
current_1980 = xmas_1980['currently']
#current_1980.keys()
current_1990 = xmas_1990['currently']
current_2000 = xmas_2000['currently']

print("The temperature on Christmas day at Central Park was", current_1980['temperature'], "degrees in 1980",
     current_1990['temperature'], "degrees in 1990 and", current_2000['temperature'], "degrees in 2000.")


I am working on the file "06/darksky-foundations-homework.ipynb".
The first task is:
Question: Why doesn't this work?
for info in daily_info:
    print(info['moonPhase'], info['time'])
    daily_time = info['time']
    from datetime import datetime
    print datetime.fromtimestamp('dailytime')
4) What's the difference between the high and low temperatures for today?
Once again, according to the documentation: temperatureMin, temperatureMinTime, temperatureMax, and temperatureMaxTime (only defined on daily data points): numerical values representing the minimum and maximumum temperatures (and the UNIX times at which they occur) on the given day in degrees Fahrenheit.
Can you write Python code for it?

for temp in daily_info:
    #print(temp['temperatureMin'], temp['temperatureMax'], temp['time'])
    temp_diff = temp['temperatureMax'] - temp['temperatureMin']
    if temp['time'] == 1465358400:
        print("The difference between the high and low temperatures for today is", temp_diff, "degrees")


I am working on the file "06/forecast_api.ipynb".
The first task is:
1) Make a request from the Forecast.io API for where you were born (or lived, or want to visit!).
Can you write Python code for it?

# SEOUL = '37.532600,127.024612'
response = requests.get("https://api.forecast.io/forecast/4230d91e7452245b2479aec2fc16870d/37.532600,127.024612")
data = response.json()

data.keys()

data['timezone']


I am working on the file "06/forecast_api.ipynb".
The first task is:
2) What's the current wind speed? How much warmer does it feel than it actually is?
Can you write Python code for it?

# Wind speed
data['currently']['windSpeed']

# How much warmer does it feel than it actually is
data['currently']['apparentTemperature']-data['currently']['temperature']


I am working on the file "06/forecast_api.ipynb".
The first task is:
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
Can you write Python code for it?

daily = data['daily']['data']
print (daily[0]['moonPhase'])


I am working on the file "06/forecast_api.ipynb".
The first task is:
6) What's the weather looking like for the rest of today in Miami, Florida? I'd like to know the temperature for every hour, and if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
Can you write Python code for it?

# Miami, Florida = 25.787676,-80.224145
response = requests.get("https://api.forecast.io/forecast/4230d91e7452245b2479aec2fc16870d/25.787676,-80.224145")
data = response.json()

data['timezone']

hourly = data['hourly']['data']
for weather in hourly:
    if weather['cloudCover'] > 0.5:
        print (weather['temperature'],"and cloudy")
    else:
        print(weather['temperature'])


I am working on the file "06/homework-6-schuetz.ipynb".
The first task is:
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
Can you write Python code for it?

daily_forecast = data['daily']['data']

print('Starting with today\'s, the forecasts for the next week are for highs of:')
for day in daily_forecast:
    if 85 <= day['temperatureMax']:
        warmth = 'hot'
    elif 70 <= day['temperatureMax'] < 85:
        warmth = 'warm'
    else:
        warmth = 'cold'
    print(day['temperatureMax'], 'degrees Fahrenheit, a pretty', warmth, 'day.')


I am working on the file "06/homework-6-schuetz.ipynb".
The first task is:
7) What was the temperature in Central Park on Christmas Day, 1980? How about 1990? 2000?
Tip: You'll need to use UNIX time, which is the number of seconds since January 1, 1970. Google can help you convert a normal date!
Tip: You'll want to use Forecast.io's "time machine" API at https://developer.forecast.io/docs/v2
Can you write Python code for it?

decades = range(3)

for decade in decades:
    cp_url = 'https://api.forecast.io/forecast/' + apikey + '/' + coordinates['Central Park'] + ',' + str(10 * decade + 1980) + '-12-25T12:00:00'
    cp_response = requests.get(cp_url)
    cp_data = cp_response.json()
    print('On Christmas Day in', str(1980 + decade * 10) + ', the high in Central Park was', cp_data['daily']['data'][0]['temperatureMax'], 'degrees Fahrenheit.')


I am working on the file "06/homework-6-schuetz_graded.ipynb".
The first task is:
2) What's the current wind speed? How much warmer does it feel than it actually is?
Can you write Python code for it?

print('The current wind speed is', data['currently']['windSpeed'], 'miles per hour.')

print('It feels', round(data['currently']['apparentTemperature'] - data['currently']['temperature'], 2), 'degrees Fahrenheit warmer than it actually is.')


I am working on the file "06/homework-6-schuetz_graded.ipynb".
The first task is:
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
Can you write Python code for it?


I am working on the file "06/homework-6-schuetz_graded.ipynb".
The first task is:
6) What's the weather looking like for the rest of today in Miami, Florida? I'd like to know the temperature for every hour, and if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
Can you write Python code for it?

fl_url = 'https://api.forecast.io/forecast/' + apikey + '/' + coordinates['Miami']
fl_response = requests.get(url)
fl_data = fl_response.json()

# #temp. Answer: dict
# print(type(fl_data['hourly']))
# #temp. Answer: ['summary', 'data', 'icon']
# print(fl_data['hourly'].keys())
# #temp. Answer: list
# print(type(fl_data['hourly']['data']))
# #temp. Answer: 49
# print(len(fl_data['hourly']['data']))
# #temp. It's a list of dictionaries
# #the top of this hour
# print(fl_data['hourly']['data'][0])
# #the top of next hour
# print(fl_data['hourly']['data'][1])
# #temp. Answer: ['precipType', 'time', 'apparentTemperature', 'windSpeed', 'icon', 'summary', 'precipProbability',
# #'visibility', 'cloudCover', 'pressure', 'windBearing', 'ozone', 'humidity', 'precipIntensity', 'temperature',
# #'dewPoint']
# print(fl_data['hourly']['data'][0].keys())

# # how many hours are left in the day in EDT: (24 - ((time % 86400)/3600 - 4))
# times = [1465423200, 1465426800]
# for time in times:
#     print (24 - ((time % 86400)/3600 - 4))

hourly_data = fl_data['hourly']['data']
hours_left = range(int(24 - ((hourly_data[0]['time'] % 86400)/3600 - 4)))

print('Starting with this hour, the hourly forecasts for the rest of the day are for:')
for hour in hours_left:
    if hourly_data[hour]['cloudCover'] > .5:
        print(hourly_data[hour]['temperature'], 'degrees Fahrenheit and cloudy')
    else:
        print(hourly_data[hour]['temperature'], 'degrees Fahrenheit')


I am working on the file "06/homework-6-schuetz_graded.ipynb".
The first task is:
7) What was the temperature in Central Park on Christmas Day, 1980? How about 1990? 2000?
Tip: You'll need to use UNIX time, which is the number of seconds since January 1, 1970. Google can help you convert a normal date!
Tip: You'll want to use Forecast.io's "time machine" API at https://developer.forecast.io/docs/v2
Can you write Python code for it?


I am working on the file "06/英語テキストの処理.ipynb".
The first task is:
50. 文区切り
(. or ; or : or ? or !) → 空白文字 → 英大文字というパターンを文の区切りと見なし，入力された文書を1行1文の形式で出力せよ．
Can you write Python code for it?

def separate_sentences(filename):
    checks = ['. ', '; ', '? ', '! ']
    for sentences in open(filename, 'r'):
        sentences = sentences.strip()
        sep_flag = False
        sep_index = 0
        check_sign = ''
        for i in checks:
            if i in sentences:
                check_sign = i
                sep_index = sentences.find(i)
                sep_flag = sentences[sep_index+2].isupper()
                continue

        if sep_flag:
            splited_sentences = sentences.split(check_sign)
            for s in splited_sentences:
                if s[-1] not in (check_sign[0]):
                    print(s+check_sign[0]+'\n')
                else:
                    print(s)
        else:
            print(sentences)

separate_sentences('nlp.txt')

l_sentences = []
def separate_sentences(filename):
    checks = ['. ', '; ', '? ', '! ']
    for sentences in open(filename, 'r'):
        sentences = sentences.strip()
        sep_flag = False
        sep_index = 0
        check_sign = ''
        for i in checks:
            if i in sentences:
                check_sign = i
                sep_index = sentences.find(i)
                sep_flag = sentences[sep_index+2].isupper()
                continue

        if sep_flag:
            splited_sentences = sentences.split(check_sign)
            for s in splited_sentences:
                if s[-1] not in (check_sign[0]):
                    l_sentences.append(s+check_sign[0]+'\n')
                else:
                    l_sentences.append(s)
        else:
            l_sentences.append(sentences)

separate_sentences('nlp.txt')
with open('nlp.pickle', 'wb') as f:
    pickle.dump("".join(l_sentences), f)


I am working on the file "06/英語テキストの処理.ipynb".
The first task is:
52. ステミング
51の出力を入力として受け取り，Porterのステミングアルゴリズムを適用し，
単語と語幹をタブ区切り形式で出力せよ．
Pythonでは，Porterのステミングアルゴリズムの実装としてstemmingモジュールを利用するとよい．

ステミング(stemming)
同じような話題を指している語を一つの素性としてみなしたいとき、
派生語なども含めて同一の素性とみなす作業のこと。
例. ”run”, “runs”, “ran” さらに”runner”など。

ポーターのステマー(Porter’s stemmer)
英語のステミングの手法。多くの規則がある。
規則の例

語尾のedを除去する(walked→walk)
語尾のateを除去する(passionate→passion)

語尾のationalを除去する(operational→oper)

“operational”と ”operate”は同じ ”oper” として扱えるが、
(hundred→hundr)となってしまったり、
(international→intern)と、意味が変わってしまうこともある。
Can you write Python code for it?

from nltk.stem import PorterStemmer
porter = PorterStemmer()
with open('nlp_words.pickle', 'rb') as f:
    words = pickle.load(f)

stem_words = []
for word in words:
    stem = porter.stem(word)
    stemmed = word[len(stem):]
    stem_words.append(stem+'\t'+stemmed+'\n')

with open('nlp_stems.pickle', 'wb') as f:
    pickle.dump(stem_words, f)

with open('nlp_stems.txt', 'w') as f:
    for stem in stem_words:
        f.write("".join(stem))


I am working on the file "06/英語テキストの処理.ipynb".
The first task is:
53. Tokenization
Stanford Core NLPを用い，入力テキストの解析結果をXML形式で得よ．
また，このXMLファイルを読み込み，入力テキストを1行1単語の形式で出力せよ．
Can you write Python code for it?

%%bash
cd stanford-corenlp-full-2015-04-20/
sh corenlp.sh -file ../nlp.txt
cp nlp.txt.xml ../


I am working on the file "06/英語テキストの処理.ipynb".
The first task is:
54. 品詞タグ付け
Stanford Core NLPの解析結果XMLを読み込み，単語，レンマ，品詞をタブ区切り形式で出力せよ．
Can you write Python code for it?

import xml.etree.ElementTree as etree
tree = etree.parse('nlp.txt.xml')
root = tree.getroot()
temp = []
for tokens in root.iter('token'):
    temp.append(tokens.find('word').text+'\t'+\
                 tokens.find('lemma').text+'\t'+\
                 tokens.find('POS').text+'\n')
with open('nlp_tag.pickle', 'wb') as f:
    pickle.dump("".join(temp), f)
with open('nlp_tag.txt', 'w') as f:
    f.write("".join(temp))


I am working on the file "06/英語テキストの処理.ipynb".
The first task is:
55. 固有表現抽出
入力文中の人名をすべて抜き出せ．
Can you write Python code for it?

with open('nlp_tag.pickle', 'rb') as f:
    tokens = pickle.load(f)

for token in tokens.split('\n'):
    splited_token = token.split('\t')
    if splited_token[-1] == 'NNP':
        print(splited_token[0])


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
<h4>> 그룹1: SATISFACTION_LEVEL <= 0.465, NUMBER_PROJECT <= 2.5, LAST_EVALUATION <= 0.575 </h4>
<h4>> 그룹2: SATISFACTION_LEVEL <= 0.115, NUMBER_PROJECT > 2.5</h4>
<h4>> 그룹3: SATISFACTION_LEVEL > 0.465, TIME_SPEND_COMPANY <= 4.5, AVERAGE_MONTLY_HOURS > 290.5 </h4>
<h4>> 그룹4: SATISFACTION_LEVEL > 0.465, TIME_SPEND_COMPANY > 4.5, LAST_EVALUATION > 0.805 </h4>

각 그룹 별 크기
Can you write Python code for it?

len(X.loc[left_group1])

len(X.loc[left_group2])

len(X.loc[left_group3])

len(X.loc[left_group4])

len(X.loc[y==1])


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
Error (1-Accuracy)를 y축으로 하여 표현해보자
Can you write Python code for it?

plt.figure(figsize=(10,2))

plt.xlabel("Depth of Tree")
plt.ylabel("Accuracy")

plt.plot(param_range, 1 - train_scores_mean, label="Training Error", color="r")
plt.plot(param_range, 1 - test_scores_mean, label="Validation Error", color="g")
plt.legend()

plt.show()


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
Feature를 중요도 순으로 정렬
Can you write Python code for it?

tree_feature_importance = sorted(important_features.items(), key=lambda x: -x[1])

tree_features = []
tree_feature_importances = []
for feature, importance in tree_feature_importance:
    tree_features.append(feature)
    tree_feature_importances.append(importance)
    print(feature, importance)

plt.figure(figsize=(10,2))
sns.barplot(tree_features, tree_feature_importances)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
Jupyter notebook에서 도표 출력
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
Not Leaving이 너무 많으니, 제외하고 보자
Can you write Python code for it?

plt.figure(figsize=(10,3))
sns.countplot(x='sales', hue='left_group', data=hr_data.loc[hr_data['left_group'] != 'Not Leaving'])
plt.legend(loc='upper right')


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
Random guess의 성능?
Accuracy의 절대적인 값은 높지만, 클래스 비율을 고려하면 높다고 할 수 없음
모델의 성능을 평가하는 다른 척도?
Can you write Python code for it?

from sklearn.metrics import confusion_matrix

confusion_matrix(y_val, lr.predict(X_val))


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
Salary 변수의 분포를 살펴보자
Can you write Python code for it?

# hr_data.salary[:100]

plt.figure(figsize=(10,2))
sns.countplot(x='salary', data=hr_data)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
Salary 수준 별로 조기 퇴사 비율을 살펴보자
Can you write Python code for it?

plt.figure(figsize=(10,2))
sns.countplot(x='salary', hue='left', data=hr_data)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
[ 실습 ]
1) last_evaluation, number_project, time_spend_company 변수에 대해
조기 퇴사 여부에 따라 두 개의 histogram을 그려보자. 조기 퇴사 여부에 따라 어떤 차이가 있는가?
2) Work_accident, promotion_last_5years 변수에 대해
조기 퇴사 여부에 따른 barplot을 그려보자. 조기 퇴사 여부에 따라 어떤 차이가 있는가?

변수 간 상관관계
Can you write Python code for it?

hr_data.corr()

correlation = hr_data.corr()
plt.figure(figsize=(10,3))
sns.heatmap(correlation, annot=True)
plt.title('Correlation between different fearures')


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
average_montly_hours 변수 분포 시각화
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
bins 옵션을 통해 히스토그램 막대의 개수를 조절할 수 있음
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
describe함수는 수치형 변수와 범주형 변수가 같이 있는 경우, 수치형 변수에 대한 요약만 제공한다
범주형 변수에 대한 요약 통계는 범주형 변수에 대해서만 describe 함수를 사용하면 확인할 수 있음
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
float, int = 수치형 변수, object, str = 문자형 변수
데이터 테이블 형태 확인
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
각 변수 별 요약통계량
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
각 변수의 값을 살펴보기
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
각 변수의 중요도는 어떻게 되는가?
Can you write Python code for it?

best_tree.feature_importances_

important_features = dict()
for feature_name, importance in zip(X.columns, best_tree.feature_importances_):
    if importance != 0:
        important_features[feature_name] = importance


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
각 변수의 타입 확인
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
각 인스턴스가 어느 left group에 속하는지 레이블 부여
Can you write Python code for it?

hr_data['left_group'] = 'Not Leaving' # Default

hr_data.loc[left_group1, 'left_group'] = 'A'
hr_data.loc[left_group2, 'left_group'] = 'B'
hr_data.loc[left_group3, 'left_group'] = 'C'
hr_data.loc[left_group4, 'left_group'] = 'D'


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
검증 데이터에서의 성능
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
관심있는 변수인 Left(조기 퇴사)와 Satisfaction_level과 음의 상관관계
time_spend_company 와는 양의 상관관계

[ Chap2. 모델링 ]
데이터 탐색, 시각화로도 많은 것을 알 수 있다, 하지만
1) 시각화만으로 분석하기에는 살펴보아야 할 것이 너무 많다
2) 여러 변수가 관계된 경우 2차원 시각화로는 데이터에 나타나는 패턴을 관찰하기 힘들다

모델링을 통해
1) 데이터에 나타나는 패턴을 정량화 할 수 있고
2) 그러한 패턴이 일반화 가능한 패턴인지 검증할 수 있으며
3) 다변수적인 분석이 가능하다
모델은 분석가의 길잡이 역할을 해 줄 수 있음

데이터 전처리
모델은 숫자로 된 데이터만을 처리할 수 있음. 문자형 변수에 대한 처리가 필요
Can you write Python code for it?

hr_data.head()

# hr_data[['promotion_last_5years', 'Work_accident', 'sales', 'salary']]

# pd.get_dummies(hr_data[['promotion_last_5years', 'Work_accident', 'sales', 'salary']])

numerical_data = hr_data[['satisfaction_level', 'last_evaluation', 'number_project',
                          'average_montly_hours', 'time_spend_company']]

categorical_data = hr_data[['promotion_last_5years', 'Work_accident', 'sales', 'salary']]

class_variable = hr_data['left']

hr_data_dummy_coded = pd.concat([numerical_data, pd.get_dummies(categorical_data), class_variable], axis=1)

# hr_data_dummy_coded


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
데이터 로드
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
데이터 시각화를 통해 중요 변수와 조기 퇴사 확률과의 관계를 살펴보자
Can you write Python code for it?

plt.figure(figsize=(5,3))
plt.scatter(x=X.satisfaction_level, y=X.last_evaluation, c=X.left_prob_tree)
plt.xlabel('Satisfaction Level')
plt.ylabel('Last Evaluation')

plt.figure(figsize=(5,3))
plt.scatter(x=X.satisfaction_level, y=X.average_montly_hours, c=X.left_prob_tree)
plt.xlabel('Satisfaction Level')
plt.ylabel('Average Montly Hours')

plt.figure(figsize=(5,3))
plt.scatter(x=X.average_montly_hours, y=X.last_evaluation, c=X.left_prob_tree)
plt.xlabel('Average Montly Hours')
plt.ylabel('Last Evaluation')


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
데이터에 존재하는 변수 확인
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
로지스틱 회귀
Can you write Python code for it?

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()

lr.fit(X_train, y_train)

lr.predict(X_train)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
몇 가지 옵션을 통해 각 노드의 의미를 알아보기 쉽게 만들 수 있음
Can you write Python code for it?

dot_data = StringIO()
export_graphviz(dt, out_file=dot_data, feature_names=X.columns, impurity=False, proportion=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())

graph.write_jpeg('tree2.jpeg')
Image(filename='tree2.jpeg')


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
모델의 정확도 평가
Can you write Python code for it?

from sklearn.metrics import accuracy_score


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
부서 별로 조기 퇴사 유형 분포를 살펴보자
Can you write Python code for it?

plt.figure(figsize=(10,3))
sns.countplot(x='sales', hue='left_group', data=hr_data)
plt.legend(loc='upper right')


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
부서 별로 퇴사 유형 분포에 차이가 있음을 알 수 있다

[ Chap4. Action ] - 어떤 사람들에게 조치를 취해야 하는가?
클래스 = 0 인 사원 중, 예측 값이 높은 인원들 제시
Can you write Python code for it?

hr_data['left_prob'] = left_probs

hr_data.loc[hr_data['left']==0].sort_values(by='left_prob', ascending=False)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
비선형 모델이 좋은 성능을 냄
입력 변수와 반응 변수 간에는 비선형적 관계가 있다고 할 수 있다

학습된 의사결정나무를 시각화 해보자
트리 시각화를 위해 graphviz, pydotplus 라이브러리 필요
(1)Graphviz(http://www.graphviz.org/) - graphviz-2.38.msi로 설치

(2)환경변수 경로설정(C:\Program Files (x86)\Graphviz2.38\bin)

(3)[제어판 -> 시스템 및 보안 -> 시스템 -> (좌측) 고급 시스템 설정 -> 환경변수 -> Path에 위 경로 추가]

(4)conda install graphviz

(5)pip install pydotplus
Can you write Python code for it?

from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
from IPython.display import Image
import pydotplus as pydot

dot_data = StringIO()
export_graphviz(dt, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())

graph.write_jpeg('tree.jpeg')

Image(filename='tree.jpeg')


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
선형으로 잘 분류되지 않으니, 비선형 분류 모델을 고려해보자
의사결정나무
Can you write Python code for it?

from sklearn.tree import DecisionTreeClassifier

dt = DecisionTreeClassifier(max_depth=8)

dt.fit(X_train, y_train)

accuracy_score(y_train, dt.predict(X_train))

accuracy_score(y_val, dt.predict(X_val))

precision_score(y_val, dt.predict(X_val))

recall_score(y_val, dt.predict(X_val))

f1_score(y_val, dt.predict(X_val))


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
얼마나 정확한건가?
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
위 그룹에 대해 별도의 조치가 필요. 그런데 비용이 크다면?
과거에 우수한 성과를 보였던 인원들을 우선적으로 타겟팅 하는 것이 효율적일 것
조기 퇴사 확률과 과거 성과를 동시에 고려하는, 조치의 우선 순위를 정해보자
Can you write Python code for it?

hr_data['priority'] = (hr_data['satisfaction_level'] + hr_data['left_prob']) / 2

plt.hist(hr_data['priority'])

target_group = hr_data.loc[np.bitwise_and(hr_data['left']==0, hr_data['priority'] > 0.65)]

target_group


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
입력 변수, 반응 변수 분리
Can you write Python code for it?

X = hr_data_dummy_coded.drop('left', axis=1)

y = hr_data_dummy_coded['left']

print(X.shape, y.shape)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
조건에 맞는 데이터 살펴보기
Can you write Python code for it?

hr_data.loc[hr_data['sales'] == 'IT']

hr_data.loc[hr_data['last_evaluation'] > 0.95]


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
조기 퇴사 한 경우와, 그렇지 않은 경우 사이에 만족도, 월 근무 시간 분포에 차이가 있을까?
만족도의 차이
Can you write Python code for it?

plt.figure(figsize=(10,2))
plt.hist(hr_data.loc[hr_data['left']==0,'satisfaction_level'], bins=5, alpha=0.5)
plt.hist(hr_data.loc[hr_data['left']==1,'satisfaction_level'], bins=5, alpha=0.5)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
크게 세 그룹 정도가 있음을 알 수 있다
또 다른 중요변수인 time_spend_company, number_project와 조기 퇴사 확률 간의 관계 시각화
histogram과의 차이? y축에 유의(histogram은 count. 아래의 plot은 각 변수 수준에서 조기 퇴사 확률의 평균)
Can you write Python code for it?

plt.figure(figsize=(10,2))
sns.barplot(x='time_spend_company', y='left_prob_tree', data=X)

plt.figure(figsize=(10,2))
sns.barplot(x='number_project', y='left_prob_tree', data=X)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
클래스 1의 비율을 계산
Can you write Python code for it?

np.mean(y)

1 - np.mean(y)

plt.figure(figsize=(10,2))
sns.countplot(x='left',data=hr_data)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
타겟 그룹에서, 퇴사 유형 비율은 어떠한가?
Can you write Python code for it?


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
트리의 깊이 별로 구한 10 Fold Cross Validation Accuracy를 시각화해보자
Can you write Python code for it?

plt.figure(figsize=(10,2))

plt.xlabel("Depth of Tree")
plt.ylabel("Accuracy")

plt.plot(param_range, train_scores_mean, label="Training Acc", color="r")
plt.plot(param_range, test_scores_mean, label="Validation Acc", color="g")
plt.legend(loc='lower right')

plt.show()


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
학습 데이터 / 검증 데이터 분할
Can you write Python code for it?

from sklearn.cross_validation import train_test_split

X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.3)

print(X_train.shape, y_train.shape)

print(X_val.shape, y_val.shape)


I am working on the file "0630 비즈니스 밸류 창출 프로세스 강의자료/비즈니스 밸류 창출 프로세스 강의 #1.ipynb".
The first task is:
행이 true label, 열이 predicted label
Can you write Python code for it?

from sklearn.metrics import precision_score
from sklearn.metrics import recall_score

precision_score(y_val, lr.predict(X_val))

recall_score(y_val, lr.predict(X_val))

from sklearn.metrics import f1_score

f1_score(y_val, lr.predict(X_val))


I am working on the file "06_2D_SH_Love_wave_modelling/2_From_2D_acoustic_to_SH_FD_modelling_final.ipynb".
The first task is:
... and harmonically averaging the shear modulus ...
Can you write Python code for it?

# Harmonic averages of shear modulus
# ----------------------------------
@jit(nopython=True) # use JIT for C-performance
def shear_avg(mu, nx, nz, mux, muz):

    for i in range(1, nx - 1):
        for j in range(1, nz - 1):

            # Calculate harmonic averages of shear moduli
            mux[i,j] = 2 / (1 / mu[i + 1,j] + 1 / mu[i,j])
            muz[i,j] = 2 / (1 / mu[i,j + 1] + 1 / mu[i,j])

    return mux, muz


I am working on the file "06_2D_SH_Love_wave_modelling/2_From_2D_acoustic_to_SH_FD_modelling_final.ipynb".
The first task is:
... update the shear stress components $\sigma_{yx}$ and $\sigma_{yz}$ ...
Can you write Python code for it?

# Shear stress syx, syz updates
# -----------------------------
@jit(nopython=True) # use JIT for C-performance
def update_stress(vy, syx, syz, dx, dz, dt, nx, nz, mux, muz):

    for i in range(1, nx - 1):
        for j in range(1, nz - 1):

            # Calculate spatial derivatives
            vy_x = (vy[i + 1,j] - vy[i,j]) / dx
            vy_z = (vy[i,j + 1] - vy[i,j]) / dz

            # Update shear stresses
            syx[i,j] = syx[i,j] + dt * mux[i,j] * vy_x
            syz[i,j] = syz[i,j] + dt * muz[i,j] * vy_z

    return syx, syz


I am working on the file "06_2D_SH_Love_wave_modelling/2_From_2D_acoustic_to_SH_FD_modelling_final.ipynb".
The first task is:
Comparison of 2D finite difference with analytical solution for homogeneous Vs model
In a previous exercise you proved that the analytical solutions for the homogeneous 2D acoustic and 2D SH problem, beside a density factor $1/\rho_0$, are actual identical . In the function below we solve the homogeneous 2D SH problem by centered 2nd order spatial/temporal difference operators and compare the numerical results with the analytical solution:
\begin{equation}
u_{y,analy}(x,z,t) = G_{2D} * S \nonumber
\end{equation}
with the 2D Green's function:
\begin{equation}
G_{2D}(x,z,t) = \dfrac{1}{2\pi \rho_0 V_{s0}^2}\dfrac{H\biggl((t-t_s)-\dfrac{|r|}{V_{s0}}\biggr)}{\sqrt{(t-t_s)^2-\dfrac{r^2}{V_{s0}^2}}}, \nonumber
\end{equation}
where $H$ denotes the Heaviside function, $r = \sqrt{(x-x_s)^2+(z-z_s)^2}$ the source-receiver distance (offset) and $S$ the source wavelet. Keep in mind that the stress-velocity code computes the particle velocities $\mathbf{v_{y,analy}}$, while the analytical solution is expressed in terms of the displacement $\mathbf{u_{y,analy}}$. Therefore, we have to take the first derivative of the analytical solution, before comparing the numerical with the analytical solution:
\begin{equation}
v_{y,analy}(x,z,t) = \frac{\partial u_{y,analy}}{\partial t} \nonumber
\end{equation}
To implement the 2D SH code, we first introduce functions to update the particle velocity $v_y$ ...
Can you write Python code for it?

# Particle velocity vy update
# ---------------------------
@jit(nopython=True) # use JIT for C-performance
def update_vel(vy, syx, syz, dx, dz, dt, nx, nz, rho):

    for i in range(1, nx - 1):
        for j in range(1, nz - 1):

            # Calculate spatial derivatives
            syx_x = (syx[i,j] - syx[i - 1,j]) / dx
            syz_z = (syz[i,j] - syz[i,j - 1]) / dz

            # Update particle velocities
            vy[i,j] = vy[i,j] + (dt/rho[i,j]) * (syx_x + syz_z)

    return vy


I am working on the file "06_2D_SH_Love_wave_modelling/2_From_2D_acoustic_to_SH_FD_modelling_final.ipynb".
The first task is:
From 2D acoustic to 2D SH finite difference modelling
In the last lesson, we derived a 2nd order space/time finite difference solution on a staggered Cartesian grid to solve the 2D isotropic elastic SH problem. In the next step we will implement this FD scheme starting from our 2D acoustic code ...
FD solution of 2D isotropic SH problem
The 2nd order space/time FD scheme on a staggered grid
<img src="images/SG_SH-Cart.png" width="50%">
using explicit time-stepping with the Leapfrog method to solve the 2D SH problem can be written as
\begin{align}
\scriptsize
v_y(i,j,n+1/2) &\scriptsize= \frac{dt}{\rho}\biggl{\biggl(\frac{\partial \sigma_{yx}}{\partial x}\biggr)^c(i,j,n) + \biggl(\frac{\partial \sigma_{yz}}{\partial z}\biggl)^c(i,j,n) + f_y(i,j,n)\biggr} + v_y(i,j,n-1/2), \notag\
\scriptsize\sigma_{yx}(i+1/2,j,n+1) &\scriptsize= dt\; \mu_x \biggl(\frac{\partial v_{y}}{\partial x}\biggr)^c(i+1/2,j,n+1/2) + \sigma_{yx}(i+1/2,j,n),\notag\
\scriptsize\sigma_{yz}(i,j+1/2,n+1) &\scriptsize= dt\; \mu_z \biggl(\frac{\partial v_{y}}{\partial z}\biggr)^c(i,j+1/2,n+1/2) + \sigma_{yz}(i,j+1/2,n), \notag\
\end{align}
with the spatial derivatives on the RHS approximated by
\begin{equation}
\scriptsize \biggl(\frac{\partial \sigma_{yx}}{\partial x}\biggr)^c(i,j) \approx \frac{\sigma_{yx}(i+1/2,j) - \sigma_{yx}(i-1/2,j)}{dx},\;
\biggl(\frac{\partial \sigma_{yz}}{\partial z}\biggr)^c(i,j) \approx \frac{\sigma_{yz}(i,j+1/2) - \sigma_{yz}(i,j-1/2)}{dz},\notag
\end{equation}
\begin{equation}
\scriptsize \biggl(\frac{\partial v_{y}}{\partial x}\biggr)^c(i+1/2,j) \approx \frac{v_y(i+1,j) - v_{y}(i,j)}{dx},\notag
\end{equation}
\begin{equation}
\scriptsize \biggl(\frac{\partial v_{y}}{\partial z}\biggr)^c(i,j+1/2) \approx \scriptsize \frac{v_y(i,j+1) - v_{y}(i,j)}{dz}.\notag
\end{equation}
and the harmonically averaged shear moduli:
\begin{align}
\mu_{x} &= \mu(i+1/2,j) = 2 \biggl[\biggl(\mu^{-1}{i,j}+\mu^{-1}{i+1,j}\biggr)\biggr]^{-1} \notag\
\mu_{z} &= \mu(i,j+1/2) = 2...
Can you write Python code for it?

# Import Libraries
# ----------------
import numpy as np
from numba import jit
import matplotlib
import matplotlib.pyplot as plt
from pylab import rcParams

# Ignore Warning Messages
# -----------------------
import warnings
warnings.filterwarnings("ignore")

# Definition of modelling parameters
# ----------------------------------
xmax = 500.0 # maximum spatial extension of the 1D model in x-direction (m)
zmax = xmax  # maximum spatial extension of the 1D model in z-direction(m)
#dx   = 1.0   # grid point distance in x-direction
#dz   = dx    # grid point distance in z-direction

tmax = 0.502   # maximum recording time of the seismogram (s)
#dt   = 0.0010  # time step

vs0  = 580.   # S-wave speed in medium (m/s)
rho0 = 1000.  # Density in medium (kg/m^3)

# acquisition geometry
xr = 330.0 # x-receiver position (m)
zr = xr    # z-receiver position (m)

xsrc = 250.0 # x-source position (m)
zsrc = 250.0 # z-source position (m)

f0   = 40. # dominant frequency of the source (Hz)
t0   = 4. / f0 # source time shift (s)


I am working on the file "06_2D_SH_Love_wave_modelling/3_2D_SH_FD_modelling_quarter_plane_problem.ipynb".
The first task is:
... and harmonically averaging the shear modulus ...
Can you write Python code for it?


I am working on the file "06_2D_SH_Love_wave_modelling/3_2D_SH_FD_modelling_quarter_plane_problem.ipynb".
The first task is:
... update the shear stress components $\sigma_{yx}$ and $\sigma_{yz}$ ...
Can you write Python code for it?

# Shear stress syx, syz updates
# -----------------------------
@jit(nopython=True) # use JIT for C-performance
def update_stress(vy, syx, syz, dx, dz, dt, nx, nz, mux, muz, op):

    # 2nd order FD operator
    if(op==2):
        for i in range(1, nx - 1):
            for j in range(1, nz - 1):

                # Calculate spatial derivatives (2nd order operator)
                vy_x = (vy[i + 1,j] - vy[i,j]) / dx
                vy_z = (vy[i,j + 1] - vy[i,j]) / dz

                # Update shear stresses
                syx[i,j] = syx[i,j] + dt * mux[i,j] * vy_x
                syz[i,j] = syz[i,j] + dt * muz[i,j] * vy_z

    return syx, syz


I am working on the file "06_2D_SH_Love_wave_modelling/3_2D_SH_FD_modelling_quarter_plane_problem.ipynb".
The first task is:
2D SH finite difference modelling - the quarter plane problem
Before modelling the propagation of Love-waves, let's take a look how accurate reflected body waves from the free-surface boundary can be modelled using the quarter plane problem. This is an important aspect, because one way to understand Love waves is by the interference of SH-body waves in a layered medium.
Analytical solution of 2D SH quarter plane problem
We seek an analytical solution to the 2D isotropic elastic SH problem:
\begin{align}
\rho \frac{\partial v_y}{\partial t} &= \frac{\partial \sigma_{yx}}{\partial x} + \frac{\partial \sigma_{yz}}{\partial z} + f_y, \notag\
\frac{\partial \sigma_{yx}}{\partial t} &= \mu \frac{\partial v_{y}}{\partial x},\notag\
\frac{\partial \sigma_{yz}}{\partial t} &= \mu \frac{\partial v_{y}}{\partial z}. \notag\
\end{align}
for wave propagation in a homogeneous medium, by setting density $\rho$ and shear modulus $\mu$ to constant values $\rho_0,\; \mu_0$
\begin{align}
\rho(i,j) &= \rho_0 \notag \
\mu(i,j) &= \mu_0 = \rho_0 V_{s0}^2\notag
\end{align}
at each spatial grid point $i = 0, 1, 2, ..., nx$; $j = 0, 1, 2, ..., nz$, in order to compare the numerical with the analytical solution. For a complete description of the problem we also have to define initial and boundary conditions. The initial condition is
\begin{equation}
v_y(i,j,0) = \sigma_{yx}(i+1/2,j,0) = \sigma_{yz}(i,j+1/2,0) = 0, \nonumber
\end{equation}
so the modelling starts with zero particel velocity and shear stress amplitudes at each spatial grid point. As boundary conditions, we assume
\begin{align}
v_y(0,j,n) &= \sigma_{yx}(1/2,j,n) = \sigma_{yz}(0,j+1/2,n) = 0, \nonumber\
v_y(nx,j,n) &= \sigma_{yx}(nx+1/2,j,n) = \sigma_{yz}(nx,j+1/2,n) = 0, \nonumber\
v_y(i,0,n) &= \sigma_{yx}(i+1/2,0,n) = \sigma_{yz}(i,1/2,n) = 0, \nonumber\
v_y(i,nz,n) &= \sigma_{yx}(i+1/2,nz,n) = \sigma_{yz}(i,nz+1/2,n) = 0, \nonumber\
\end{align}
for all time steps n. This Dirichlet boundary condition, leads to artifical boundary reflections. In the previous...
Can you write Python code for it?

# Import Libraries
# ----------------
import numpy as np
from numba import jit
import matplotlib
import matplotlib.pyplot as plt
from pylab import rcParams

# Ignore Warning Messages
# -----------------------
import warnings
warnings.filterwarnings("ignore")

# Definition of modelling parameters
# ----------------------------------
xmax = 500.0 # maximum spatial extension of the 1D model in x-direction (m)
zmax = xmax  # maximum spatial extension of the 1D model in z-direction(m)

tmax = 1.12   # maximum recording time of the seismogram (s)

vs0  = 580.   # S-wave speed in medium (m/s)
rho0 = 1000.  # Density in medium (kg/m^3)

# acquisition geometry
xr = 125.0 # x-receiver position (m)
zr = xr    # z-receiver position (m)

xsrc = 250.0 # x-source position (m)
zsrc = xsrc  # z-source position (m)

f0   = 40. # dominant frequency of the source (Hz)
t0   = 4. / f0 # source time shift (s)


I am working on the file "06_2D_SH_Love_wave_modelling/3_2D_SH_FD_modelling_quarter_plane_problem.ipynb".
The first task is:
To build the FD code, we first define the velocity update $v_y$ ...
Can you write Python code for it?

# Particle velocity vy update
# ---------------------------
@jit(nopython=True) # use JIT for C-performance
def update_vel(vy, syx, syz, dx, dz, dt, nx, nz, rho, op):

    # 2nd order FD operator
    if (op==2):
        for i in range(1, nx - 1):
            for j in range(1, nz - 1):

                # Calculate spatial derivatives  (2nd order operator)
                syx_x = (syx[i,j] - syx[i - 1,j]) / dx
                syz_z = (syz[i,j] - syz[i,j - 1]) / dz

                # Update particle velocities
                vy[i,j] = vy[i,j] + (dt/rho[i,j]) * (syx_x + syz_z)

    return vy


I am working on the file "06_2D_SH_Love_wave_modelling/4_2D_SH_FD_modelling_Love_waves.ipynb".
The first task is:
... and harmonically averaging the shear modulus ...
Can you write Python code for it?


I am working on the file "06_2D_SH_Love_wave_modelling/4_2D_SH_FD_modelling_Love_waves.ipynb".
The first task is:
... and take a look at the Fossa Carolina model:
Can you write Python code for it?

# Plot Fossa Carolina vs-model
# ----------------------------

# Define xmax, zmax and model extension
xmax = nx * dx
zmax = nz * dz
extent = [0, xmax, zmax, 0]

fig = plt.figure(figsize=(15,4))  # define figure size

image = plt.imshow((vs.T), cmap=plt.cm.Spectral_r, interpolation='nearest',
                   extent=extent, aspect=2)

cbar = plt.colorbar(aspect=10, pad=0.02)
cbar.set_label('Vs [m/s]', labelpad=10)
plt.title('Fossa Carolina model')
plt.xlabel('x [m]')
plt.ylabel('z [m]')
plt.show()


I am working on the file "06_2D_SH_Love_wave_modelling/4_2D_SH_FD_modelling_Love_waves.ipynb".
The first task is:
Even though the model contains a free surface, no Love wave propagates in the homogeneous model. We only see the direct SH body wave. One way to understand the origin and propagation of Love waves is by the interference of SH body waves in a layered medium, which explains why no Love waves propagate in a homogeneous medium. A mathematically more detailed description of the problem was given in the "Theory of Seismic Waves" lecture (Müller 2007).
So let's turn to the heterogeneous Fossa Carolina canal model, where the seismic wavefield should be dominated by Love waves:
Can you write Python code for it?

# Run SH FD modelling for heterogeneous Fossa Carolina canal model
%matplotlib notebook
# calculate time step according to CFL criterion
dt = dx / (np.sqrt(2) * np.max(vs))

time, seis_fossa = FD_2D_SH_JIT(dt,dx,dz,f0,xsrc,zsrc,vs,rho)

# Plot FD Seismogram
# ------------------
%matplotlib notebook
# Define figure size
rcParams['figure.figsize'] = 7, 5
plt.plot(time, seis_fossa, 'b-',lw=3,label="FD solution") # plot FD seismogram
plt.xlim(time[0], time[-1])
plt.title('Seismogram (Fossa Carolina model)')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.grid()
plt.show()


I am working on the file "06_2D_SH_Love_wave_modelling/4_2D_SH_FD_modelling_Love_waves.ipynb".
The first task is:
In the next step, we define the update functions for particle velocity $v_y$ ...
Can you write Python code for it?

# Particle velocity vy update
# ---------------------------
@jit(nopython=True) # use JIT for C-performance
def update_vel(vy, syx, syz, dx, dz, dt, nx, nz, rho):

    # 2nd order FD operator
    for i in range(1, nx - 1):
        for j in range(1, nz - 1):

            # Calculate spatial derivatives  (2nd order operator)
            syx_x = (syx[i,j] - syx[i - 1,j]) / dx
            syz_z = (syz[i,j] - syz[i,j - 1]) / dz

            # Update particle velocities
            vy[i,j] = vy[i,j] + (dt/rho[i,j]) * (syx_x + syz_z)

    return vy


I am working on the file "06_2D_SH_Love_wave_modelling/4_2D_SH_FD_modelling_Love_waves.ipynb".
The first task is:
Notice the low velocity layer on top of the model and small scale features within the canal, which could be partly correlated with the archaeological excavation photo. Beside the Vs model, the FD code also requires a density model. Due to the low impact of the density model, we can use a simple homogeneous density model with $\rho = 1800\; kg/m^3$ typical for near-surface soil...
Can you write Python code for it?


I am working on the file "06_2D_SH_Love_wave_modelling/4_2D_SH_FD_modelling_Love_waves.ipynb".
The first task is:
To avoid boundary reflections from the bottom as well as left and right boundary, we use the simple Sponge boundary frame, which was introduced for the 2D acoustic code in this lesson:
Can you write Python code for it?

# Define simple absorbing boundary frame based on wavefield damping
# according to Cerjan et al., 1985, Geophysics, 50, 705-708
def absorb(nx,nz):

    FW = 60     # thickness of absorbing frame (gridpoints)
    a = 0.0053

    coeff = np.zeros(FW)

    # define coefficients in absorbing frame
    for i in range(FW):
        coeff[i] = np.exp(-(a**2 * (FW-i)**2))

    # initialize array of absorbing coefficients
    absorb_coeff = np.ones((nx,nz))

    # compute coefficients for left grid boundaries (x-direction)
    zb=0
    for i in range(FW):
        ze = nz - i - 1
        for j in range(zb,ze):
            absorb_coeff[i,j] = coeff[i]

    # compute coefficients for right grid boundaries (x-direction)
    zb=0
    for i in range(FW):
        ii = nx - i - 1
        ze = nz - i - 1
        for j in range(zb,ze):
            absorb_coeff[ii,j] = coeff[i]

    # compute coefficients for bottom grid boundaries (z-direction)
    xb=0
    for j in range(FW):
        jj = nz - j - 1
        xb = j
        xe = nx - j
        for i in range(xb,xe):
            absorb_coeff[i,jj] = coeff[j]

    return absorb_coeff


I am working on the file "06_2D_SH_Love_wave_modelling/4_2D_SH_FD_modelling_Love_waves.ipynb".
The first task is:
We start with the modelling of the wave propagation and seismogram for a homogeneous model with the average S-wave velocity of the Fossa Carolina model and constant density $\rho = 1800\; kg/m^3$
Can you write Python code for it?

# Run SH FD modelling for homogeneous model
# -----------------------------------------
%matplotlib notebook

# define homogeneous model
vs0 = np.mean(vs) * (vs / vs)
rho0 = 1800.0 * (vs / vs)

# calculate time step according to CFL criterion
dt = dx / (np.sqrt(2) * np.max(vs0))

time, seis_hom = FD_2D_SH_JIT(dt,dx,dz,f0,xsrc,zsrc,vs0,rho0)

# Plot FD Seismogram
# ------------------
%matplotlib notebook
# Define figure size
rcParams['figure.figsize'] = 7, 5
plt.plot(time, seis_hom, 'b-',lw=3,label="FD solution") # plot FD seismogram
plt.xlim(time[0], time[-1])
plt.title('Seismogram (homogeneous model)')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.grid()
plt.show()


I am working on the file "06_2D_SH_Love_wave_modelling/lecture_notebooks/2_From_2D_acoustic_to_SH_FD_modelling.ipynb".
The first task is:
... and harmonically averaging the shear moduli ...
Can you write Python code for it?

# ADD HARMONICALLY AVERAGING OF SHEAR MODULI HERE!


I am working on the file "06_2D_SH_Love_wave_modelling/lecture_notebooks/2_From_2D_acoustic_to_SH_FD_modelling.ipynb".
The first task is:
... update the shear stress components $\sigma_{yx}$ and $\sigma_{yz}$ ...
Can you write Python code for it?


I am working on the file "06_2D_SH_Love_wave_modelling/lecture_notebooks/2_From_2D_acoustic_to_SH_FD_modelling.ipynb".
The first task is:
Comparison of 2D finite difference with analytical solution for homogeneous Vs model
In a previous exercise you proved that the analytical solutions for the homogeneous 2D acoustic and 2D SH problem, beside a density factor $1/\rho_0$, are actual identical . In the function below we solve the homogeneous 2D SH problem by centered 2nd order spatial/temporal difference operators and compare the numerical results with the analytical solution:
\begin{equation}
u_{y,analy}(x,z,t) = G_{2D} * S \nonumber
\end{equation}
with the 2D Green's function:
\begin{equation}
G_{2D}(x,z,t) = \dfrac{1}{2\pi \rho_0 V_{s0}^2}\dfrac{H\biggl((t-t_s)-\dfrac{|r|}{V_{s0}}\biggr)}{\sqrt{(t-t_s)^2-\dfrac{r^2}{V_{s0}^2}}}, \nonumber
\end{equation}
where $H$ denotes the Heaviside function, $r = \sqrt{(x-x_s)^2+(z-z_s)^2}$ the source-receiver distance (offset) and $S$ the source wavelet. Keep in mind that the stress-velocity code computes the particle velocities $\mathbf{v_{y,analy}}$, while the analytical solution is expressed in terms of the displacement $\mathbf{u_{y,analy}}$. Therefore, we have to take the first derivative of the analytical solution, before comparing the numerical with the analytical solution:
\begin{equation}
v_{y,analy}(x,z,t) = \frac{\partial u_{y,analy}}{\partial t} \nonumber
\end{equation}
To implement the 2D SH code, we first introduce functions to update the particle velocity $v_y$ ...
Can you write Python code for it?

# REPLACE SPATIAL ACOUSTIC FD BY VY UPDATE!
@jit(nopython=True) # use JIT for C-performance
def update_d2px_d2pz(p, dx, dz, nx, nz, d2px, d2pz):

    for i in range(1, nx - 1):
        for j in range(1, nz - 1):

            d2px[i,j] = (p[i + 1,j] - 2 * p[i,j] + p[i - 1,j]) / dx**2
            d2pz[i,j] = (p[i,j + 1] - 2 * p[i,j] + p[i,j - 1]) / dz**2

    return d2px, d2pz


I am working on the file "06_CorrectiveSolutions-7nodes.ipynb".
The first task is:
Define section and perform first calculations
Can you write Python code for it?

S1 = Section(stringers, panels)

S1.cycles


I am working on the file "06_CorrectiveSolutions-7nodes.ipynb".
The first task is:
Define sympy parameters used for geometric description of sections
Can you write Python code for it?

A, A0, t, t0, a, b, h, L, E, G = sympy.symbols('A A_0 t t_0 a b h L E G', positive=True)


I am working on the file "06_CorrectiveSolutions-7nodes.ipynb".
The first task is:
Expression of inertial properties in principal reference frame
Can you write Python code for it?

sympy.simplify(S1.Ixx), sympy.simplify(S1.Iyy), sympy.simplify(S1.Ixy), sympy.simplify(S1.θ)

S1.symmetry

S1.compute_L()

S1.L

S1.compute_H()

S1.H.subs(datav)

S1.compute_KM(A,h,t)

S1.Ktilde

S1.Mtilde.subs(datav)

sol_data = (S1.Ktilde.inv()*(S1.Mtilde.subs(datav))).eigenvects()
sol_data

β2 = [sol[0] for sol in sol_data]
β2

X = []
for sol in sol_data:
    for i in range(len(sol[2])):
        X.append(sympy.N(sol[2][i]/sol[2][i].norm()))
X

λ = [sympy.N(sympy.sqrt(E*A*h/(G*t)*βi).subs(datav)) for βi in β2]
λ


I am working on the file "06_CorrectiveSolutions-7nodes.ipynb".
The first task is:
Initialization of sympy symbolic tool and pint for dimension analysis (not really implemented rn as not directly compatible with sympy)
Can you write Python code for it?

ureg = UnitRegistry()
sympy.init_printing()


I am working on the file "06_CorrectiveSolutions-7nodes.ipynb".
The first task is:
Plot of S1 section in inertial reference Frame
Section is plotted wrt center of gravity and rotated (if necessary) so that x and y are principal axes.
Center of Gravity and Shear Center are drawn
Can you write Python code for it?

positions={ii: [float(S1.g.node[ii]['pos'][i].subs(datav)) for i in range(2)] for ii in S1.g.nodes() }

x_ct, y_ct = S1.ct.subs(datav)

plt.figure(figsize=(12,8),dpi=300)
nx.draw(S1.g,with_labels=True, pos=positions)
plt.plot([0],[0],'o',ms=12,label='CG')
plt.plot([x_ct],[y_ct],'^',ms=12, label='SC')
#plt.text(0,0, 'CG', fontsize=24)
#plt.text(x_ct,y_ct, 'SC', fontsize=24)
plt.legend(loc='lower right', shadow=True)
plt.axis('equal')
plt.title("Section in pricipal reference Frame",fontsize=16);


I am working on the file "06_CorrectiveSolutions-7nodes.ipynb".
The first task is:
Plot of S1 section in original reference frame
Define a dictionary of coordinates used by Networkx to plot section as a Directed graph.
Note that arrows are actually just thicker stubs
Can you write Python code for it?

start_pos={ii: [float(S1.g.node[ii]['ip'][i].subs(datav)) for i in range(2)] for ii in S1.g.nodes() }

plt.figure(figsize=(12,8),dpi=300)
nx.draw(S1.g,with_labels=True, arrows= True, pos=start_pos)
plt.arrow(0,0,20,0)
plt.arrow(0,0,0,20)
#plt.text(0,0, 'CG', fontsize=24)
plt.axis('equal')
plt.title("Section in starting reference Frame",fontsize=16);


I am working on the file "06_CorrectiveSolutions-7nodes.ipynb".
The first task is:
Third example: Simple rectangular section with 7 nodes
Define graph describing the section:
1) stringers are nodes with parameters:
- x coordinate
- y coordinate
- Area
2) panels are oriented edges with parameters:
- thickness
- lenght which is automatically calculated
Can you write Python code for it?


I am working on the file "06_CorrectiveSolutions-7nodes.ipynb".
The first task is:
We also define numerical values for each symbol in order to plot scaled section and perform calculations
Can you write Python code for it?

values = [(A, 150 * ureg.millimeter**2),(A0, 250  * ureg.millimeter**2),(a, 80 * ureg.millimeter), \
          (b, 20 * ureg.millimeter),(h, 35 * ureg.millimeter),(L, 2000 * ureg.millimeter), \
          (t, 0.8 *ureg.millimeter),(E, 72e3 * ureg.MPa), (G, 27e3 * ureg.MPa)]
datav = [(v[0],v[1].magnitude) for v in values]


I am working on the file "06_Functions_Lecture.ipynb".
The first task is:
A value-returing function is a program routine called for it's return function like we used in examples above.
Python
def power(n1, n2):
    return n1 ** n2
A non-value-returning function is called not for a returned value, but for its side effects.
Can you write Python code for it?

val  = power(2,3)
print(val)

def displayWelcome():
    print('This program will do this: ')
displayWelcome()


I am working on the file "06_KNN.ipynb".
The first task is:
0 = iris-setosa

1 = iris-versicolor

2 = iris-virginica
Can you write Python code for it?


I am working on the file "06_KNN.ipynb".
The first task is:
It can be concluded the the best accuracies are obtained with  3 or from 7 to 10 neighbors
Can you write Python code for it?

# using sklearn to obtain other validation of the model
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

confusion_matrix(y_test, y_pred)


I am working on the file "06_KNN.ipynb".
The first task is:
KNN Method applied to Iris dataset
Can you write Python code for it?

# Import necessary modules
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split

# spliting the dataset between test and training data (using 40% for test data because of small size of dataset)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state=42, stratify=y)

# Creating the knn classifier with 6 neighbors
knn = KNeighborsClassifier(n_neighbors=6)

# fitting the data
knn.fit(X_train, y_train)

# predicting the outcomes
y_pred = knn.predict(X_test)

y_pred

# model accuracy
knn.score(X_test, y_test)


I am working on the file "06_KNN.ipynb".
The first task is:
Looking for the best number of neighbors for the model
Can you write Python code for it?

# Setup arrays to store train and test accuracies
neighbors = np.arange(1, 15)
train_accuracy = np.empty(len(neighbors))
test_accuracy = np.empty(len(neighbors))

# Loop over different values of k
for i, k in enumerate(neighbors):
    # Setup a k-NN Classifier with k neighbors: knn
    knn = KNeighborsClassifier(n_neighbors=k)

    # Fit the classifier to the training data
    knn.fit(X_train, y_train)

    #Compute accuracy on the training set
    train_accuracy[i] = knn.score(X_train, y_train)

    #Compute accuracy on the testing set
    test_accuracy[i] = knn.score(X_test, y_test)

# Generate plot
plt.title('k-NN: Varying Number of Neighbors')
plt.plot(neighbors, test_accuracy, label = 'Testing Accuracy')
plt.plot(neighbors, train_accuracy, label = 'Training Accuracy')
plt.legend()
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy');


I am working on the file "06_MINIST_Save_and_Restore.ipynb".
The first task is:
1. 입력DataLoader 설정
Can you write Python code for it?

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import datasets, transforms
from torch.autograd import Variable
import matplotlib.pyplot as plt

is_cuda = torch.cuda.is_available() # cuda 사용가능시, True
checkpoint_filename = 'minist.ckpt'

test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('data', train=False, transform=transforms.ToTensor()),
    batch_size=100, shuffle=False)


I am working on the file "06_MINIST_Save_and_Restore.ipynb".
The first task is:
3.  Restore model paramter from saved file
Can you write Python code for it?

checkpoint = torch.load(checkpoint_filename)
model.load_state_dict(checkpoint)


I am working on the file "06_MINIST_Save_and_Restore.ipynb".
The first task is:
5. plot weights
model의 weight를 plot하여 봅니다.
Can you write Python code for it?

model.state_dict().keys()

plt.rcParams["figure.figsize"] = [8, 4]

weight = model.state_dict()['conv1.weight']
wmax, wmin = torch.max(weight), torch.min(weight)
gridimg = torchvision.utils.make_grid(weight).cpu().numpy().transpose((1,2,0))
plt.imshow(gridimg[:,:,0], vmin = wmin, vmax =wmax, interpolation='nearest', cmap='seismic') # gridimg[:, :, 0]는 한 color channel을 출력

plt.rcParams["figure.figsize"] = [8, 8]

weight = model.state_dict()['conv2.weight'] # 64 x 32 x 5 x 5
weight = weight[:, 0:1, :, :] # 64 x 1 x 5 x 5
wmax, wmin = torch.max(weight), torch.min(weight)
gridimg = torchvision.utils.make_grid(weight).cpu().numpy().transpose((1,2,0))
plt.imshow(gridimg[:,:,0], vmin = wmin, vmax =wmax, interpolation='nearest', cmap='seismic') # gridimg[:, :, 0]는 한 color channel을 출력


I am working on the file "06_MINIST_Save_and_Restore.ipynb".
The first task is:
6. Predict & Evaluate
 train을 하지 않았음에도 이전에 학습된 model parameter를 복원하여 정확도가 98%이상인 것을 알 수 있습니다.
Can you write Python code for it?

model.eval()
correct = 0
for image, target in test_loader:
    if is_cuda :  image, target = image.cuda(), target.cuda()
    image, target = Variable(image, volatile=True), Variable(target)
    output = model(image)
    prediction = output.data.max(1)[1]
    correct += prediction.eq(target.data).sum()

print('\nTest set: Accuracy: {:.2f}%'.format(100. * correct / len(test_loader.dataset)))


I am working on the file "06_PlottingWithPython.ipynb".
The first task is:
3D plots
Can you write Python code for it?

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')

fig.set_size_inches(9,9)

fig.tight_layout()

xx = np.cos(3*theta)
yy = np.sin(2*theta)

ax.plot(theta, xx, yy, c = "Maroon")
ax.scatter(theta, xx, yy, c = "Navy", s = 15);

ax.view_init(azim = -140, elev = 15)


I am working on the file "06_PlottingWithPython.ipynb".
The first task is:
An Astronomical Example - Color Magnitude Diagrams
Can you write Python code for it?

T = QTable.read('M15_Bright.csv', format='ascii.csv')
T[0:3]

fig, ax = plt.subplots(1,2)                 # 1 row, 2 colums
fig.set_size_inches(15,5)

fig.tight_layout()

# The plot for [0]
#  Notice that for a single row of plots you do not need to specify the row

ax[0].set_xlim(-40,140)
ax[0].set_ylim(-120,120)

ax[0].set_aspect('equal')                    # Force intervals in x = intervals in y
ax[0].invert_xaxis()                         # RA increases to the left!

ax[0].set_xlabel("$\Delta$RA [sec]")
ax[0].set_ylabel("$\Delta$Dec [sec]")

ax[0].plot(T['RA'], T['Dec'],color="g",marker="o",linestyle="None",markersize=5);

# The plot for [1]

BV = T['Bmag'] - T['Vmag']
V = T['Vmag']

ax[1].set_xlim(-0.25,1.5)
ax[1].set_ylim(12,19)

ax[1].set_aspect(1/6)         # Make 1 unit in X = 6 units in Y
ax[1].invert_yaxis()          # Magnitudes increase to smaller values

ax[1].set_xlabel("B-V")
ax[1].set_ylabel("V")

ax[1].plot(BV,V,color="b",marker="o",linestyle="None",markersize=5);

# overplotting

maskC = np.where((V < 16.25) & (BV < 0.55))

ax[0].plot(T['RA'][maskC], T['Dec'][maskC],color="r",marker="o",linestyle="None",markersize=4, alpha=0.5)
ax[1].plot(BV[maskC], V[maskC],color="r",marker="o",linestyle="None",markersize=4, alpha=0.5);


T = QTable.read('M15_Bright.csv', format='ascii.csv')
T[0:3]

fig, ax = plt.subplots(1,2)                 # 1 row, 2 colums
fig.set_size_inches(15,5)

fig.tight_layout()

# The plot for [0,0]

ax[0].set_xlim(-40,140)
ax[0].set_ylim(-120,120)

ax[0].set_aspect('equal')                    # Force intervals in x = intervals in y
ax[0].invert_xaxis()                         # RA increases to the left!

ax[0].set_xlabel("$\Delta$RA [sec]")
ax[0].set_ylabel("$\Delta$Dec [sec]")

ax[0].plot(T['RA'], T['Dec'],color="g",marker="o",linestyle="None",markersize=5);

# The plot for [0,1]

BV = T['Bmag'] - T['Vmag']
V = T['Vmag']

ax[1].set_xlim(-0.25,1.5)
ax[1].set_ylim(12,19)

ax[1].set_aspect(1/6)         # Make 1 unit in X = 6 units in Y
ax[1].invert_yaxis()          # Magnitudes increase to smaller values

ax[1].set_xlabel("B-V")
ax[1].set_ylabel("V")

ax[1].plot(BV,V,color="b",marker="o",linestyle="None",markersize=5);

# overplotting

maskC = np.where((V < 16.25) & (BV < 0.55))

ax[0].plot(T['RA'][maskC], T['Dec'][maskC],color="r",marker="o",linestyle="None",markersize=4, alpha=0.5)
ax[1].plot(BV[maskC], V[maskC],color="r",marker="o",linestyle="None",markersize=4, alpha=0.5);


I am working on the file "06_PlottingWithPython.ipynb".
The first task is:
Bad initial guesses can lead to very bad fits
Can you write Python code for it?

Aguess = 35
Bguess = 1.0

fitpars, error = curve_fit(ringo,D3['x'],D3['y'],p0=[Aguess,Bguess])

print(fitpars)

plt.plot(Z, ringo(Z, *fitpars), 'r-')
plt.plot(Z, ringo(Z,Aguess,Bguess), 'g--')

plt.plot(D3['x'],D3['y'],marker="o",linestyle="None",markersize=5);


I am working on the file "06_PlottingWithPython.ipynb".
The first task is:
Curve Fitting
Can you write Python code for it?

D1 = QTable.read('data1.csv', format='ascii.csv')
D1[0:2]

plt.plot(D1['x'],D1['y'],marker="o",linestyle="None",markersize=5);

# 1-D fit y = ax + b

Fit1 = np.polyfit(D1['x'],D1['y'],1)

Fit1       # The coefficients of the fit (a,b)

Yfit = np.polyval(Fit1,D1['x'])   # The polynomial of Fit1 applied to the points D1['x']

plt.plot(D1['x'], D1['y'], marker="o", linestyle="None", markersize=5)

plt.plot(D1['x'], Yfit, linewidth=4, color='c', linestyle='--')

D2 = QTable.read('data2.csv', format='ascii.csv')

plt.plot(D2['x'],D2['y'],marker="o",linestyle="None",markersize=5);

# 2-D fit y = ax**2 + bx + c

Fit2 = np.polyfit(D2['x'],D2['y'],2)

Fit2

Yfit = np.polyval(Fit2,D2['x'])

plt.plot(D2['x'], D2['y'], marker="o", linestyle="None", markersize=5)

plt.plot(D2['x'], Yfit, linewidth=3, color='y', linestyle='--');

# Be careful, very high-order fits may be garbage

Fit3 = np.polyfit(D1['x'],D1['y'],20)

xx = np.linspace(0,10,200)

Yfit = np.polyval(Fit3,xx)

plt.plot(D1['x'], D1['y'], marker="o", linestyle="None", markersize=8)
plt.plot(xx, Yfit, linewidth=3, color='m', linestyle='--');

plt.ylim(-20,120)


I am working on the file "06_PlottingWithPython.ipynb".
The first task is:
Everyone likes Pie
Can you write Python code for it?

fig,ax = plt.subplots(1,1)                    # One window
fig.set_size_inches(6,6)                   # (width,height) - letter paper landscape

fig.tight_layout()                          # Make better use of space on plot

ax.set_aspect('equal')

labels = np.array(['John', 'Paul' ,'George' ,'Ringo'])     # Name of slices
sizes = np.array([0.3, 0.15, 0.45, 0.10])                  # Relative size of slices
colors = np.array(['r', 'g', 'b', 'c'])                    # Color of Slices
explode = np.array([0, 0, 0.1, 0])                         # Offset slide 3

ax.pie(sizes, explode=explode, labels=labels, colors=colors,
       startangle=90, shadow=True);


plt.axis('equal')

labels = ('John', 'Paul' ,'George' ,'Ringo')     # Name of slices
sizes = [0.3, 0.15, 0.45, 0.10]                  # Size of slices
colors = ['r', 'g', 'b', 'c']                    # Color of Slices
explode = (0, 0, 0.1, 0)                         # Offset slide 3

plt.pie(sizes,explode=explode,labels=labels,colors=colors,shadow=True);


I am working on the file "06_PlottingWithPython.ipynb".
The first task is:
Fitting a specific function
Can you write Python code for it?

D3 = QTable.read('data3.csv', format='ascii.csv')

plt.plot(D3['x'],D3['y'],marker="o",linestyle="None",markersize=5);

from scipy.optimize import curve_fit


I am working on the file "06_PlottingWithPython.ipynb".
The first task is:
In addition, you can specify colors in many different ways:

Grayscale intensities: color = '0.8'
RGB triplets: color = (0.3, 0.1, 0.9)
RGB triplets (with transparency): color = (0.3, 0.1, 0.9, 0.4)
Hex strings: color = '#7ff00'
HTML color names: color = 'Chartreuse'
a name from the xkcd color survey prefixed with 'xkcd:' (e.g., 'xkcd:sky blue')

matplotlib will work with Astropy units
Can you write Python code for it?

from astropy import units as u

from astropy.visualization import quantity_support
quantity_support()

v = 10 * u.m / u.s
t2 = np.linspace(0,10,1000) * u.s
y = v * t2

plt.plot(t2,y)