[root@localhost src]# gradle --stop No Gradle daemons are running. [root@localhost src]# gradle hello World Starting a Gradle Daemon (subsequent builds will be faster) FAILURE: Build failed with an exception. * What went wrong: Task 'hello' not found in root project 'src'. Some candidates are: 'help'. * Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 7.419 secs [root@localhost src]# gradle hello World FAILURE: Build failed with an exception. * What went wrong: Task 'hello' not found in root project 'src'. Some candidates are: 'help'. * Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 1.66 secs
install gradle into CentOS
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget https://services.gradle.org/distributions/gradle-3.1-all.zip [root@localhost src]# unzip gradle-3.1-all.zip [root@localhost src]# mv gradle-3.1 /usr/local/bin/
環境パスを追加
[root@localhost src]# vi ~/.bash_profile
export PATH=$PATH:/usr/local/bin/gradle-3.1/bin
反映
export PATH=$PATH:/usr/local/bin/gradle-3.1/bin
バージョン確認
[root@localhost src]# gradle -v ------------------------------------------------------------ Gradle 3.1 ------------------------------------------------------------ Build time: 2016-09-19 10:53:53 UTC Revision: 13f38ba699afd86d7cdc4ed8fd7dd3960c0b1f97 Groovy: 2.4.7 Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015 JVM: 1.8.0_111 (Oracle Corporation 25.111-b15) OS: Linux 2.6.32-642.6.2.el6.x86_64 amd64
Try gradle
git clone and run shell script
[vagrant@localhost 1.01-Exercise-RunYourFirstTask]$ ./gradlew hello Downloading https://services.gradle.org/distributions/gradle-3.3-bin.zip .................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................. Unzipping /home/vagrant/.gradle/wrapper/dists/gradle-3.3-bin/64bhckfm0iuu9gap9hg3r7ev2/gradle-3.3-bin.zip to /home/vagrant/.gradle/wrapper/dists/gradle-3.3-bin/64bhckfm0iuu9gap9hg3r7ev2 Set executable permissions for: /home/vagrant/.gradle/wrapper/dists/gradle-3.3-bin/64bhckfm0iuu9gap9hg3r7ev2/gradle-3.3/bin/gradle Starting a Gradle Daemon (subsequent builds will be faster) :hello Hello World! BUILD SUCCESSFUL Total time: 1 mins 7.442 secs
task hello {
doLast {
println "Hello, Jeremy"
}
}
Code snipet
if(chat.getAuthor().getAvatarId() == 0){
Picasso.with(getContext()).load(android.R.color.transparent).into(chat_author_avatar);
chat_author_avatar.setBackgroundColor(chat.getAuthor().getColor());
} else {
Picasso.with(getContext()).load(chat.getAuthor().getAvatarId()).into(
chat_author_avatar);
chat_author_avatar.setBackgroundColor(Color.TRANSPARENT);
}
public int compute Fibonacci(int positionInFibSequence)
{
if (positionInFibSequence <= 2){
return 1;
} else {
return computeFibonacci(positionInFibSequence -1)
+ computeFibonacci(positionInFibSequence -2)
}
}[/python]
[python]
private class SepiaFilterTask extends AsyncTask {
protected Bitmap doInBackground(Bitmap... bitmaps){
Bitmap loadedBitmap = bitmaps[0];
}
sepiaBitmap.setPixel(x, y, Color.argb(alpha, outRed, outGreen, outBlue));
}
}
return sepiaBitmap;
}[/python]
Optimizer
an optimizer
– find minimum values of functions
– build parameterized models based on data
– refine allocations to stocks in portfolios
f(x) = x^2 + x^3 + s
f(x) = (x-1.5)^2 + 0.5
"""Minimize an objective function, using SciPy."""
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as spo
def f(X):
"""Given a scalar X, return some value (a real number)."""
Y = (X - 1.5)**2 + 0.5
print "X = {}, Y = {}".format(X, Y)
return Y
def test_run():
Xguess = 2.0
min_result = spo.minimize(f, Xguess, method='SLSQP', options={'disp': True})
print "Minima found at:"
print "X = {}, Y = {}".format(min_result.x, min_result.fun)
if __name__ == "__main__":
test_run()
Pandas Fillna()
Pandas Fillna() documentation
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html
DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
fillna(method='ffill')
"""Plot a histogram"""
import pandas as pd
import matplotlib.pyplot as plt
from util import get_data, plot_data
def compute_daily_returns(df):
daily_returns = df.copy()
daily_returns[1:] = (df[1:] / df[:-1].values) - 1
daily_returns.ix[0, :] = 0
return daily_returns
def test_run():
dates = pd.date_range('2009-01-01','2012-12-31')
symbols = ['SPY']
df = get_data(symbols, dates)
plot_data(df)
daily_returns - compute_daily_returns(df)
plot_data(daily_returns, title="Daily returns", ylabel="Daily returns")
if __name__ == "__main__":
test_run()
scatterplots in python
"""Scatterplot."""
import pandas as pd
import matplotlib.pyplot as plt
from util import get_data, plot_data
def compute_daily_returns(df):
daily_returns = df.copy()
daily_returns[1:] = (df[1:] / df[:-1].values) - 1
daily_returns.ix[0, :] = 0
return daily_returns
def test_run():
dates = pd.date_range('2009-01-01', '2012-12-31')
symbols = ['SPY', 'XOM', 'GLD']
df = get_data(symbols, dates)
daily_returns = compute_daily_returns(df)
daily_returns.plot(kind='scatter',x='SPY',y='XOM')
plt.show()
if __name__ == "__main__":
test_run()
Arithmetic operations
import numpy as np def test_run(): a = np.array([(1, 2, 3, 4, 5),(10, 20, 30, 40, 50)]) print "Original array a:\n", a print "\nMultiply a by 2:\n", 2 * a if __name__ == "__main__": test_run()
Rolling statistics is buying opportunity
rolling standard dev
def test_run():
dates = pd.date_range('2012-01-01','2012-12-31')
symbols = ['SPY']
df = get_data(symbols, dates)
ax = df['SPY'].plot(title="SPY rolling mean", label='SPY')
rm_SPY = pd.rolling_mean(df['SPY'], window=20)
rm_SPY.plot(label='Rolling mean', ax=ax)
Array attributes
import numpy as np def test_run(): a = np.random.random((5, 4)) print a print a.shape if __name__ == "__main__": test_run()
import numpy as np def test_run(): a = np.random.random((5, 4)) print a.size if __name__ == "__main__": test_run()
import numpy as np def test_run(): np.random.seed(693) a = np.random.randint(0, 10, size=(5, 4)) print "Array:\n", a if __name__ == "__main__": test_run()
Locate maximum value
import numpy as np def get_max_index(a): return a.argmax() def test_run(): a = np.array([9, 6, 2, 3, 12, 14, 7, 10], dtype=np.int32) print "Array:", a print "Maximum value", a.max() print "Index of max.:", get_max_index(a) if __name__ == "__main__": test_run()
Timing python operations
import time def time_run(): t1 = time.time() print "ML4T" t2 = time.time() print "The time taken by print statement is ",t2 - t1," seconds" if __name__ == "__main__": test_run()
Accessing array element
import numpy as np def test_run(): a = np.random.rand(5, 4) print "Array:\n", a element = a[3, 2] print element if __name__ == "__main__": test_run()
Indexing an array with another array
import numpy as np def test_run(): a = np.random.rand(5) indices = np.array([1,1,2,3]) print a[indices] if __name__ == "__main__": test_run()
Replace a slice
nd1[0:2,0:2]=nd2[-2:,2:4]
"""Creating NumPy arrays.""" import numpy as np def test_run(): print np.array([(2, 3, 4),(5, 6, 7)]) if __name__ == "__main__": test_run()
"""Creating NumPy arrays.""" import numpy as np def test_run(): print np.empty(5) print np.empty((5,4,3)) if __name__ == "__main__": test_run()
import numpy as np def test_run(): print np.ones((5, 4), dtype=np.int_) if __name__ == "__main__": test_run()
plot on “equal footing”
The best way to normalize price data so that all prices start at 1.0
df1 = df1/df1[0]
import os
import pandas as pd
import matplotlib.pyplot as plt
def plot_selected(df, columns, start_index, end_index):
def symbol_to_path(symbol, base_dir="data"):
"""Return CSV file path given ticker symbol."""
return os.path.join(base_dir, "{}.csv".format(str(symbol)))
def get_data(symbols, dates):
df = pd.DataFrame(index=dates)
if 'SPY' not in symbols:
symbols.insert(0, 'SPY')
for symbol in symbols:
df_temp = pd.read_csv(symbol_to_path(symbol), index_col='Date',
parse_dates=True, usecols=['Date', 'Adj Close'], na_values=['nan'])
df_temp = df_temp.rename(colums={'Adj Close': symbol})
df = df.join(df_temp)
if symbol = 'SPY':
df = df.dropna(subset=["SPY"])
return df
def plot_data(df, title="Stock prices"):
ax = df.plot(title=title, fontsize=12)
ax.set_xlabel("Date")
ax.set_ylabel("Price")
plt.show()
def test_run():
dates = pd.date_range('2010-01-01', '2010-12-31')
symbols = ['GOOG', 'IBM', 'GLD']
df = get_data(symbols, dates)
plot_selected(df, ['SPY', 'IBM'], '2010-03-01', '2010-04-01')
if __name__ == "__main__":
test_run()