def plot_champions(champs_df, champ_pca_array): champ_names = champs_df.index.values x = champ_pca_array[:, 0] y = champ_pca_array[:, 1] difficulty = champs_df['difficulty'].values magic = champs_df['attack'].values plt.figure(figsize=(20, 10)) plt.scatter(x, y, c=magic, s=difficulty * 1500, cmap=plt.get_cmap('Spectral')) for champ_name, x, y in zip(champ_names, x, y): plt.annotate(champ_name, xy=(x, y), xytext=(-20, 20), textcoords='offset points', ha='right', va='bottom', bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')) plt.savefig('image.png') elice_utils.send_image('image.png') plt.clf() plt.close()
def draw_dataset(X, R): # Code from Jooyeon's homework global FILE_PREFIX filename = FILE_PREFIX + "dataset.svg" colors = [] for r_i in R: max_r = max(r_i) for k in range(len(r_i)): if r_i[k] == max_r: colors.append(k + 1) break plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.gca().spines['bottom'].set_color('#999999') plt.gca().spines['left'].set_color('#999999') plt.xlabel('x1', fontsize=20, color='#555555') plt.ylabel('x2', fontsize=20, color='#555555') plt.tick_params(axis='x', colors='#777777') plt.tick_params(axis='y', colors='#777777') plt.scatter(X[:, 0], X[:, 1], c=colors, edgecolor='none', s=30) plt.savefig(filename) elice_utils.send_image(filename) plt.close()
def draw(X, y, clf): filename = "nonlinear_svm.png" ###These are to make the figure clearer. You don't need to change this part. plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.gca().spines['bottom'].set_color('#999999') plt.gca().spines['left'].set_color('#999999') plt.xlabel('x1', fontsize=20, color='#555555') plt.ylabel('x2', fontsize=20, color='#555555') plt.tick_params(axis='x', colors='#777777') plt.tick_params(axis='y', colors='#777777') x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 h = 0.5 # step size in the mesh xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contour(xx, yy, Z, colors='#CE5A57', alpha=0.25) plt.scatter(X[:, 0], X[:, 1], c=['#444C5C' if yy == 1 else '#78A5A3' for yy in y], edgecolor='none', s=30) plt.savefig(filename) elice_utils.send_image(filename) plt.close()
def one_var_default(x_vec, y_vec): filename = "default_logit_fig.png" regr_linear = linear_model.LinearRegression() regr_linear.fit(x_vec, y_vec) regr_logistic = linear_model.LogisticRegression() regr_logistic.fit(x_vec, y_vec) print("Independent variable: {}".format("Balance")) print("Coefficients: {}".format(regr_logistic.coef_)) print("Intercept: {}".format(regr_logistic.intercept_)) x_minmax = np.arange(x_vec.min(), x_vec.max()).reshape(-1, 1) plt.plot(x_vec, regr_linear.predict(x_vec), color='blue', linewidth=3) plt.plot(x_minmax, regr_logistic.predict_proba(x_minmax)[:, 1], color='red', linewidth=3) plt.scatter(x_vec, y_vec, color='black') plt.xlim((x_vec.min(), x_vec.max())) plt.savefig(filename) elice_utils.send_image(filename) plt.close()
def plotResult(data): ''' 숫자들로 이루어진 리스트 data가 주어질 때, 이 data의 분포를 그래프로 나타냅니다. 이 부분은 수정하지 않으셔도 됩니다. ''' filename = "uniform.png" frequency = [0 for i in range(int(max(data)) + 1)] for element in data: frequency[int(element)] += 1 n = len(frequency) myRange = range(0, len(frequency)) width = 1 plt.bar(myRange, frequency, width, color="blue") plt.xlabel("Sample", fontsize=20) plt.ylabel("Frequency", fontsize=20) plt.savefig(filename) elice_utils.send_image(filename) plt.close()
def draw_chart(X, Y, slope, intercept, x_col): fig = plt.figure() fig.suptitle('Linear Regression for Class Data') ax = fig.add_subplot(111) ax.set_xlabel(x_col) ax.set_ylabel('GPA') plt.scatter(X, Y) min_X = min(X) max_X = max(X) min_Y = min_X * slope + intercept max_Y = max_X * slope + intercept plt.plot([min_X, max_X], [min_Y, max_Y], color='red', linestyle='--', linewidth=3.0) ax.text(min_X, min_Y + 0.1, r'$y = %.2lfx + %.2lf$' % (slope, intercept), fontsize=15) plt.savefig('chart.svg') elice_utils.send_image('chart.svg')
def draw(X, y, W, b, support_vec_idx): filename = "svm.png" ###These are to make the figure clearer. You don't need to change this part. plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.gca().spines['bottom'].set_color('#999999') plt.gca().spines['left'].set_color('#999999') plt.xlabel('x1', fontsize=20, color='#555555') plt.ylabel('x2', fontsize=20, color='#555555') plt.tick_params(axis='x', colors='#777777') plt.tick_params(axis='y', colors='#777777') plt.scatter(X[:, 0], X[:, 1], c=['#444C5C' if yy == 1 else '#78A5A3' for yy in y], edgecolor='none', s=30) ls = np.linspace(-50, 50, 100) plt.plot(ls, [-1 * (W[0] * v + b) / W[1] for v in ls], lw=1.5, color='#CE5A57') plt.scatter(X[:, 0][support_vec_idx], X[:, 1][support_vec_idx], s=200, c='#CE5A57', edgecolor='none', marker='*') plt.savefig(filename) elice_utils.send_image(filename) plt.close()
def visualize(clf, X, Y): X_ = [] Y_ = [] colors = [] shapes = [] plt.figure(figsize=(6, 6)) for x, y in zip(X, Y): X_.append(x[1]) Y_.append(x[0]) if y == 0: colors.append('b') else: colors.append('r') if clf.predict([x])[0] == y: shapes.append('o') else: shapes.append('x') for x, y in zip(X, Y): c = '#87CEFA' if clf.predict([x])[0] == 1: c = '#fab387' plt.scatter(x[1], x[0], marker='s', c=c, s=1200, edgecolors='none') for _s, c, _x, _y in zip(shapes, colors, X_, Y_): plt.scatter(_x, _y, marker=_s, c=c, s=200) plt.savefig("image.svg", format="svg") elice_utils.send_image("image.svg")
def main(): __download_file = "./download.jpg" image_url = "http://i.imgur.com/A8eQsll.jpg" download_image(image_url, __download_file) elice_utils.send_image(__download_file) classify_image.main(__download_file)
def main(): #### Generate data #### # Set mean and standard deviation of nomral distribution for each label mu = np.array([[1, 2], [1, -3], [5, 0]]) std = 0.7 # Set the number of samples Num = 50 # Sample from the normal distributions X = np.concatenate( [np.random.normal(loc=mu, scale=std) for i in range(Num)]) y = np.concatenate([[0, 1, 2] for i in range(Num)]) labels = set(y) colours = ['red', 'green', 'blue'] # Initialize K-means center points centers = initialize(X, 3) preds = y #### Plot results of K-means clustering #### for i in range(10): # Create an empty pyplot figure plt.figure() # Scatter plot for each label for label in labels: idx = (preds == label) plt.scatter(X[idx, 0], X[idx, 1], color=colours[label], marker='.') # Plot K-means center points plt.scatter(centers[:, 0], centers[:, 1], marker='x', color='black') # Set title plt.title('Iteration : ' + str(i)) # Show the plot filename = 'kmeans' + str(i) + '.png' plt.savefig(filename) elice_utils.send_image(filename) # Proceed the next iteration of K-means clustering kmeans = KMeans(n_clusters=3, n_init=1, init=centers, max_iter=1, random_state=0).fit(X) preds = kmeans.labels_ centers = kmeans.cluster_centers_
def plotting(samples, m, s): count, bins, ignored = plt.hist(samples, 30, normed=True) plt.plot(bins, norm.pdf(bins, m, s), linewidth=2, color='r') plt.xlabel("X", fontsize=20) plt.ylabel("P(X)", fontsize=20) plt.xlim(0, 100) plt.savefig("normal_samples.png") elice_utils.send_image("normal_samples.png") plt.close() return
def plot_labels(data, labels): "Clusering 결과를 시각화합니다. 인수로 데이터, 예측된 레이블을 받습니다." def deco_plot(ax): ax.set_xlabel("X1", fontsize=15); ax.set_ylabel("X2", fontsize=15) ax.set_ylim([0,100]); ax.set_xlim([0,100]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(data[:,0], data[:,1], c=labels, s=20, alpha=.7, linewidth=0) deco_plot(ax) plt.savefig("results.png") elice_utils.send_image("results.png") plt.close()
def plotting_samples(samples, color): "생성된 표본을 시각화합니다. 표본에 부여된 색상은 정답을 의미합니다." def deco_plot(ax): ax.set_xlabel("X1", fontsize=15); ax.set_ylabel("X2", fontsize=15) ax.set_ylim([0,100]); ax.set_xlim([0,100]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(samples[:,0], samples[:,1], c=color, s=20, alpha=.7, linewidth=0) deco_plot(ax) plt.savefig('samples.png') elice_utils.send_image('samples.png') plt.close()
def main(mus, cov): samples = generate_samples(mus, cov) fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) MVN_2d_plot(ax1, samples, mus, cov) deco_plot(ax1) ax2 = fig.add_subplot(1, 2, 2, projection='3d') MVN_3d_plot(ax2, samples, mus, cov) deco_plot(ax2) plt.savefig('test.png') elice_utils.send_image('test.png') return
def draw_datasetonly(X, y): filename = "dataset.png" ###These are to make the figure clearer. You don't need to change this part. plt.gca().spines['top'].set_visible(False) plt.gca().spines['right'].set_visible(False) plt.gca().spines['bottom'].set_color('#999999') plt.gca().spines['left'].set_color('#999999') plt.xlabel('x1', fontsize=20, color='#555555'); plt.ylabel('x2', fontsize=20, color='#555555') plt.tick_params(axis='x', colors='#777777') plt.tick_params(axis='y', colors='#777777') plt.scatter(X[:, 0], X[:, 1], c = ['#444C5C' if yy == 1 else '#78A5A3' for yy in y], edgecolor='none', s=30) plt.savefig(filename) elice_utils.send_image(filename) plt.close()
def doKMeans(X): X = np.array(X) labels = set([0, 1, 2]) colours = ['red', 'green', 'blue'] # Initialize K-means center points centers = [ np.random.uniform(np.min(X[:, col]), np.max(X[:, col]), [3, 1]) for col in range(X.shape[1]) ] centers = np.concatenate(centers, axis=1) preds = np.zeros(len(X)) #### Plot results of K-means clustering #### for i in range(10): # Proceed the next iteration of K-means clustering kmeans = KMeans(n_clusters=3, n_init=1, init=centers, max_iter=1, random_state=0).fit(X) preds = kmeans.labels_ centers = kmeans.cluster_centers_ # Create an empty pyplot figure plt.figure() # Scatter plot for each label for label in labels: idx = (preds == label) plt.scatter(X[idx, 0], X[idx, 1], color=colours[label], marker='.') # Plot K-means center points plt.scatter(centers[:, 0], centers[:, 1], marker='x', color='black') # Set title plt.title('KMeans iteration : ' + str(i)) # Show the plot filename = 'kmeans' + str(i) + '.png' plt.savefig(filename) elice_utils.send_image(filename)
def main(): data = np.array(read_data("data.txt")) ''' distance는 distance() 함수를 argument로 보내는 것입니다. 다음 문제에서 agglomerative_cluster 함수를 직접 만들어 보겠습니다. ''' cluster_histories = agglomerative_cluster(data, distance) ''' 여기서부터 애니메이션 그래프를 생성하는 코드입니다. ''' fig, ax = plt.subplots() fig.set_tight_layout(True) ''' 각 프레임별로 색깔을 어떻게 세팅할지 정의합니다. ''' def get_colors(clusters): colors = [0] * len(data) for i in range(len(clusters)): for pi in clusters[i]: colors[pi] = i return colors ''' 첫 번째 프레임을 만듭니다. ''' ax.scatter(data[:, 0], data[:, 1], c=get_colors(cluster_histories[0])) ''' i번째 프레임을 어떻게 업데이트할지 정의합니다. ''' def update(i): label = 'timestep {0}'.format(i) ax.scatter(data[:, 0], data[:, 1], c=get_colors(cluster_histories[i])) ax.set_title(label) return ax ''' 애니메이션을 만듭니다. ''' anim = FuncAnimation(fig, update, frames=np.arange(0, len(data)), interval=1000) anim.save('line.gif', dpi=80, writer='imagemagick') elice_utils.send_image('line.gif')
def draw_chart(X, Y, slope, intercept): fig = plt.figure() ax = fig.add_subplot(111) plt.scatter(X, Y) # 차트의 X, Y축 범위와 그래프를 설정합니다. min_X = min(X) max_X = max(X) min_Y = min_X * slope + intercept max_Y = max_X * slope + intercept plt.plot([min_X, max_X], [min_Y, max_Y], color='red', linestyle='--', linewidth=3.0) # 기울과와 절편을 이용해 그래프를 차트에 입력합니다. ax.text(min_X, min_Y + 0.1, r'$y = %.2lfx + %.2lf$' % (slope, intercept), fontsize=15) plt.savefig('chart.png') elice_utils.send_image('chart.png')
def plotting(Y, labels): # plot the results legend_ = [] colors = cm.rainbow(np.linspace(0, 1, 10)) for i in sorted(list(set(labels))): idxs = (labels == i).nonzero() l = plt.scatter(np.squeeze(Y[idxs, 0]), Y[idxs, 1], 20, color=colors[int(i)]) legend_.append(l) plt.legend(legend_, list(range(10)), loc='center left', ncol=1, fontsize=8, bbox_to_anchor=(1, 0.5)) plt.savefig("result.png") elice_utils.send_image("result.png") return
def plotResult(data): frequency = [0 for i in range(max(data) + 1)] for element in data: frequency[element] += 1 n = len(frequency) myRange = range(1, n) width = 1 plt.bar(myRange, frequency[1:]) plt.xlabel("Sample", fontsize=20) plt.ylabel("Frequency", fontsize=20) filename = "chart.svg" plt.savefig(filename) elice_utils.send_image(filename) plt.close()
def plot(D, W, n1): n2 = ["D1", "D2", "D3", "D4", "D5"] fig, ax = plt.subplots() ax.scatter(W[0], W[1]) ax.scatter(D.T[0], D.T[1]) for i, txt in enumerate(n1): if (txt == "free"): ax.annotate(txt, (W[0][i] - 0.13, W[1][i] - 0.02)) elif txt == "motto": ax.annotate(txt, (W[0][i] - 0.06, W[1][i] + 0.07)) elif txt == "know": ax.annotate(txt, (W[0][i] - 0.14, W[1][i] + 0.03)) elif txt == "live": ax.annotate(txt, (W[0][i] + 0.03, W[1][i] - 0.03)) else: ax.annotate(txt, (W[0][i] + 0.02, W[1][i] + 0.02)) for i, txt in enumerate(n2): ax.annotate(txt, (D.T[0][i] + 0.02, D.T[1][i] + 0.03)) plt.savefig('demo.png') elice_utils.send_image('demo.png')
def main(): plt.figure(figsize=(5,5)) X = [] Y = [] # N을 10배씩 증가할 때 파이 값이 어떻게 변경되는지 확인해보세요. N = 10000 for i in range(N): X.append(np.random.rand() * 2 - 1) Y.append(np.random.rand() * 2 - 1) X = np.array(X) Y = np.array(Y) distance_from_zero = np.sqrt(X * X + Y * Y) is_inside_circle = distance_from_zero <= 1 print("Estimated pi = %f" % (np.average(is_inside_circle) * 4)) plt.scatter(X, Y, c=is_inside_circle) plt.savefig('circle.png') elice_utils.send_image('circle.png')
def main(): plt.figure(figsize=(5,5)) X = [] Y = [] # N을 10배씩 증가할 때 파이 값이 어떻게 변경되는지 확인해보세요. N = 10000 for i in range(N): X.append(np.random.random()) Y.append(np.random.random()) X = np.array(X) Y = np.array(Y) X = X * 2 - 1 # [0, 1] --> [0, 2] --> [-1, 1] Y = Y * 2 - 1 dist = np.sqrt(X ** 2 + Y ** 2) is_inside_circle = dist <= 1 print("Estimated pi = %f" % (np.average(is_inside_circle) * 4)) plt.scatter(X, Y, c=is_inside_circle) plt.savefig('circle.png') elice_utils.send_image('circle.png')
def one_var_advertising_pred(x_vec, y_vec, x_label, y_label, rs=108): filename = "advertising_fig_simple_train_test_{}.png".format(x_label) x_train, x_test, y_train, y_test = train_test_split(x_vec, y_vec, test_size=0.2, random_state=rs) regr = linear_model.LinearRegression() regr.fit(x_train, y_train) predicted_y_test = regr.predict(x_test) print(filename) print("Independent variable: {}".format(x_label)) print("Coefficients: {}".format(regr.coef_)) print("Intercept: {}".format(regr.intercept_)) print("RSS on test data: {}".format(np.sum( (predicted_y_test - y_test)**2))) print("MSE on test data: {}".format( mean_squared_error(y_test, predicted_y_test))) print("R^2 on test data: {}".format(r2_score(y_test, predicted_y_test))) # Another way to compute the R^2 score # print("R^2 on test data: {}".format(regr.score(x_test, y_test))) print() plt.scatter(x_train, y_train, color='black') plt.plot(x_train, regr.predict(x_train), color='blue', linewidth=3) plt.xlim((0, int(max(x_vec) * 1.1))) plt.xlabel(x_label, fontsize=20) plt.ylabel(y_label, fontsize=20) plt.savefig(filename) elice_utils.send_image(filename) plt.close()
def visualize_2d_wine(X): '''X를 시각화하는 코드를 구현합니다.''' plt.scatter(X[:, 0], X[:, 1]) plt.savefig("image.png") elice_utils.send_image("image.png")
def visualize_2d_wine(X, labels): plt.figure(figsize=(10, 6)) plt.scatter(X[:,0],X[:,1],c=labels) plt.savefig("image.svg", format="svg") elice_utils.send_image("image.svg")
#Scatter plot the resulting vectors fig, ax = plt.subplots() ax.scatter(Word.T[0], Word.T[1]) ax.scatter(Doc[0], Doc[1]) for i, txt in enumerate(name_word): if (txt == "free"): ax.annotate(txt, (Word.T[0][i] - 0.06, Word.T[1][i] + 0.01)) else: ax.annotate(txt, (Word.T[0][i] + 0.01, Word.T[1][i] + 0.01)) for i, txt in enumerate(name_doc): ax.annotate(txt, (Doc[0][i] + 0.01, Doc[1][i] + 0.01)) plt.savefig('demo.png') elice_utils.send_image('demo.png') #Compute the vector for the query query = (Word[3] + Word[5]) / 2 #Compute the cosine distance between that query and each document results = [] for i in range(len(Doc.T)): results.append((name_doc[i], cosine(Doc.T[i], query))) #Sort the results in descending order results.sort(key=lambda X: X[1]) print("Closest documents to the given query are: ") print("Results = ", results)
students_data = [] students_target = [] # 1 f = open('./students_train.csv', 'r') for line in f: student = line.strip().split(',') students_data.append([ float(student[0]), 1 if student[2] == 'female' else 0, 1 if student[3] == 'white' else 0, 1 if student[3] == 'black' else 0, 1 if student[3] == 'hispanic' else 0 ]) students_target.append(1 if float(student[1]) >= 185 else 0) f.close() # 2 clf = tree.DecisionTreeClassifier(max_depth=2) clf = clf.fit(students_data, students_target) dot_data = tree.export_graphviz(clf, out_file=None, feature_names=['weight', 'is_woman', 'is_white', 'is_black', 'is_hispanic'], class_names=['shorter_than_185', 'taller_than_185'] ) graph = pydotplus.graph_from_dot_data(dot_data) graph.write_png("graph.png") elice_utils.send_image("graph.png")
''' 이 밑의 코드는 시각화를 위한 코드입니다. 궁금하다면 살펴보세요. ''' def visualize_boxplot(title, values, labels): width = .35 print(title) fig, ax = plt.subplots() ind = numpy.arange(len(values)) rects = ax.bar(ind, values, width) ax.bar(ind, values, width=width) ax.set_xticks(ind + width/2) ax.set_xticklabels(labels) def autolabel(rects): # attach some text labels for rect in rects: height = rect.get_height() ax.text(rect.get_x()+rect.get_width()/2., height + 0.01, '%.2lf%%' % (height * 100), ha='center', va='bottom') autolabel(rects) plt.savefig("image.svg", format="svg") elice_utils.send_image("image.svg") if __name__ == "__main__": main()
# Initialize variables sess.run(tf.global_variables_initializer()) # Fail to run session (Unfeeded placeholder) try: y = sess.run(sine) except: print('Feed the placeholder') # Plot sine graph plt.figure() plt.plot(np.pi * np.linspace(-1., 1.), sess.run(sine, feed_dict)) plt.title('Plot sine graph from -1. to 1. with amplitude ' + str(sess.run(A)) + ' frequency ' + str(sess.run(f))) plt.savefig('fig1.png') elice_utils.send_image('fig1.png') # Modify amplitude (Re-assign variable value) plt.figure() sess.run(A.assign(2.)) plt.plot(np.pi * np.linspace(-1., 1.), sess.run(sine, feed_dict)) plt.title('Plot sine graph from -1. to 1. with amplitude ' + str(sess.run(A)) + ' frequency ' + str(sess.run(f))) plt.savefig('fig2.png') elice_utils.send_image('fig2.png') # Modify frequency (Re-assign variable value) plt.figure() sess.run(f.assign(2.)) plt.plot(np.pi * np.linspace(-1., 1.), sess.run(sine, feed_dict)) plt.title('Plot sine graph from -1. to 1. with amplitude ' + str(sess.run(A)) + ' frequency ' + str(sess.run(f))) plt.savefig('fig3.png')